Tables and Select
Case Labels
Tagging Rows by Priority
A CASE expression returns a different value per row based on conditions. It is the SQL way to label or bucket data.
Program
Play the script to label each task as high, medium, or low priority.
case_labels.sql
CREATE TABLE tasks (name TEXT, priority INTEGER);
INSERT INTO tasks VALUES ('deploy', 5), ('docs', 2), ('cleanup', 0);
SELECT name, priority, CASE WHEN priority >= 3 THEN 'high' WHEN priority >= 1 THEN 'medium' ELSE 'low' END AS label FROM tasks ORDER BY priority DESC;
CASE
`CASE WHEN ... THEN ...` returns a value chosen per row.
first match wins
Conditions are tested top to bottom; the first matching `WHEN` decides the label.
ELSE
`ELSE 'low'` is the default when no `WHEN` matches.