What is the main purpose of a CASE expression in SQL?
A CASE expression lets you perform conditional logic inside a SQL query, similar to an IF-THEN-ELSE in programming. It helps decide what value to show based on conditions.
Click to reveal answer
beginner
How does a CASE expression improve SQL queries?
It allows you to create new columns or change output values dynamically based on conditions without changing the data itself.
Click to reveal answer
intermediate
Why can't we just use WHERE or JOIN instead of CASE expressions?
WHERE filters rows and JOIN combines tables, but CASE changes values within rows based on conditions. It adds decision-making inside the output.
Click to reveal answer
beginner
Give a real-life example where CASE expressions are useful.
For example, showing 'Pass' or 'Fail' based on a student's score in a report, without changing the stored scores.
Click to reveal answer
intermediate
Can CASE expressions be used in SELECT, WHERE, and ORDER BY clauses?
Yes, CASE expressions can be used in SELECT to create conditional columns, in WHERE to filter based on conditions, and in ORDER BY to sort conditionally.
Click to reveal answer
What does a CASE expression do in SQL?
ADeletes rows from a table
BJoins two tables together
CPerforms conditional logic to return values based on conditions
DCreates a new table
✗ Incorrect
CASE expressions let you choose values based on conditions inside a query.
Which SQL clause can NOT use a CASE expression?
ASELECT
BWHERE
CORDER BY
DCREATE TABLE
✗ Incorrect
CASE expressions are used inside queries, not in CREATE TABLE statements.
Why are CASE expressions needed instead of just using WHERE?
AWHERE changes values, CASE filters rows
BWHERE filters rows, CASE changes values within rows
CWHERE creates tables, CASE deletes tables
DWHERE sorts data, CASE joins tables
✗ Incorrect
WHERE filters which rows appear; CASE changes what values appear in those rows.
Which of these is a good use of CASE expressions?
AShowing 'High', 'Medium', or 'Low' priority based on a numeric score
BAdding two tables together
CDeleting duplicate rows
DCreating a new database
✗ Incorrect
CASE expressions help convert numeric scores into readable categories.
Can CASE expressions be nested inside each other?
AYes, you can nest CASE expressions for complex conditions
BNo, nesting CASE expressions is not allowed
COnly in WHERE clause
DOnly in ORDER BY clause
✗ Incorrect
Nesting CASE expressions is allowed to handle multiple layers of conditions.
Explain why CASE expressions are important in SQL queries and how they differ from WHERE clauses.
Think about filtering rows versus changing what you see in the results.
You got /3 concepts.
Describe a real-life scenario where using a CASE expression would make a SQL report more useful.
Consider how you might show 'Pass' or 'Fail' based on scores.
You got /3 concepts.
Practice
(1/5)
1. Why do we need CASE expressions in SQL queries?
easy
A. To change the database schema
B. To create new tables in the database
C. To delete rows from a table
D. To return different values based on conditions within a query
Solution
Step 1: Understand the purpose of CASE expressions
CASE expressions allow SQL queries to return different results depending on conditions you specify.
Step 2: Compare with other SQL operations
Creating tables, deleting rows, or changing schema are done by other SQL commands, not CASE.
Final Answer:
To return different values based on conditions within a query -> Option D
Quick Check:
CASE = conditional output [OK]
Hint: CASE is for conditional results inside queries [OK]
Common Mistakes:
Thinking CASE creates or deletes tables
Confusing CASE with schema changes
Assuming CASE modifies data directly
2. Which of the following is the correct syntax to start a CASE expression in SQL?
easy
A. IF condition THEN result ELSE other END
B. CASE WHEN condition THEN result ELSE other END
C. SWITCH condition THEN result ELSE other END
D. SELECT CASE condition THEN result ELSE other
Solution
Step 1: Recall the standard CASE syntax
The correct syntax starts with CASE, followed by WHEN condition THEN result, optional ELSE, and ends with END.
Step 2: Identify incorrect options
IF and SWITCH are not valid SQL CASE keywords; SELECT CASE is incomplete syntax.
Final Answer:
CASE WHEN condition THEN result ELSE other END -> Option B
Quick Check:
CASE syntax = WHEN...THEN...END [OK]
Hint: CASE always uses WHEN...THEN...END structure [OK]
Common Mistakes:
Using IF instead of CASE in SQL
Omitting END keyword
Starting CASE with SELECT
3. Given the table Employees with column Salary, what will this query return?
SELECT Salary, CASE WHEN Salary > 5000 THEN 'High' ELSE 'Low' END AS SalaryLevel FROM Employees;
medium
A. Only salaries below 5000 with label 'Low'
B. Only salaries above 5000 with label 'High'
C. A list of salaries with labels 'High' for salaries above 5000 and 'Low' otherwise
D. An error because CASE cannot be used in SELECT
Solution
Step 1: Understand the CASE logic in SELECT
The CASE checks each Salary; if greater than 5000, label 'High', else 'Low'.
Step 2: Check what rows are returned
The query returns all rows with Salary and corresponding label, no rows are filtered out.
Final Answer:
A list of salaries with labels 'High' for salaries above 5000 and 'Low' otherwise -> Option C
Quick Check:
CASE labels salaries conditionally [OK]
Hint: CASE in SELECT labels rows, does not filter [OK]
Common Mistakes:
Thinking CASE filters rows like WHERE
Assuming only 'High' salaries show
Believing CASE causes syntax error in SELECT
4. Identify the error in this SQL snippet using CASE:
SELECT Name, CASE Salary > 5000 THEN 'High' ELSE 'Low' END AS SalaryLevel FROM Employees;
medium
A. Missing WHEN keyword after CASE
B. Missing END keyword at the end
C. Incorrect alias usage for SalaryLevel
D. CASE cannot be used in SELECT clause
Solution
Step 1: Review CASE syntax
CASE must be followed by WHEN condition THEN result; here WHEN is missing.
Step 2: Check other parts
END keyword is present; alias usage is correct; CASE is valid in SELECT.
Final Answer:
Missing WHEN keyword after CASE -> Option A
Quick Check:
CASE requires WHEN after CASE [OK]
Hint: CASE always needs WHEN after it [OK]
Common Mistakes:
Omitting WHEN keyword
Confusing END with alias
Thinking CASE can't be in SELECT
5. You want to assign a grade based on Score in a table: - Score ≥ 90: 'A' - Score ≥ 80 and < 90: 'B' - Score ≥ 70 and < 80: 'C' - Otherwise: 'F' Which CASE expression correctly implements this grading logic?
hard
A. CASE WHEN Score ≥ 90 THEN 'A' WHEN Score ≥ 80 THEN 'B' WHEN Score ≥ 70 THEN 'C' ELSE 'F' END
B. CASE WHEN Score < 70 THEN 'F' WHEN Score < 90 THEN 'C' WHEN Score < 80 THEN 'B' ELSE 'A' END
C. CASE WHEN Score ≥ 70 THEN 'C' WHEN Score ≥ 80 THEN 'B' WHEN Score ≥ 90 THEN 'A' ELSE 'F' END
D. CASE Score WHEN ≥ 90 THEN 'A' WHEN ≥ 80 THEN 'B' WHEN ≥ 70 THEN 'C' ELSE 'F' END
Solution
Step 1: Understand the grading order
Grades must be checked from highest to lowest score to assign correct grade.
Step 2: Analyze each option
CASE WHEN Score ≥ 90 THEN 'A' WHEN Score ≥ 80 THEN 'B' WHEN Score ≥ 70 THEN 'C' ELSE 'F' END checks from highest to lowest correctly; CASE WHEN Score < 70 THEN 'F' WHEN Score < 90 THEN 'C' WHEN Score < 80 THEN 'B' ELSE 'A' END incorrectly assigns scores 70-89 to 'C'; CASE WHEN Score ≥ 70 THEN 'C' WHEN Score ≥ 80 THEN 'B' WHEN Score ≥ 90 THEN 'A' ELSE 'F' END checks in wrong order; CASE Score WHEN ≥ 90 THEN 'A' WHEN ≥ 80 THEN 'B' WHEN ≥ 70 THEN 'C' ELSE 'F' END uses invalid CASE syntax.
Final Answer:
CASE WHEN Score ≥ 90 THEN 'A' WHEN Score ≥ 80 THEN 'B' WHEN Score ≥ 70 THEN 'C' ELSE 'F' END -> Option A
Quick Check:
Order conditions high to low in CASE [OK]
Hint: Check conditions from highest to lowest in CASE [OK]
Common Mistakes:
Checking conditions in wrong order
Using CASE with invalid syntax
Assuming CASE compares values directly without WHEN
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.