0
0
MySQLquery~5 mins

CASE WHEN expression in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the CASE WHEN expression in SQL?
The CASE WHEN expression lets you perform conditional logic inside SQL queries. It works like an if-else statement to return different values based on conditions.
Click to reveal answer
beginner
Write the basic syntax of a CASE WHEN expression.
The syntax is:<br>
CASE<br>  WHEN condition1 THEN result1<br>  WHEN condition2 THEN result2<br>  ELSE default_result<br>END
Click to reveal answer
beginner
Can CASE WHEN be used in the SELECT clause? Give an example.
Yes, it can. For example:<br>
SELECT name,<br>  CASE WHEN score >= 60 THEN 'Pass'<br>       ELSE 'Fail'<br>  END AS result<br>FROM students;
Click to reveal answer
intermediate
What happens if none of the WHEN conditions are true and there is no ELSE clause?
If no WHEN condition matches and there is no ELSE, the CASE expression returns NULL.
Click to reveal answer
intermediate
How can CASE WHEN improve readability compared to nested IF statements?
CASE WHEN organizes multiple conditions clearly in one block, making queries easier to read and maintain than many nested IF functions.
Click to reveal answer
What does the CASE WHEN expression return if a condition is true?
AThe result specified after THEN
BNULL
CThe ELSE result
DAn error
Which clause is optional in a CASE WHEN expression?
AWHEN
BEND
CTHEN
DELSE
Where can you use CASE WHEN in a SQL query?
AIn SELECT, WHERE, ORDER BY, and more
BOnly in SELECT clause
COnly in WHERE clause
DOnly in JOIN conditions
What is the correct way to end a CASE WHEN expression?
AFINISH
BEND
CSTOP
DDONE
If you want to assign grades based on scores using CASE WHEN, which is a valid condition?
AWHEN grade THEN score
BWHEN score = 'A' THEN 90
CWHEN score > 90 THEN 'A'
DWHEN THEN score > 90
Explain how the CASE WHEN expression works in SQL and give a simple example.
Think of it like an if-else chain inside a query.
You got /5 concepts.
    Describe a situation where using CASE WHEN improves a SQL query compared to multiple separate queries.
    Consider grading students or categorizing data in one query.
    You got /4 concepts.