What is the purpose of the Simple CASE syntax in SQL?
The Simple CASE syntax helps you choose a value based on matching a single expression to different possible values, similar to a multiple-choice decision.
Click to reveal answer
beginner
Write the basic structure of a Simple CASE expression in SQL.
CASE expression WHEN value1 THEN result1 WHEN value2 THEN result2 ... ELSE default_result END
Click to reveal answer
intermediate
What happens if none of the WHEN conditions match and there is no ELSE clause in a Simple CASE expression?
The CASE expression returns NULL if no WHEN condition matches and there is no ELSE clause.
Click to reveal answer
intermediate
How is Simple CASE different from Searched CASE in SQL?
Simple CASE compares one expression to multiple values. Searched CASE evaluates multiple boolean conditions independently.
Click to reveal answer
beginner
Example: What will this SQL return?
SELECT CASE grade WHEN 'A' THEN 'Excellent' WHEN 'B' THEN 'Good' ELSE 'Other' END AS performance FROM students;
It returns 'Excellent' if grade is 'A', 'Good' if grade is 'B', and 'Other' for any other grade.
Click to reveal answer
What keyword starts a Simple CASE expression in SQL?
ASELECT
BIF
CSWITCH
DCASE
✗ Incorrect
The Simple CASE expression always starts with the keyword CASE.
In Simple CASE, what does the expression after CASE represent?
AThe value to compare against WHEN values
BThe result to return
CA boolean condition
DThe default value
✗ Incorrect
The expression after CASE is compared to each WHEN value to find a match.
What does the ELSE clause do in a Simple CASE expression?
AIs required for CASE to work
BSpecifies the default result if no WHEN matches
CStarts the CASE expression
DEnds the CASE expression
✗ Incorrect
ELSE provides a default result when no WHEN condition matches.
If no WHEN matches and no ELSE is provided, what is returned?
ANULL
B0
CError
DEmpty string
✗ Incorrect
Without ELSE, CASE returns NULL if no match is found.
Which of these is a valid Simple CASE expression?
ACASE WHEN score > 90 THEN 'A' ELSE 'B' END
BIF score = 90 THEN 'A' ELSE 'B'
CCASE score WHEN 90 THEN 'A' WHEN 80 THEN 'B' ELSE 'C' END
DSWITCH(score) WHEN 90 THEN 'A' ELSE 'B'
✗ Incorrect
Option C uses Simple CASE syntax correctly.
Explain how the Simple CASE syntax works in SQL and give a real-life example.
Think about choosing an output based on one value matching several options.
You got /5 concepts.
Describe the difference between Simple CASE and Searched CASE expressions in SQL.
Focus on how conditions are checked in each type.
You got /3 concepts.
Practice
(1/5)
1. What does the Simple CASE expression do in SQL?
easy
A. It loops through rows and updates values conditionally.
B. It compares one expression to multiple values and returns a result for the first match.
C. It creates a new table based on conditions.
D. It deletes rows that match a condition.
Solution
Step 1: Understand the purpose of Simple CASE
Simple CASE compares one expression against multiple possible values.
Step 2: Identify the result behavior
It returns the result for the first matching value it finds.
Final Answer:
It compares one expression to multiple values and returns a result for the first match. -> Option B
Quick Check:
Simple CASE = compare expression to values [OK]
Hint: Simple CASE matches one value to many options [OK]
Common Mistakes:
Thinking it loops through rows
Confusing with IF statements
Assuming it creates or deletes tables
2. Which of the following is the correct syntax for a Simple CASE expression in SQL?
easy
A. CASE expression WHEN value THEN result ELSE default END
B. IF expression THEN result ELSE default END
C. CASE WHEN expression THEN result END
D. SWITCH expression WHEN value THEN result END
Solution
Step 1: Recall Simple CASE syntax structure
The correct syntax is: CASE expression WHEN value THEN result ELSE default END.
Step 2: Compare options
CASE expression WHEN value THEN result ELSE default END matches this syntax exactly, others use incorrect keywords or structure.
Final Answer:
CASE expression WHEN value THEN result ELSE default END -> Option A
Quick Check:
Simple CASE syntax = CASE expr WHEN val THEN res ELSE def END [OK]
Hint: Simple CASE uses 'CASE expr WHEN val THEN res' format [OK]
Common Mistakes:
Using WHEN without expression after CASE
Confusing with IF or SWITCH syntax
Omitting END keyword
3. Given the table Employees with column DepartmentID, what will this query return?
SELECT EmployeeName, CASE DepartmentID WHEN 1 THEN 'HR' WHEN 2 THEN 'Sales' ELSE 'Other' END AS DeptName FROM Employees;
medium
A. Employee names with department names 'HR', 'Sales', or 'Other' based on DepartmentID.
B. Only employees from HR and Sales departments.
C. An error because ELSE is missing.
D. Employee names with numeric DepartmentID values.
Solution
Step 1: Understand the CASE expression in the query
The CASE checks DepartmentID: 1 returns 'HR', 2 returns 'Sales', others return 'Other'.
Step 2: Analyze the SELECT output
The query returns EmployeeName and DeptName with the mapped department names or 'Other'.
Final Answer:
Employee names with department names 'HR', 'Sales', or 'Other' based on DepartmentID. -> Option A
Quick Check:
CASE maps DepartmentID to names [OK]
Hint: CASE returns first matching value or ELSE default [OK]
Common Mistakes:
Thinking ELSE is required (it's optional)
Assuming only HR and Sales appear
Expecting numeric DepartmentID instead of names
4. Identify the error in this Simple CASE query:
SELECT ProductName, CASE ProductCategory WHEN 'A' THEN 'Category A' WHEN 'B' 'Category B' ELSE 'Other' END AS CategoryName FROM Products;
medium
A. CASE expression should be inside parentheses.
B. Missing END keyword at the end.
C. Incorrect use of ELSE keyword.
D. Missing THEN keyword after WHEN 'B'.
Solution
Step 1: Review each WHEN clause syntax
WHEN clauses must have THEN keyword before the result. The second WHEN is missing THEN.
Step 2: Check other parts of the query
END keyword is present, ELSE is correctly used, parentheses are not required around CASE expression.
Final Answer:
Missing THEN keyword after WHEN 'B'. -> Option D
Quick Check:
WHEN must be followed by THEN [OK]
Hint: Each WHEN must be followed by THEN [OK]
Common Mistakes:
Forgetting THEN after WHEN
Thinking END is optional
Adding unnecessary parentheses
5. You want to categorize sales amounts into 'Low', 'Medium', and 'High' using Simple CASE. Which query correctly assigns: - 'Low' for 0 - 'Medium' for 1 - 'High' for 2 or more (default)?
SELECT SaleID, CASE SaleAmount WHEN 0 THEN 'Low' WHEN 1 THEN 'Medium' ELSE 'High' END AS SaleCategory FROM Sales;
hard
A. Fails because ELSE cannot be used for multiple values.
B. Incorrect because Simple CASE cannot handle ranges like 2 or more.
C. Correctly categorizes sales as described.
D. Syntax error due to missing WHEN for 2.
Solution
Step 1: Understand Simple CASE limitations
Simple CASE matches exact values; ELSE handles all other cases.
Step 2: Analyze the query logic
WHEN 0 returns 'Low', WHEN 1 returns 'Medium', ELSE covers 2 or more as 'High'.
Final Answer:
Correctly categorizes sales as described. -> Option C
Quick Check:
ELSE covers all unmatched values [OK]
Hint: Use ELSE for all other values in Simple CASE [OK]
Common Mistakes:
Thinking ELSE can't cover multiple values
Trying to use ranges in Simple CASE
Expecting WHEN for every possible value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.