Nested CASE Expressions in SQL
📖 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