On this page
  1. CASE
  2. Syntax & Overview
  3. Notes & Best Practices
  4. Example

CASE

Prev: `NULLIF` | Up: functions | Next: `CAST`
Rel: functions, Aggregations, CTEs & Window Functions
Ref: PostgreSQL — SQL Commands
Tags: #public #databases #sql #functions #tier/core


Syntax & Overview

CASE WHEN score >= 90 THEN 'A' ELSE 'B' END

Inline conditional expression evaluated top-to-bottom.

Notes & Best Practices

Example

SELECT id,
  CASE
    WHEN score >= 90 THEN 'A'
    WHEN score >= 80 THEN 'B'
    ELSE 'C'
  END AS grade
FROM exams;

Linked from

Related notes