📖 Scenario: You work in a retail store database. You want to classify sales transactions based on the amount and the payment method. This helps the store understand customer behavior better.
🎯 Goal: Build an SQL query that uses nested CASE expressions to categorize sales into different groups based on amount and payment_method.
📋 What You'll Learn
Create a table called sales with columns id (integer), amount (decimal), and payment_method (text).
Insert exactly three rows into sales with these values: (1, 50, 'cash'), (2, 200, 'card'), (3, 120, 'cash').
Write a SELECT query that uses a nested CASE expression to classify each sale into categories:
'Small Cash' if amount < 100 and payment_method is 'cash',
'Large Cash' if amount >= 100 and payment_method is 'cash',
'Card Payment' if payment_method is 'card',
Otherwise, classify as 'Other'.
💡 Why This Matters
🌍 Real World
Retail stores often classify sales transactions to analyze customer payment preferences and spending habits.
💼 Career
Understanding nested CASE expressions helps database analysts and developers write complex conditional queries for reporting and data transformation.
Progress0 / 4 steps
1
Create the sales table and insert data
Write SQL statements to create a table called sales with columns id (integer), amount (decimal), and payment_method (text). Then insert these exact rows: (1, 50, 'cash'), (2, 200, 'card'), and (3, 120, 'cash').
SQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Add a SELECT statement to retrieve all sales
Write a SQL SELECT statement to retrieve all columns from the sales table.
SQL
Hint
Use SELECT * FROM sales; to get all rows and columns.
3
Write a nested CASE expression to classify sales
Write a SQL SELECT statement that selects id, amount, payment_method, and a new column called category. Use a nested CASE expression inside the SELECT to classify each sale as follows: if payment_method is 'cash', then check if amount is less than 100 to label it 'Small Cash', else label it 'Large Cash'. If payment_method is 'card', label it 'Card Payment'. Otherwise, label it 'Other'.
SQL
Hint
Use a CASE inside another CASE to check payment_method first, then amount.
4
Complete the query with proper formatting
Ensure the final SQL query includes the nested CASE expression with correct indentation and aliases the classification column as category. The query should select id, amount, payment_method, and category from the sales table.
SQL
Hint
Check your query for correct CASE nesting and alias category.
Practice
(1/5)
1. What is the main purpose of using nested CASE expressions in SQL?
easy
A. To check multiple conditions inside another condition for detailed results
B. To create new tables from existing data
C. To delete rows based on conditions
D. To join two tables together
Solution
Step 1: Understand the role of CASE expressions
CASE expressions allow conditional logic to return different values based on conditions.
Step 2: Recognize nested CASE usage
Nested CASE means putting one CASE inside another to check conditions within conditions for more detailed categorization.
Final Answer:
To check multiple conditions inside another condition for detailed results -> Option A
Quick Check:
Nested CASE = detailed condition checks [OK]
Hint: Nested CASE means CASE inside CASE for detailed checks [OK]
Common Mistakes:
Confusing CASE with JOIN operations
Thinking CASE creates or deletes tables
Ignoring the need to close each CASE with END
2. Which of the following is the correct syntax for a nested CASE expression in SQL?
easy
A. CASE WHEN condition1 THEN IF condition2 THEN result1 ELSE result2 END ELSE result3 END
B. CASE WHEN condition1 THEN CASE condition2 THEN result1 ELSE result2 END ELSE result3 END
C. IF condition1 THEN CASE WHEN condition2 THEN result1 ELSE result2 END ELSE result3 END
D. CASE WHEN condition1 THEN CASE WHEN condition2 THEN result1 ELSE result2 END ELSE result3 END
Solution
Step 1: Review correct CASE syntax
CASE expressions use WHEN ... THEN ... ELSE ... END structure; nested CASE must follow this pattern inside THEN.
Step 2: Identify correct nested CASE
CASE WHEN condition1 THEN CASE WHEN condition2 THEN result1 ELSE result2 END ELSE result3 END correctly nests a CASE inside THEN with proper WHEN, THEN, ELSE, and END keywords.
Final Answer:
CASE WHEN condition1 THEN CASE WHEN condition2 THEN result1 ELSE result2 END ELSE result3 END -> Option D
Quick Check:
Nested CASE syntax = CASE WHEN condition1 THEN CASE WHEN condition2 THEN result1 ELSE result2 END ELSE result3 END [OK]
Hint: Nested CASE uses WHEN-THEN inside THEN with END for each CASE [OK]
Common Mistakes:
Using IF instead of CASE inside SQL
Missing END keyword for inner CASE
Incorrect keyword order in nested CASE
3. Given the table Employees with column Salary, what will this query return?
SELECT Salary, CASE WHEN Salary > 70000 THEN CASE WHEN Salary > 90000 THEN 'High' ELSE 'Medium' END ELSE 'Low' END AS SalaryLevel FROM Employees;
medium
A. Labels all salaries above 70000 as 'High' and others as 'Low'
B. Labels salaries above 90000 as 'High', between 70001 and 90000 as 'Medium', others as 'Low'
C. Labels all salaries below 70000 as 'Medium' and others as 'High'
D. Returns an error due to incorrect CASE syntax
Solution
Step 1: Analyze outer CASE condition
If Salary > 70000, then check inner CASE; else label 'Low'.
Step 2: Analyze inner CASE condition
Inside outer WHEN true, if Salary > 90000 then 'High', else 'Medium'.
Final Answer:
Labels salaries above 90000 as 'High', between 70001 and 90000 as 'Medium', others as 'Low' -> Option B
Quick Check:
Nested CASE categorizes salary levels correctly [OK]
Hint: Check outer condition first, then inner CASE for detailed labels [OK]
Common Mistakes:
Ignoring inner CASE and labeling all above 70000 as same
Confusing syntax causing errors
Misreading comparison operators
4. Identify the error in this nested CASE SQL snippet:
SELECT CASE WHEN score > 80 THEN CASE score > 90 THEN 'Excellent' ELSE 'Good' END ELSE 'Average' END AS Grade FROM Results;
medium
A. Using wrong comparison operator
B. Missing END keyword for outer CASE
C. Missing WHEN keyword before inner condition
D. Incorrect ELSE placement inside inner CASE
Solution
Step 1: Check inner CASE syntax
Inner CASE lacks WHEN before condition; it should be 'CASE WHEN score > 90 THEN ...'
Step 2: Verify overall CASE structure
Outer CASE has proper WHEN and END; error is only in inner CASE missing WHEN keyword.
Final Answer:
Missing WHEN keyword before inner condition -> Option C
Quick Check:
Inner CASE needs WHEN before condition [OK]
Hint: Every CASE condition must start with WHEN [OK]
Common Mistakes:
Forgetting WHEN in inner CASE
Assuming END is missing when it is present
Misplacing ELSE inside CASE
5. You want to classify products based on Price and Stock using nested CASE. If Price > 100 and Stock > 50, label 'Premium-Available'. If Price > 100 and Stock ≤ 50, label 'Premium-Limited'. Otherwise, label 'Standard'. Which SQL snippet correctly implements this?
hard
A. CASE WHEN Price > 100 THEN CASE WHEN Stock > 50 THEN 'Premium-Available' ELSE 'Premium-Limited' END ELSE 'Standard' END
B. CASE WHEN Price > 100 AND Stock > 50 THEN 'Premium-Available' WHEN Price > 100 AND Stock <= 50 THEN 'Premium-Limited' ELSE 'Standard' END
C. CASE WHEN Price > 100 THEN 'Premium' ELSE CASE WHEN Stock > 50 THEN 'Available' ELSE 'Limited' END END
D. CASE WHEN Price > 100 THEN CASE Stock > 50 THEN 'Premium-Available' ELSE 'Premium-Limited' END ELSE 'Standard' END
Solution
Step 1: Understand the nested conditions
First check if Price > 100, then inside check Stock > 50 or not.
Step 2: Match correct syntax and logic
CASE WHEN Price > 100 THEN CASE WHEN Stock > 50 THEN 'Premium-Available' ELSE 'Premium-Limited' END ELSE 'Standard' END correctly nests CASE inside THEN with proper WHEN, THEN, ELSE, and END keywords matching the problem.
Final Answer:
CASE WHEN Price > 100 THEN CASE WHEN Stock > 50 THEN 'Premium-Available' ELSE 'Premium-Limited' END ELSE 'Standard' END -> Option A
Quick Check:
Nested CASE matches Price then Stock checks [OK]
Hint: Check outer condition first, nest inner CASE for second condition [OK]
Common Mistakes:
Using AND instead of nested CASE
Missing WHEN in inner CASE
Incorrect ELSE placement
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.