SQL CASE expression
SQL's if/else inside a query. It is an expression that returns a value (usable in SELECT, WHERE, ORDER BY, GROUP BY and inside aggregates), not a statement.
- 類別
- 條件
- Returns
the common type of all THEN and ELSE results- In this category
- 3 of 43
語法
CASE WHEN cond THEN result [WHEN ...] [ELSE result] END
CASE value WHEN v THEN r ... END
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | 是 | Supported under this name. |
| PostgreSQL | 是 | Supported under this name. |
| MySQL | 是 | Supported under this name. |
| SQLite | 是 | Supported under this name. |
| Oracle | 是 | Supported under this name. |
範例
Any engine
SELECT name,
CASE WHEN amount >= 1000 THEN 'big'
WHEN amount >= 100 THEN 'mid'
ELSE 'small' END AS size_band
FROM orders;
Any engine
SELECT SUM(CASE WHEN status = 'paid' THEN amount END) AS paid_total FROM invoices;
Any engine
SELECT id, status,
CASE status WHEN 'new' THEN 1 WHEN 'paid' THEN 2 ELSE 3 END AS sort_key
FROM orders
ORDER BY sort_key;
小知識
- Two forms: searched (CASE WHEN x > 5 THEN ...) and simple (CASE status WHEN 'new' THEN ...). The simple form cannot use operators or IS NULL.
- Comparisons with NULL never match: WHEN col = NULL is never true. Write WHEN col IS NULL, or handle NULL in the ELSE.
- Branch order matters: the first true WHEN wins, and later branches are skipped. One documented exception on SQL Server: aggregates inside the branches are computed first, so CASE WHEN COUNT(*) = 0 THEN 0 ELSE 1 / MIN(x) END can still divide by zero.
- The result type is resolved across every branch. On SQL Server, CASE WHEN ok THEN 1 ELSE 'n/a' END is an int expression and fails when the 'n/a' branch is taken.
- In aggregates, CASE builds conditional sums: SUM(CASE WHEN status = 'paid' THEN amount END) works on every engine, where PIVOT syntax exists only on SQL Server and Oracle.
Questions about CASE
What is the difference between simple and searched CASE?
Simple CASE compares one expression for equality against values; searched CASE evaluates full boolean conditions. Only the searched form can express ranges and IS NULL.
Why doesn't WHEN col = NULL work in CASE?
NULL never equals anything in SQL, including another NULL. Use WHEN col IS NULL, or let the ELSE catch it.
Can CASE be used in WHERE and ORDER BY?
Yes. It is an expression anywhere a value goes. ORDER BY CASE status ... END is the standard custom-sort trick.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Conditional expressions: CASE, IIF, IF and DECODE Dialect comparison
- Custom sort order: CASE, FIELD and DECODE Dialect comparison
- Pivot rows to columns with CASE Snippet
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.