Learn SQL
SQL CASE WHEN Explained: Conditional Logic, Buckets, and Conditional Aggregation
CASE WHEN is SQL's if/else. Use it to label rows, build ranges, translate codes, and turn one grouped query into a compact pivot-style report.
On This Page
CASE WHEN is if/else for every row
Sooner or later every analysis needs a rule like: if the amount is small, call it small; otherwise call it large. In SQL, that rule is a CASE expression. It runs once per row and returns one value, so you can add a label column without changing the underlying table.
SELECT
order_id,
amount,
CASE
WHEN amount < 50 THEN 'small'
WHEN amount < 200 THEN 'medium'
ELSE 'large'
END AS size_band
FROM shop_orders
WHERE amount IS NOT NULL;The examples in this guide come from the CASE WHEN chapter in SQL for Files, a browser-based SQL editor that runs DuckDB locally on CSV, JSON, and Parquet files. You can run every query against one-click sample data without installing a database.
Searched CASE vs simple CASE
The searched form above lists full conditions, so it can test ranges, combine columns, or check for NULL. The simple form compares one column with fixed values and is shorter for lookups such as status codes.
SELECT
order_id,
CASE status
WHEN 'shipped' THEN 'complete'
WHEN 'pending' THEN 'open'
ELSE 'closed'
END AS status_group
FROM shop_orders;- Use the searched form for ranges, multiple columns, and NULL checks.
- Use the simple form when every branch is an equality test on the same column.
- CASE status WHEN NULL never matches, because NULL = NULL is not true.
Branch order and NULL values
CASE checks its branches from top to bottom and stops at the first one that is true. Without an ELSE, rows that match nothing get NULL. Missing values cause the most surprises: a comparison like NULL < 100 is not true, so the row skips every WHEN and lands in ELSE.
SELECT
order_id,
CASE
WHEN amount IS NULL THEN 'missing'
WHEN amount < 100 THEN 'under 100'
ELSE '100 or more'
END AS amount_label
FROM shop_orders;Check IS NULL first
Put the IS NULL branch before any comparison. Otherwise a missing amount silently ends up in the ELSE bucket and inflates it.
Conditional aggregation: count and sum only what matches
Placed inside an aggregate, CASE lets one grouped query report several slices side by side. COUNT ignores NULL, so a CASE without ELSE counts only matching rows. SUM with ELSE 0 adds only the matching amounts.
SELECT
country,
COUNT(*) AS all_orders,
COUNT(CASE WHEN status = 'shipped' THEN 1 END) AS shipped_orders,
SUM(CASE WHEN status = 'shipped' THEN amount ELSE 0 END) AS shipped_amount
FROM shop_orders
GROUP BY country
ORDER BY country;DuckDB also supports the standard FILTER clause, which expresses the same idea a little more directly.
SELECT
country,
COUNT(*) FILTER (WHERE status = 'shipped') AS shipped_orders
FROM shop_orders
GROUP BY country;Other places CASE fits
- SELECT: add labels, flags, and buckets.
- GROUP BY: group by a bucket to build a histogram-style summary.
- ORDER BY: define a custom sort order, such as pending orders first.
- Inside aggregates: count or sum only the rows that match.
SELECT order_id, status
FROM shop_orders
ORDER BY
CASE status
WHEN 'pending' THEN 1
WHEN 'shipped' THEN 2
ELSE 3
END,
order_id;Practice CASE WHEN in the browser
- Open the CASE WHEN chapter in the SQL for Files Learn SQL course.
- Load the shop_orders sample table with one click.
- Solve the four challenges: buckets, NULL handling, value mapping, and conditional aggregation.
- Apply the same patterns to your own CSV, JSON, or Parquet files.
Continue in the editor
Open SQL for Files to add your own CSV, JSON, or Parquet files and try these examples locally in your browser.
Related Learn SQL lessons
Related guides
SQL NULL Explained: How Missing Values Work and Why They Matter
NULL is SQL's way of saying a value is missing or unknown — and it behaves differently from almost every other value beginners expect.
SQL DISTINCT, GROUP BY, and HAVING: From Unique Values to Grouped Reports
DISTINCT cleans up repeated values, GROUP BY creates summaries per category, and HAVING filters those summaries after aggregation.
SQL Cheat Sheet for Data Analysis: Filter, Group, Join, Dates, and Ranking
One pattern per common analysis question, from previewing a new table to ranking rows inside groups, each linked to a lesson where you can practice it.