Bird
Raised Fist0
SQLquery~20 mins

CASE in SELECT for computed columns in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CASE Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of CASE in SELECT for grade classification
Given the table Students with columns id, name, and score, what is the output of this query?
SELECT name,
CASE
WHEN score >= 90 THEN 'A'
WHEN score >= 80 THEN 'B'
WHEN score >= 70 THEN 'C'
ELSE 'F'
END AS grade
FROM Students
ORDER BY id;
SQL
CREATE TABLE Students (id INT, name VARCHAR(20), score INT);
INSERT INTO Students VALUES (1, 'Alice', 85), (2, 'Bob', 92), (3, 'Charlie', 68);
A[{"name": "Alice", "grade": "B"}, {"name": "Bob", "grade": "A"}, {"name": "Charlie", "grade": "F"}]
B[{"name": "Alice", "grade": "C"}, {"name": "Bob", "grade": "B"}, {"name": "Charlie", "grade": "F"}]
C[{"name": "Alice", "grade": "B"}, {"name": "Bob", "grade": "A"}, {"name": "Charlie", "grade": "C"}]
D[{"name": "Alice", "grade": "A"}, {"name": "Bob", "grade": "A"}, {"name": "Charlie", "grade": "F"}]
Attempts:
2 left
💡 Hint
Remember the CASE checks conditions in order and stops at the first true condition.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in CASE usage
Which option contains a syntax error in the CASE expression used in a SELECT statement?
SQL
SELECT id, name, CASE score WHEN score >= 90 THEN 'A' ELSE 'F' END AS grade FROM Students;
ASELECT id, name, CASE score WHEN score >= 90 THEN 'A' ELSE 'F' END AS grade FROM Students;
BSELECT id, name, CASE WHEN score >= 90 THEN 'A' ELSE 'F' END AS grade FROM Students;
CSELECT id, name, CASE WHEN score >= 90 THEN 'A' ELSE 'F' END grade FROM Students;
D;stnedutS MORF edarg SA DNE 'F' ESLE 'A' NEHT 09 => erocs NEHW ESAC ,eman ,di TCELES
Attempts:
2 left
💡 Hint
Check the syntax of CASE with and without an expression after CASE keyword.
❓ query_result
advanced
2:30remaining
Result of nested CASE expressions in SELECT
Consider the table Orders with columns order_id, amount, and status. What is the output of this query?
SELECT order_id,
CASE
WHEN status = 'shipped' THEN 'Completed'
WHEN status = 'pending' THEN CASE WHEN amount > 100 THEN 'High Pending' ELSE 'Low Pending' END
ELSE 'Unknown'
END AS order_status
FROM Orders
ORDER BY order_id;
SQL
CREATE TABLE Orders (order_id INT, amount INT, status VARCHAR(10));
INSERT INTO Orders VALUES (1, 150, 'pending'), (2, 50, 'pending'), (3, 200, 'shipped'), (4, 30, 'cancelled');
A[{"order_id":1,"order_status":"Low Pending"},{"order_id":2,"order_status":"High Pending"},{"order_id":3,"order_status":"Completed"},{"order_id":4,"order_status":"Unknown"}]
B[{"order_id":1,"order_status":"High Pending"},{"order_id":2,"order_status":"Low Pending"},{"order_id":3,"order_status":"Unknown"},{"order_id":4,"order_status":"Unknown"}]
C[{"order_id":1,"order_status":"Completed"},{"order_id":2,"order_status":"Low Pending"},{"order_id":3,"order_status":"Completed"},{"order_id":4,"order_status":"Unknown"}]
D[{"order_id":1,"order_status":"High Pending"},{"order_id":2,"order_status":"Low Pending"},{"order_id":3,"order_status":"Completed"},{"order_id":4,"order_status":"Unknown"}]
Attempts:
2 left
💡 Hint
Look carefully at the nested CASE inside the WHEN status = 'pending' condition.
❓ optimization
advanced
1:30remaining
Optimizing CASE expressions for performance
Which option is the most efficient way to write a CASE expression that assigns 'Adult' if age >= 18, 'Teen' if age between 13 and 17, and 'Child' otherwise, assuming the age column is indexed?
ACASE WHEN age BETWEEN 13 AND 17 THEN 'Teen' WHEN age >= 18 THEN 'Adult' ELSE 'Child' END
BCASE age WHEN 18 THEN 'Adult' WHEN 13 THEN 'Teen' ELSE 'Child' END
CCASE WHEN age >= 18 THEN 'Adult' WHEN age >= 13 THEN 'Teen' ELSE 'Child' END
DCASE WHEN age > 18 THEN 'Adult' WHEN age >= 13 THEN 'Teen' ELSE 'Child' END
Attempts:
2 left
💡 Hint
Think about the order of conditions and how SQL evaluates CASE WHEN clauses.
🔧 Debug
expert
3:00remaining
Debug the unexpected output from CASE in SELECT
A developer wrote this query:
SELECT id,
CASE WHEN score > 90 THEN 'Excellent'
WHEN score > 80 THEN 'Good'
WHEN score > 70 THEN 'Average'
ELSE 'Poor'
END AS rating
FROM Results;

