0
0
PostgreSQLquery~20 mins

CASE expression in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CASE Expression Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A[{"product_id":1,"price_category":"Cheap"},{"product_id":2,"price_category":"Moderate"},{"product_id":3,"price_category":"Expensive"}]
B[{"product_id":1,"price_category":"Expensive"},{"product_id":2,"price_category":"Cheap"},{"product_id":3,"price_category":"Moderate"}]
C[{"product_id":1,"price_category":"Moderate"},{"product_id":2,"price_category":"Moderate"},{"product_id":3,"price_category":"Cheap"}]
D[{"product_id":1,"price_category":null},{"product_id":2,"price_category":null},{"product_id":3,"price_category":null}]
Attempts:
2 left
💡 Hint
Think about the price ranges and how CASE matches conditions in order.
📝 Syntax
intermediate
2: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;
ACASE WHEN salary > 50000 THEN 'High' WHEN salary <= 50000 THEN 'Low' END
BCASE salary WHEN > 50000 THEN 'High' WHEN <= 50000 THEN 'Low' END
CCASE salary WHEN 50000 THEN 'Equal' ELSE 'Other' END
DCASE WHEN salary = 50000 THEN 'Equal' ELSE 'Other' END
Attempts:
2 left
💡 Hint
Check how the simple CASE expression compares values.
query_result
advanced
2: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;
A[{"order_id":1,"delivery_status":"Pending"},{"order_id":2,"delivery_status":"Pending"},{"order_id":3,"delivery_status":"Pending"}]
B[{"order_id":1,"delivery_status":null},{"order_id":2,"delivery_status":null},{"order_id":3,"delivery_status":null}]
C[{"order_id":1,"delivery_status":"Late"},{"order_id":2,"delivery_status":"On time"},{"order_id":3,"delivery_status":"Pending"}]
D[{"order_id":1,"delivery_status":"On time"},{"order_id":2,"delivery_status":"Late"},{"order_id":3,"delivery_status":"Pending"}]
Attempts:
2 left
💡 Hint
Check the nested CASE logic and date comparisons carefully.
🔧 Debug
advanced
2: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;
AThe order of WHEN conditions causes all ages above 12 to be classified as 'Adult'.
BThe ELSE clause should be first to catch all ages.
CThe CASE expression is missing an END keyword.
DThe age comparisons should use BETWEEN instead of >.
Attempts:
2 left
💡 Hint
Think about how CASE evaluates conditions in order.
optimization
expert
3: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;
AUse nested CASE expressions for each grade level.
BUse multiple OR conditions in a single WHEN clause to reduce checks.
CUse a derived table with ranges and join on score to get grade.
DUse a CASE expression with ordered WHEN conditions as shown.
Attempts:
2 left
💡 Hint
Consider how indexing and joins can improve performance over many CASE checks.