0
0
MySQLquery~20 mins

CASE WHEN expression in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CASE WHEN Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of CASE WHEN with numeric ranges
Consider the following SQL query:

SELECT id, score,
CASE
WHEN score >= 90 THEN 'A'
WHEN score >= 80 THEN 'B'
WHEN score >= 70 THEN 'C'
ELSE 'F'
END AS grade
FROM students;

What will be the output for a student with score = 85?
MySQL
CREATE TABLE students (id INT, score INT);
INSERT INTO students VALUES (1, 85);
A{"id":1,"score":85,"grade":"C"}
B{"id":1,"score":85,"grade":"A"}
C{"id":1,"score":85,"grade":"B"}
D{"id":1,"score":85,"grade":"F"}
Attempts:
2 left
💡 Hint
Check the conditions in order and see which range 85 fits into.
query_result
intermediate
2:00remaining
CASE WHEN with NULL values
Given the table:
CREATE TABLE employees (id INT, department VARCHAR(20));
INSERT INTO employees VALUES (1, 'Sales'), (2, NULL);

What is the output of this query?
SELECT id,
CASE WHEN department IS NULL THEN 'No Dept' ELSE department END AS dept_status
FROM employees ORDER BY id;
MySQL
CREATE TABLE employees (id INT, department VARCHAR(20));
INSERT INTO employees VALUES (1, 'Sales'), (2, NULL);
A[{"id":1,"dept_status":null},{"id":2,"dept_status":"No Dept"}]
B[{"id":1,"dept_status":"Sales"},{"id":2,"dept_status":null}]
C[{"id":1,"dept_status":"No Dept"},{"id":2,"dept_status":"No Dept"}]
D[{"id":1,"dept_status":"Sales"},{"id":2,"dept_status":"No Dept"}]
Attempts:
2 left
💡 Hint
Remember how CASE WHEN handles NULL with IS NULL condition.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in CASE WHEN
Which of the following CASE WHEN expressions is syntactically correct in MySQL?
ACASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END
BCASE score WHEN > 50 THEN 'Pass' ELSE 'Fail' END
CCASE WHEN score > 50 THEN 'Pass' ELSE 'Fail'
DCASE score WHEN score > 50 THEN 'Pass' ELSE 'Fail' END
Attempts:
2 left
💡 Hint
Check the syntax for CASE WHEN with and without expression.
optimization
advanced
2:00remaining
Optimizing CASE WHEN for multiple conditions
You want to assign categories based on age:
SELECT age,
CASE
WHEN age < 13 THEN 'Child'
WHEN age < 20 THEN 'Teen'
WHEN age < 65 THEN 'Adult'
ELSE 'Senior'
END AS category
FROM people;

Which of the following changes improves readability and maintains correctness?
AKeep original CASE WHEN with ordered conditions from smallest to largest
BUse nested CASE WHEN inside ELSE for each range
CUse CASE age WHEN &lt; 13 THEN 'Child' WHEN &lt; 20 THEN 'Teen' WHEN &lt; 65 THEN 'Adult' ELSE 'Senior' END
DUse multiple separate IF statements instead of CASE WHEN
Attempts:
2 left
💡 Hint
Think about how CASE WHEN evaluates conditions in order.
🧠 Conceptual
expert
2:00remaining
Understanding CASE WHEN evaluation order and NULL handling
Given the query:
SELECT value,
CASE
WHEN value > 10 THEN 'High'
WHEN value <= 10 THEN 'Low'
ELSE 'Unknown'
END AS category
FROM data;

What will be the category for a row where value is NULL?
A'Low'
B'Unknown'
C'High'
DNULL
Attempts:
2 left
💡 Hint
Remember how comparisons with NULL behave in SQL CASE WHEN.