But the output shows 'Poor' for a score of 85. What is the most likely cause?
AThe ELSE clause is overriding all previous WHEN conditions.
BThe score column is stored as text, so comparisons are lexicographical, not numeric.
CThe query is missing an ORDER BY clause, causing wrong results.
DThe CASE expression syntax is invalid and defaults to ELSE for all rows.
Attempts:
2 left
💡 Hint
Think about how data types affect comparison operators in SQL.

Practice

(1/5)
1. What does the CASE statement do in a SQL SELECT query?
easy
A. Creates a new column with values based on conditions
B. Deletes rows that do not meet a condition
C. Joins two tables together
D. Sorts the results in ascending order

Solution

  1. Step 1: Understand the purpose of CASE in SELECT

    The CASE statement is used to create new columns by checking conditions and assigning values accordingly.
  2. Step 2: Compare with other SQL operations

    Deleting rows is done with DELETE, joining tables with JOIN, and sorting with ORDER BY, not CASE.
  3. Final Answer:

    Creates a new column with values based on conditions -> Option A
  4. Quick Check:

    CASE = computed column based on conditions [OK]
Hint: CASE creates conditional columns in SELECT queries [OK]
Common Mistakes:
  • Thinking CASE deletes rows
  • Confusing CASE with JOIN
  • Assuming CASE sorts data
2. Which of the following is the correct syntax to use CASE in a SELECT statement?
easy
A. SELECT CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END result FROM students
B. SELECT CASE score > 50 THEN 'Pass' ELSE 'Fail' END AS result FROM students;
C. SELECT CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' AS result FROM students;
D. SELECT CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END AS result FROM students;

Solution

  1. Step 1: Review correct CASE syntax

    The correct syntax requires WHEN conditions, THEN results, ELSE for default, and END to close, with AS to name the column.
  2. Step 2: Identify errors in other options

    A omits AS before result (END result); B misses WHEN after CASE; C misses END before AS result.
  3. Final Answer:

    SELECT CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END AS result FROM students; -> Option D
  4. Quick Check:

    CASE syntax = WHEN...THEN...ELSE...END AS alias [OK]
Hint: Always use WHEN, THEN, ELSE, END, and AS alias [OK]
Common Mistakes:
  • Omitting WHEN keyword
  • Forgetting END keyword
  • Not using AS for alias
3. Given the table employees with column salary, what will this query return?
SELECT name, CASE WHEN salary >= 5000 THEN 'High' ELSE 'Low' END AS salary_level FROM employees;
medium
A. An error because CASE cannot be used in SELECT
B. Only employees with salary ≥ 5000
C. A list of employee names with salary_level showing 'High' if salary ≥ 5000, else 'Low'
D. A list of employee names with salary values doubled

