How to Use CASE Statements in MySQL
Last updated July 25, 2026 · By the SaturnSQL team
Use CASE WHEN condition THEN value ... ELSE value END inside SELECT to derive columns, and inside ORDER BY for custom sort orders. The compact CASE expr WHEN value form compares one expression against literals.
SELECT id,
amount,
CASE
WHEN amount >= 100 THEN 'large'
WHEN amount >= 20 THEN 'medium'
ELSE 'small'
END AS size
FROM orders;Custom sort order
SELECT * FROM orders
ORDER BY CASE status
WHEN 'pending' THEN 1
WHEN 'shipped' THEN 2
WHEN 'delivered' THEN 3
ELSE 4
END, created_at DESC;Without an ELSE, non-matching rows get NULL. CASE is an expression, so it also works in WHERE, GROUP BY, and aggregate arguments like SUM(CASE WHEN ... THEN 1 ELSE 0 END).
Run this in SaturnSQL
SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.
Try it free