Challenge - 5 Problems
CASE Expression Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of CASE expression with simple conditions
What is the output of the following SQL query?
SELECT
product_id,
CASE
WHEN price < 50 THEN 'Cheap'
WHEN price BETWEEN 50 AND 100 THEN 'Moderate'
ELSE 'Expensive'
END AS price_category
FROM products
ORDER BY product_id
LIMIT 3;PostgreSQL
CREATE TEMP TABLE products (product_id INT, price INT); INSERT INTO products VALUES (1, 30), (2, 75), (3, 150); SELECT product_id, CASE WHEN price < 50 THEN 'Cheap' WHEN price BETWEEN 50 AND 100 THEN 'Moderate' ELSE 'Expensive' END AS price_category FROM products ORDER BY product_id LIMIT 3;
Attempts:
2 left
💡 Hint
Think about the price ranges and how CASE matches conditions in order.
✗ Incorrect
The CASE expression checks conditions in order. For product_id 1, price 30 is less than 50, so 'Cheap'. For product_id 2, price 75 is between 50 and 100, so 'Moderate'. For product_id 3, price 150 is greater than 100, so 'Expensive'.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in CASE expression
Which option contains a syntax error in the CASE expression?
SELECT
employee_id,
CASE salary
WHEN > 50000 THEN 'High'
WHEN <= 50000 THEN 'Low'
END AS salary_level
FROM employees;Attempts:
2 left
💡 Hint
Check how the simple CASE expression compares values.
✗ Incorrect
In option B, the syntax 'WHEN > 50000' is invalid because simple CASE compares the expression to a value, not a condition. Conditions require the searched CASE syntax as in option B.
❓ query_result
advanced2:30remaining
Output of nested CASE expressions
What is the output of this query?
SELECT
order_id,
CASE
WHEN status = 'shipped' THEN
CASE
WHEN shipped_date <= order_date + INTERVAL '3 days' THEN 'On time'
ELSE 'Late'
END
ELSE 'Pending'
END AS delivery_status
FROM orders
ORDER BY order_id
LIMIT 3;PostgreSQL
CREATE TEMP TABLE orders (order_id INT, status TEXT, order_date DATE, shipped_date DATE); INSERT INTO orders VALUES (1, 'shipped', '2024-01-01', '2024-01-03'), (2, 'shipped', '2024-01-01', '2024-01-06'), (3, 'processing', '2024-01-01', NULL); SELECT order_id, CASE WHEN status = 'shipped' THEN CASE WHEN shipped_date <= order_date + INTERVAL '3 days' THEN 'On time' ELSE 'Late' END ELSE 'Pending' END AS delivery_status FROM orders ORDER BY order_id LIMIT 3;
Attempts:
2 left
💡 Hint
Check the nested CASE logic and date comparisons carefully.
✗ Incorrect
Order 1 shipped within 2 days (<= 3 days), so 'On time'. Order 2 shipped after 5 days (> 3 days), so 'Late'. Order 3 is not shipped, so 'Pending'.
🔧 Debug
advanced2:30remaining
Find the logical error in CASE expression
The following query is intended to classify ages into groups but produces incorrect results. What is the logical error?
SELECT
person_id,
CASE
WHEN age > 18 THEN 'Adult'
WHEN age > 12 THEN 'Teen'
ELSE 'Child'
END AS age_group
FROM people;Attempts:
2 left
💡 Hint
Think about how CASE evaluates conditions in order.
✗ Incorrect
Since WHEN age > 18 comes first, ages above 18 are 'Adult'. But ages between 13 and 18 also satisfy age > 12, but that condition is never reached because the first WHEN is checked first. The order should be reversed to check smaller ranges first.
❓ optimization
expert3:00remaining
Optimize CASE expression for performance
Which option is the most efficient way to write this CASE expression for a large table?
SELECT
user_id,
CASE
WHEN score >= 90 THEN 'A'
WHEN score >= 80 THEN 'B'
WHEN score >= 70 THEN 'C'
WHEN score >= 60 THEN 'D'
ELSE 'F'
END AS grade
FROM exam_results;Attempts:
2 left
💡 Hint
Consider how indexing and joins can improve performance over many CASE checks.
✗ Incorrect
Using a derived table with score ranges and joining on score allows the database to use indexes and reduces repeated condition checks, improving performance on large datasets compared to multiple CASE WHEN checks.