Solution

  1. Step 1: Understand the CASE condition

    The CASE checks if salary is 5000 or more, then assigns 'High', otherwise 'Low'.
  2. Step 2: Analyze the SELECT output

    The query returns all employees with their name and a new column salary_level showing 'High' or 'Low' accordingly.
  3. Final Answer:

    A list of employee names with salary_level showing 'High' if salary ≥ 5000, else 'Low' -> Option C
  4. Quick Check:

    CASE creates conditional labels per row [OK]
Hint: CASE labels rows based on condition, does not filter [OK]
Common Mistakes:
  • Thinking CASE filters rows
  • Expecting numeric salary changes
  • Assuming syntax error
4. Identify the error in this SQL query:
SELECT name, CASE salary > 10000 THEN 'Rich' ELSE 'Average' END AS status FROM users;
medium
A. Missing WHEN keyword after CASE
B. Missing END keyword
C. Missing ELSE keyword
D. Missing AS keyword for alias

Solution

  1. Step 1: Check CASE syntax

    The CASE statement must have WHEN before the condition. Here, WHEN is missing.
  2. Step 2: Verify other parts

    END and ELSE are present, AS is used correctly, so no errors there.
  3. Final Answer:

    Missing WHEN keyword after CASE -> Option A
  4. Quick Check:

    CASE requires WHEN before condition [OK]
Hint: Always write WHEN after CASE before condition [OK]
Common Mistakes:
  • Omitting WHEN keyword
  • Confusing ELSE with END
  • Forgetting AS alias
5. You want to classify products by price into 'Cheap', 'Moderate', and 'Expensive' using CASE in SELECT. Which query correctly implements this?
hard
A. SELECT product_name, CASE price < 50 THEN 'Cheap' WHEN price BETWEEN 50 AND 100 THEN 'Moderate' ELSE 'Expensive' END price_category FROM products;
B. SELECT product_name, CASE WHEN price < 50 THEN 'Cheap' WHEN price BETWEEN 50 AND 100 THEN 'Moderate' ELSE 'Expensive' END AS price_category FROM products;
C. SELECT product_name, CASE WHEN price < 50 THEN 'Cheap' ELSE WHEN price BETWEEN 50 AND 100 THEN 'Moderate' ELSE 'Expensive' END AS price_category FROM products;
D. SELECT product_name, CASE WHEN price < 50 THEN 'Cheap' WHEN price > 100 THEN 'Expensive' ELSE 'Expensive' END AS price_category FROM products;

Solution

  1. Step 1: Check correct CASE syntax with multiple WHEN

    SELECT product_name, CASE WHEN price < 50 THEN 'Cheap' WHEN price BETWEEN 50 AND 100 THEN 'Moderate' ELSE 'Expensive' END AS price_category FROM products; correctly uses multiple WHEN conditions with THEN, ELSE, and END, and aliases the column.
  2. Step 2: Analyze logic correctness

    SELECT product_name, CASE WHEN price < 50 THEN 'Cheap' WHEN price BETWEEN 50 AND 100 THEN 'Moderate' ELSE 'Expensive' END AS price_category FROM products; classifies prices below 50 as 'Cheap', between 50 and 100 as 'Moderate', else 'Expensive', which matches the requirement.
  3. Final Answer:

    SELECT product_name, CASE WHEN price < 50 THEN 'Cheap' WHEN price BETWEEN 50 AND 100 THEN 'Moderate' ELSE 'Expensive' END AS price_category FROM products; -> Option B
  4. Quick Check:

    Multiple WHEN conditions with ELSE classify ranges [OK]
Hint: Use multiple WHEN for ranges, ELSE for default [OK]
Common Mistakes:
  • Omitting WHEN keywords
  • Misplacing ELSE before WHEN
  • Not using AS for alias
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard