What is the purpose of the Searched CASE syntax in SQL?
It allows you to perform conditional logic in SQL queries by evaluating multiple conditions and returning a result for the first true condition.
Click to reveal answer
beginner
Write the basic structure of a Searched CASE expression.
CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... ELSE default_result END
Click to reveal answer
intermediate
How does the Searched CASE differ from the Simple CASE syntax?
Searched CASE evaluates boolean conditions in each WHEN clause, while Simple CASE compares an expression to fixed values.
Click to reveal answer
beginner
Can the ELSE clause in a Searched CASE be omitted? What happens then?
Yes, it can be omitted. If no WHEN condition matches, the CASE expression returns NULL.
Click to reveal answer
beginner
Give a real-life example where Searched CASE is useful.
For example, assigning grades based on score ranges: CASE WHEN score >= 90 THEN 'A' WHEN score >= 80 THEN 'B' ELSE 'F' END
Click to reveal answer
What does the Searched CASE syntax evaluate in each WHEN clause?
ATable names
BFixed values
CColumn names
DBoolean conditions
✗ Incorrect
Searched CASE evaluates boolean conditions in each WHEN clause to decide which result to return.
What will the CASE expression return if no WHEN condition matches and ELSE is omitted?
ANULL
B0
CError
DEmpty string
✗ Incorrect
If ELSE is omitted and no WHEN condition matches, the CASE expression returns NULL.
Which keyword starts a searched CASE expression?
AWHEN
BCASE
CIF
DSELECT
✗ Incorrect
The searched CASE expression always starts with the keyword CASE.
In Searched CASE, what does the ELSE clause do?
AProvides a default result if no WHEN condition is true
BEnds the CASE expression
CSpecifies the first condition
DIs mandatory
✗ Incorrect
ELSE provides a default result when none of the WHEN conditions are true.
Which of these is a valid Searched CASE syntax?
ACASE age = 18 THEN 'Adult' END
BCASE age WHEN 18 THEN 'Adult' END
CCASE WHEN age < 18 THEN 'Minor' ELSE 'Adult' END
DCASE age < 18 THEN 'Minor' END
✗ Incorrect
Option C correctly uses WHEN with a condition and an ELSE clause.
Explain how the Searched CASE syntax works in SQL and when you would use it.
Think about how you decide between multiple choices based on conditions.
You got /4 concepts.
Write a Searched CASE expression that categorizes a numeric score into 'High', 'Medium', or 'Low'.
Use score ranges like >80, >50, else.
You got /4 concepts.
Practice
(1/5)
1. What does the searched CASE expression in SQL do?
easy
A. Deletes rows from a table
B. Joins two tables together
C. Creates a new table
D. Returns different results based on multiple conditions
Solution
Step 1: Understand the purpose of searched CASE
The searched CASE expression evaluates multiple conditions and returns a result for the first true condition.
Step 2: Compare with other SQL operations
Unlike joins, deletes, or table creation, CASE is used to return values conditionally within queries.
Final Answer:
Returns different results based on multiple conditions -> Option D
Quick Check:
Searched CASE = conditional results [OK]
Hint: Think of CASE as SQL's if-else for conditions [OK]
Common Mistakes:
Confusing CASE with JOIN operations
Thinking CASE modifies tables
Assuming CASE deletes data
2. Which of the following is the correct syntax for a searched CASE expression in SQL?
easy
A. CASE WHEN condition THEN result ELSE default END
B. CASE condition WHEN THEN result ELSE END
C. CASE THEN condition WHEN result ELSE END
D. CASE WHEN THEN condition ELSE result END
Solution
Step 1: Recall the searched CASE syntax
The correct syntax starts with CASE, followed by WHEN condition THEN result, optional ELSE, and ends with END.
Step 2: Check each option
Only CASE WHEN condition THEN result ELSE default END follows the correct order: CASE WHEN condition THEN result ELSE default END.
Final Answer:
CASE WHEN condition THEN result ELSE default END -> Option A
Quick Check:
Syntax = CASE WHEN ... THEN ... ELSE ... END [OK]
Hint: Remember: WHEN condition THEN result, ELSE optional, END last [OK]
Common Mistakes:
Mixing order of WHEN and THEN
Omitting END keyword
Placing ELSE before THEN
3. Given the table Employees with column Salary, what will this query return?
SELECT Salary, CASE WHEN Salary > 5000 THEN 'High' WHEN Salary > 3000 THEN 'Medium' ELSE 'Low' END AS SalaryLevel FROM Employees;
medium
A. Returns only salaries greater than 5000
B. Shows an error because ELSE is missing
C. Labels each salary as 'High', 'Medium', or 'Low' based on value
D. Labels all salaries as 'Low'
Solution
Step 1: Analyze the CASE conditions
The CASE checks if Salary > 5000, then returns 'High'; else if Salary > 3000, returns 'Medium'; otherwise 'Low'.
Step 2: Understand the query output
Each row shows Salary and its label based on these conditions, so salaries are categorized correctly.
Final Answer:
Labels each salary as 'High', 'Medium', or 'Low' based on value -> Option C
Quick Check:
CASE categorizes salaries correctly [OK]
Hint: Read CASE conditions top to bottom, first true wins [OK]
Common Mistakes:
Thinking ELSE is required and missing causes error
Assuming query filters rows instead of labeling
Ignoring order of WHEN conditions
4. Identify the error in this searched CASE expression:
SELECT Name, CASE WHEN Age > 18 THEN 'Adult' WHEN Age < 18 THEN 'Minor' END AS AgeGroup FROM People;
medium
A. Incorrect use of THEN keyword
B. No error, query is correct
C. No END keyword to close CASE
D. Missing ELSE clause for other cases
Solution
Step 1: Check CASE syntax completeness
The CASE has WHEN conditions, THEN results, and ends with END keyword properly.
Step 2: Consider ELSE clause necessity
ELSE is optional; if no ELSE, NULL is returned for unmatched cases, which is valid.
Final Answer:
No error, query is correct -> Option B
Quick Check:
ELSE optional, END required, syntax correct [OK]
Hint: ELSE is optional; END is mandatory [OK]
Common Mistakes:
Assuming ELSE is always required
Forgetting END keyword
Misplacing THEN keyword
5. You want to classify products by price range using searched CASE. Which query correctly assigns 'Cheap' for prices below 50, 'Affordable' for prices between 50 and 150, and 'Expensive' for prices above 150?
hard
A. SELECT ProductName, CASE WHEN Price < 50 THEN 'Cheap' WHEN Price BETWEEN 50 AND 150 THEN 'Affordable' ELSE 'Expensive' END AS PriceRange FROM Products;
B. SELECT ProductName, CASE WHEN Price > 150 THEN 'Cheap' WHEN Price BETWEEN 50 AND 150 THEN 'Affordable' ELSE 'Expensive' END AS PriceRange FROM Products;
C. SELECT ProductName, CASE WHEN Price < 50 THEN 'Cheap' WHEN Price > 150 THEN 'Affordable' ELSE 'Expensive' END AS PriceRange FROM Products;
D. SELECT ProductName, CASE WHEN Price BETWEEN 50 AND 150 THEN 'Cheap' WHEN Price < 50 THEN 'Affordable' ELSE 'Expensive' END AS PriceRange FROM Products;
Solution
Step 1: Match conditions to price ranges
Price < 50 should be 'Cheap', 50 to 150 'Affordable', above 150 'Expensive'.
Step 2: Verify each option's logic
SELECT ProductName, CASE WHEN Price < 50 THEN 'Cheap' WHEN Price BETWEEN 50 AND 150 THEN 'Affordable' ELSE 'Expensive' END AS PriceRange FROM Products; correctly orders conditions and labels prices as required. Others mislabel or misorder conditions.
Final Answer:
SELECT ProductName, CASE WHEN Price < 50 THEN 'Cheap' WHEN Price BETWEEN 50 AND 150 THEN 'Affordable' ELSE 'Expensive' END AS PriceRange FROM Products; -> Option A
Quick Check:
Conditions match price ranges correctly [OK]
Hint: Order CASE conditions from smallest to largest range [OK]
Common Mistakes:
Mixing condition order causing wrong labels
Using wrong comparison operators
Misplacing BETWEEN ranges
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.