The CASE statement is SQL’s if-then-else. It checks conditions in order and returns the value of the first one that is true: CASE WHEN amount >= 10000 THEN ‘Premium’ WHEN amount >= 2000 THEN ‘Mid’ ELSE ‘Budget’ END turns an order amount into a price band. If no condition is true and there is no ELSE, it returns NULL.
CASE is an expression, not a separate command, so it works anywhere a value can go: in SELECT to create a new column, inside SUM or COUNT for conditional totals, in GROUP BY to group by bands, in ORDER BY for a custom sort, and in UPDATE to change different rows in different ways. This lesson shows each one on a single dataset, plus the two mistakes that produce wrong answers without an error.
CASE syntax: simple and searched
There are two forms. The simple form compares one column against a list of values. The searched form tests a full condition on each line, and it is the one you will use most.
| Form | What it does | Syntax | Example |
|---|---|---|---|
| Simple CASE | Matches one expression against fixed values | CASE column WHEN value1 THEN result1 WHEN value2 THEN result2 ELSE default END | CASE payment_mode WHEN ‘COD’ THEN ‘Cash on delivery’ ELSE ‘Prepaid’ END |
| Searched CASE | Tests any condition on each line | CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ELSE default END | CASE WHEN amount >= 10000 THEN ‘Premium’ WHEN amount >= 2000 THEN ‘Mid’ ELSE ‘Budget’ END |
| With an alias | Names the new column | CASE … END AS name | CASE WHEN delivered_days > promised_days THEN ‘Late’ ELSE ‘On time’ END AS delivery_status |
Every CASE needs an END. Forgetting it is the most common syntax error, and the message the database gives rarely points at the missing word.
The simple form can only test equality. It cannot do ranges, it cannot test IS NULL, and it cannot combine columns. Anything the simple form does, the searched form can do too, so if in doubt, write the searched form.
The data used throughout
A Chennai-based online seller of home appliances, shipping to customers in three southern states. promised_days is the delivery time quoted at checkout, and delivered_days is how long delivery actually took. It is NULL for the two orders still in transit. Amounts are in rupees.
| order_id | state | payment_mode | amount | promised_days | delivered_days |
|---|---|---|---|---|---|
| 6001 | Tamil Nadu | UPI | 1299 | 3 | 2 |
| 6002 | Karnataka | COD | 4599 | 4 | 6 |
| 6003 | Tamil Nadu | Card | 899 | 3 | 3 |
| 6004 | Kerala | UPI | 12999 | 5 | NULL |
| 6005 | Karnataka | UPI | 2499 | 4 | 4 |
| 6006 | Kerala | COD | 649 | 5 | 7 |
| 6007 | Tamil Nadu | Card | 7999 | 3 | 2 |
| 6008 | Karnataka | UPI | 18999 | 4 | NULL |
Example 1: relabel values with a simple CASE
Finance wants orders split into cash on delivery and prepaid, because COD orders carry return risk and a collection fee. UPI and card both count as prepaid.
| Part | SQL |
|---|---|
| Columns | SELECT order_id, payment_mode, |
| Simple CASE | CASE payment_mode WHEN ‘COD’ THEN ‘Cash on delivery’ WHEN ‘UPI’ THEN ‘Prepaid’ WHEN ‘Card’ THEN ‘Prepaid’ END AS payment_type |
| Source | FROM orders |
| order_id | payment_mode | payment_type |
|---|---|---|
| 6001 | UPI | Prepaid |
| 6002 | COD | Cash on delivery |
| 6003 | Card | Prepaid |
| 6004 | UPI | Prepaid |
| 6005 | UPI | Prepaid |
| 6006 | COD | Cash on delivery |
| 6007 | Card | Prepaid |
| 6008 | UPI | Prepaid |
There is no ELSE here. If the business adds net banking next month, those orders get a payment_type of NULL, which is at least visible in a report. An ELSE ‘Prepaid’ would quietly lump net banking in with UPI, which may or may not be what anyone wants. Decide deliberately which you prefer.
Example 2: bucket amounts into bands (searched CASE)
The category team wants to see orders by price band: Premium at 10,000 rupees and above, Mid from 2,000, Budget below that.
| Part | SQL |
|---|---|
| Columns | SELECT order_id, amount, |
| Searched CASE | CASE WHEN amount >= 10000 THEN ‘Premium’ WHEN amount >= 2000 THEN ‘Mid’ ELSE ‘Budget’ END AS price_band |
| Source | FROM orders |
| order_id | amount | price_band |
|---|---|---|
| 6001 | 1299 | Budget |
| 6002 | 4599 | Mid |
| 6003 | 899 | Budget |
| 6004 | 12999 | Premium |
| 6005 | 2499 | Mid |
| 6006 | 649 | Budget |
| 6007 | 7999 | Mid |
| 6008 | 18999 | Premium |
The second line says only amount >= 2000, not “between 2,000 and 9,999”, and that is correct. CASE stops at the first true condition, so any order of 10,000 or more has already been labelled Premium before the Mid line is checked.
That same rule makes the order of the lines matter. Swap the first two lines, so the Mid test comes first, and orders 6004 and 6008 (12,999 and 18,999) both come out as Mid, because 12,999 is also at least 2,000. The query runs without complaint and the Premium band is empty. When you bucket with greater-than tests, go from the highest threshold down. With less-than tests, go from the lowest up.
Example 3: handle NULLs explicitly
Operations wants each order marked Late, On time or In transit. The in-transit orders have no delivered_days, so they need their own line, and it has to come first.
| Part | SQL |
|---|---|
| Columns | SELECT order_id, promised_days, delivered_days, |
| CASE | CASE WHEN delivered_days IS NULL THEN ‘In transit’ WHEN delivered_days > promised_days THEN ‘Late’ ELSE ‘On time’ END AS delivery_status |
| Source | FROM orders |
| order_id | promised_days | delivered_days | delivery_status |
|---|---|---|---|
| 6001 | 3 | 2 | On time |
| 6002 | 4 | 6 | Late |
| 6003 | 3 | 3 | On time |
| 6004 | 5 | NULL | In transit |
| 6005 | 4 | 4 | On time |
| 6006 | 5 | 7 | Late |
| 6007 | 3 | 2 | On time |
| 6008 | 4 | NULL | In transit |
Leave out the IS NULL line and orders 6004 and 6008 fall through to ELSE and are reported as On time, which is false: they have not arrived. This is the subtle trap with ELSE. Any comparison with NULL is unknown, not true, so a NULL row never matches a WHEN and always lands in ELSE. Without an ELSE it would land on NULL instead, which is at least honest. Test for NULL first whenever the column can be empty.
Example 4: CASE inside an aggregate
Put a CASE inside SUM or COUNT and you get a conditional total: count only the late orders, or sum only the COD value. This is the standard way to turn rows into columns in a report.
| Part | SQL |
|---|---|
| Group | SELECT state, COUNT(*) AS orders, |
| Late orders | SUM(CASE WHEN delivered_days > promised_days THEN 1 ELSE 0 END) AS late, |
| COD value | SUM(CASE WHEN payment_mode = ‘COD’ THEN amount ELSE 0 END) AS cod_value |
| Source and grouping | FROM orders GROUP BY state ORDER BY state |
| state | orders | late | cod_value |
|---|---|---|---|
| Karnataka | 3 | 1 | 4599 |
| Kerala | 2 | 1 | 649 |
| Tamil Nadu | 3 | 0 | 0 |
One query gives a report the operations head can act on: Kerala has one late order out of two, and Tamil Nadu has no COD exposure at all. Our lesson on SQL aggregate functions goes further into this pattern, including why COUNT(CASE WHEN … THEN 1 END) works without an ELSE and how AVG behaves when the CASE returns NULL.
Example 5: GROUP BY a CASE, and sort with one
To get the band summary from Example 2, group by the CASE expression itself:
| Part | SQL |
|---|---|
| The band as a column | SELECT CASE WHEN amount >= 10000 THEN ‘Premium’ WHEN amount >= 2000 THEN ‘Mid’ ELSE ‘Budget’ END AS price_band, |
| Totals | COUNT(*) AS orders, SUM(amount) AS revenue |
| Source | FROM orders |
| Group by the same expression | GROUP BY CASE WHEN amount >= 10000 THEN ‘Premium’ WHEN amount >= 2000 THEN ‘Mid’ ELSE ‘Budget’ END |
| Sort bands high to low | ORDER BY MIN(amount) DESC |
| price_band | orders | revenue |
|---|---|---|
| Premium | 2 | 31998 |
| Mid | 3 | 15097 |
| Budget | 3 | 2847 |
Two premium orders bring in nearly two thirds of the revenue. MySQL and PostgreSQL also let you write GROUP BY price_band using the alias, which is shorter; SQL Server does not, so the full expression is the portable form. Our GROUP BY and HAVING lesson covers the alias rules in each database.
CASE also gives you a custom sort order, when alphabetical or numeric order is not what the reader wants. Operations wants the delivery list with problems at the top: late first, then in transit, then on time.
| Part | SQL |
|---|---|
| Columns | SELECT order_id, state, promised_days, delivered_days FROM orders |
| Custom sort | ORDER BY CASE WHEN delivered_days > promised_days THEN 1 WHEN delivered_days IS NULL THEN 2 ELSE 3 END, order_id |
The result lists 6002 and 6006 (late), then 6004 and 6008 (in transit), then the four on-time orders. The CASE returns a number only used for sorting; it never appears in the output.
Example 6: CASE in UPDATE
CASE lets one UPDATE change different rows in different ways. The team finds that checkout quoted Kerala one day too short because of a settings error, so Kerala’s promised_days needs one day added and every other state stays the same:
| Part | SQL |
|---|---|
| Update | UPDATE orders |
| Per-row value | SET promised_days = CASE state WHEN ‘Kerala’ THEN promised_days + 1 ELSE promised_days END |
Orders 6004 and 6006 now show 6 promised days. The ELSE promised_days is essential. Without it, every non-Kerala row would have promised_days set to NULL, because a CASE with no match returns NULL. That is the most damaging CASE mistake there is, because it overwrites data. In practice you would usually write WHERE state = ‘Kerala’ for a single change like this; CASE earns its place in UPDATE when several groups each need a different change in one pass.
CASE in each database, and the shortcuts
The CASE expression is standard SQL and works identically in MySQL, PostgreSQL, SQL Server, SQLite, Oracle, BigQuery and Snowflake. Each database also has shorter alternatives.
| Need | Standard CASE | MySQL | PostgreSQL | SQL Server |
|---|---|---|---|---|
| Two-way choice | CASE WHEN x THEN a ELSE b END | IF(x, a, b) | Use CASE | IIF(x, a, b) |
| Replace NULL with a value | CASE WHEN col IS NULL THEN 0 ELSE col END | IFNULL(col, 0) or COALESCE | COALESCE(col, 0) | ISNULL(col, 0) or COALESCE |
| Conditional count | COUNT(CASE WHEN x THEN 1 END) | Same, or SUM(x) | COUNT(*) FILTER (WHERE x) | Same as standard |
| Mixed result types | Must be compatible | Converted silently | Error | Error, or converts text to number and fails |
The last row matters. CASE WHEN x THEN ‘Late’ ELSE 0 END mixes text and a number. MySQL and SQLite return whatever they can, while SQL Server tries to convert ‘Late’ to a number and fails at run time. Keep every THEN and ELSE the same type. COALESCE is standard and works everywhere, so prefer it to IFNULL and ISNULL in code that might move between databases.
Mistakes people make with CASE
Lines in the wrong order. CASE takes the first true condition. A broad condition above a narrow one swallows it, as Example 2 showed.
No NULL line. NULL never matches a WHEN, so it falls into ELSE and gets labelled as whatever the default is.
No ELSE in an UPDATE. Every row that matches nothing is set to NULL.
Testing NULL with the simple form. CASE col WHEN NULL THEN … never matches, because it is an equality test. Use the searched form with IS NULL.
Forgetting END, or putting AS before it. The alias comes after END.
CASE is one of the conditional functions every analyst interview tests, usually as “bucket these customers” or “build this pivot in SQL”; our SQL interview questions include both. Once you are combining tables, CASE works the same way across a join, which our SQL joins guide covers.
Frequently asked questions
What is the CASE statement in SQL?
The CASE statement is SQL’s conditional expression. It checks a list of WHEN conditions in order and returns the THEN value of the first one that is true, or the ELSE value if none is. It is used to create labels and bands, build conditional totals, sort in a custom order and update rows differently in one statement.
What is the difference between simple CASE and searched CASE?
A simple CASE compares one expression with a list of fixed values, such as CASE payment_mode WHEN ‘COD’ THEN … END, and can only test equality. A searched CASE evaluates a full condition on each line, such as CASE WHEN amount >= 2000 THEN … END, so it can test ranges, NULLs and several columns. The searched form can do everything the simple form does.
What happens if no condition in CASE is true?
CASE returns the ELSE value if there is one, and NULL if there is not. This is why an UPDATE using CASE without an ELSE can set unmatched rows to NULL. It also means rows with NULL in the tested column fall through to ELSE, since comparisons with NULL are never true.
Can CASE be used in a WHERE clause?
Yes, CASE is an expression and can appear anywhere a value can, including WHERE, GROUP BY, ORDER BY and HAVING. In WHERE it is usually clearer to write the condition directly with AND and OR, though. CASE is most useful in SELECT, inside aggregates, and in ORDER BY for custom sorting.
Can you use multiple conditions in a CASE WHEN?
Yes. Each WHEN can hold any condition, including AND and OR, such as WHEN state = ‘Kerala’ AND payment_mode = ‘COD’ THEN ‘High risk’. You can also have as many WHEN lines as you need, and nest one CASE inside another, though a long nested CASE is usually easier to read as several WHEN lines.
Is there an IF function in SQL?
Standard SQL uses CASE for conditional logic. MySQL also has IF(condition, value_if_true, value_if_false), and SQL Server has IIF with the same shape. PostgreSQL has no IF function in queries, so CASE is used there. CASE is the portable choice that works in every database.
Turn raw rows into categories a manager can read
Bucketing, flagging and pivoting with CASE is a large share of everyday analyst SQL, and most of the errors in it come from condition order and NULLs rather than syntax.
SkilloVilla’s SQL: Beginner to Advanced course covers conditional functions, aggregation, joins and advanced SQL statements, taught in live classes with mentor support and practice on SQL playgrounds. If you want SQL as part of a full analyst programme with Excel, Python, Power BI and placement support, see the Data Analytics with Python track.