CASE turns values into labels
CASE is SQL's way of saying if this, then that. Each WHEN holds a test and its THEN holds the label to use when that test passes. SQL reads the WHEN tests from the top down and stops at the first one that is true. ELSE gives the label for rows that passed no test, and END closes the CASE.
A tiny example
In plain English: Show me each book's title, labeled short when it is under 100 pages and long otherwise.
SELECT title, CASE WHEN pages < 100 THEN 'short' ELSE 'long' END AS length
FROM books;What comes back
| title | length |
|---|---|
| The Turnip | short |
| Dune | long |
| Wool | long |
The Turnip is under 100 pages, so it's 'short'; Dune and Wool failed that test, so ELSE labeled them 'long'.
Related SQL lessons
Ready for a break?
Take on today's cozy puzzle. A fresh, bite-sized query waits every morning — no timers, no pressure.
play today's sip