Technical skill practice
SQL CASE: Classify Rows With an Explicit Boundary Policy
CASE produces a result from conditional branches. In a searched CASE expression, the order of the conditions matters when more than one could match. Include an explicit missing-value branch when missing measurements need a separate label.
The practice task
Classify fictional service waits as unknown, urgent, review or routine. A 60-minute wait should be urgent and a 30-minute wait should be review. Keep those boundaries visible in the result.
The data below is fictional. Run the example in a new SQLite database. It was checked with SQLite 3.51.0; other databases may use different syntax, functions or type rules.
Complete worked query
Create the small fixture and run the query together. The IDs, boundary values and missing entries make the result small enough to check by hand.
CREATE TABLE waits(id INTEGER, minutes INTEGER);
INSERT INTO waits VALUES (1,NULL),(2,15),(3,30),(4,60),(5,75);
SELECT id, CASE
WHEN minutes IS NULL THEN 'unknown'
WHEN minutes >= 60 THEN 'urgent'
WHEN minutes >= 30 THEN 'review'
ELSE 'routine' END AS band
FROM waits ORDER BY id;Checked result
This is the result of the exact example above. NULL marks a missing database value rather than the text string "NULL".
Result rows
id | band
1 | unknown
2 | routine
3 | review
4 | urgent
5 | urgent
Read the result
The most restrictive measured condition appears first. A 75-minute wait meets both numeric thresholds, but its first qualifying branch is urgent. Placing the 30-minute branch first would classify it as review instead.
The missing-value branch makes an incomplete measurement distinguishable from a routine wait. ELSE is an explicit policy for the remaining measured values in this exercise. If negative waits are possible in the source, add validation rather than silently accepting them as routine.
CASE returns a value; it is not the same as a procedural IF statement that runs a sequence of commands. Use the expression in a result column, aggregate or sort condition where a value is required. Keep the categories consistent so another reader can reconstruct their boundaries.
Mistakes to check
Use these checks when adapting the example to a different dataset.
- Put overlapping thresholds in the intended priority order.
- Test values exactly on the boundary.
- Choose compatible result types for the branches.
Questions, answered
Can I run this example as written?
Yes, in a new SQLite database using the schema and values shown. The result above was checked with SQLite 3.51.0. Keep it separate from an existing database.
How would you verify that a revised threshold policy has not changed every category?
Prepare a small fixture covering below, at and above each threshold plus NULL. Compare the expected label for each ID before using the expression in a larger report.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.