PRO SQL Modules
SQL Basics1h 20m • 4 lessons
Multiple Tables1h 40m • 5 lessons
Query Restructuring40m • 2 lessons
Data Transformation1h 40m • 5 lessons
Analytical SQL2h • 6 lessons
CASE

Dan Lee
Lesson Objectives
By the end of this lesson, you will:
- Learn how to use CASE to apply conditional logic in SQL queries
- Understand how to categorize and transform data dynamically
- Write SQL queries to analyze Amazon order statuses and classify customers
🎬 Scenario: Categorizing Amazon Orders
Your manager at Amazon, Sophia, wants a report on customer orders, but instead of just showing raw order statuses, she wants them categorized:
Can you create a report where orders are labeled as ‘Completed’ for delivered orders, ‘In Progress’ for shipped or pending orders, and ‘Canceled’ for canceled orders?
Your task is to use CASE to classify orders into meaningful categories for better reporting. Use the amazon_orders and amazon_customers tables to address the questions.
amazon_orders table:
order_id | customer_id | product_id | order_date | quantity | total_price | status | delivery_date |
---|---|---|---|---|---|---|---|
1 | 1 | 2 | “2024-01-10T14:30:00” | 1 | 59.99 | “Delivered” | “2024-01-15T00:00:00” |
2 | 2 | 3 | “2024-01-12T09:20:00” | 2 | 39.98 | “Shipped” | “NaT” |
3 | 3 | 5 | “2024-01-15T18:10:00” | 1 | 39.99 | “Pending” | “NaT” |
4 | 4 | 1 | “2024-01-18T20:45:00” | 3 | 77.97 | “Delivered” | “2024-01-22T00:00:00” |
5 | 5 | 4 | “2024-01-20T10:05:00” | 1 | 699.99 | “Canceled” | “NaT” |
6 | 1 | 1 | “2024-01-25T16:15:00” | 2 | 51.98 | “Shipped” | “NaT” |
7 | 2 | 4 | “2024-01-27T12:40:00” | 1 | 699.99 | “Pending” | “NaT” |
8 | 3 | 2 | “2024-01-29T08:55:00” | 2 | 119.98 | “Delivered” | “2024-02-02T00:00:00” |
9 | 4 | 5 | “2024-02-01T14:00:00” | 1 | 39.99 | “Shipped” | “NaT” |
10 | 5 | 3 | “2024-02-05T11:30:00” | 3 | 59.97 | “Pending” | “NaT” |
amazon_customers table:
customer_id | name | country | signup_date | membership_status | total_spent | |
---|---|---|---|---|---|---|
1 | “Alice Johnson” | “alice@amazon.com” | “USA” | “2020-06-15T00:00:00” | “Prime” | 5000 |
2 | “Bob Smith” | “bob@amazon.com” | “Canada” | “2019-04-10T00:00:00” | “Regular” | 1200.5 |
3 | “Charlie Lee” | “charlie@amazon.com” | “UK” | “2021-02-22T00:00:00” | “Business” | 8500.75 |
4 | “David Kim” | “david@amazon.com” | “Germany” | “2022-09-30T00:00:00” | “Prime” | 3000 |
5 | “Emma Garcia” | “emma@amazon.com” | “Mexico” | “2023-01-12T00:00:00” | “Regular” | 400.2 |
1. Understanding CASE Statements
A CASE statement allows you to apply conditional logic within SQL queries.
Why use CASE? |
---|
Categorize data dynamically |
Perform conditional transformations |
Replace complex IF logic with cleaner SQL |
Syntax
1SELECT column1,
2 CASE
3 WHEN condition1 THEN result1
4 WHEN condition2 THEN result2
5 ELSE default_result
6 END AS new_column
7FROM table_name;
- Evaluates each condition in order.
- Returns the first matching condition.
- If no conditions match, the ELSE result is used (optional).
2. Using CASE to Categorize Order Status
Sophia wants a simplified order status report.
Example Query: Categorizing Amazon Orders
1SELECT order_id, customer_id, total_price, status,
2 CASE
3 WHEN status = 'Delivered' THEN 'Completed'
4 WHEN status IN ('Pending', 'Shipped') THEN 'In Progress'
5 WHEN status = 'Canceled' THEN 'Canceled'
6 ELSE 'Unknown'
7 END AS order_category
8FROM amazon_orders
9ORDER BY order_category;
order_id | customer_id | total_price | status | order_category |
---|---|---|---|---|
1 | 1 | 59.99 | Delivered | Completed |
4 | 4 | 77.97 | Delivered | Completed |
8 | 3 | 119.98 | Delivered | Completed |
2 | 2 | 39.98 | Shipped | In Progress |
6 | 1 | 51.98 | Shipped | In Progress |
9 | 4 | 39.99 | Shipped | In Progress |
3 | 3 | 39.99 | Pending | In Progress |
7 | 2 | 699.99 | Pending | In Progress |
10 | 5 | 59.97 | Pending | In Progress |
5 | 5 | 699.99 | Canceled | Canceled |
What’s Happening?
- The CASE statement groups order statuses into three categories:
- Completed → Delivered orders
- In Progress → Pending or shipped orders
- Canceled → Orders marked as canceled
- The result is a cleaner report that makes order tracking easier.
3. Using CASE to Categorize Customers Based on Spending
Sophia also wants to classify customers based on their total spending.
Example Query: Classifying Customers by Spending
1SELECT customer_id, name, total_spent,
2 CASE
3 WHEN total_spent > 5000 THEN 'High Spender'
4 WHEN total_spent BETWEEN 1000 AND 5000 THEN 'Moderate Spender'
5 ELSE 'Low Spender'
6 END AS spending_category
7FROM amazon_customers
8ORDER BY total_spent DESC;
customer_id | name | total_spent | spending_category |
---|---|---|---|
3 | Charlie Lee | 8500.75 | High Spender |
1 | Alice Johnson | 5000.00 | High Spender |
4 | David Kim | 3000.00 | Moderate Spender |
2 | Bob Smith | 1200.50 | Moderate Spender |
5 | Emma Garcia | 400.20 | Low Spender |
What’s Happening?
- The CASE statement assigns customers a category based on their total spending.
- This helps Amazon identify high-value customers for personalized promotions.
✍️ SQL Exercises
Exercise 1: Categorizing Amazon Orders
Sophia needs a simplified order report showing whether orders are Completed, In Progress, or Canceled.
Filter condition:
- Retrieve order ID, customer ID, total price, and status.
- Add a new columnorder_category:
- 'Completed' for Delivered orders.
- 'In Progress' for Shipped or Pending orders.
- 'Canceled' for Canceled orders.
- Sort by order_category in ascending order.
order_id | customer_id | total_price | status | order_category |
---|---|---|---|---|
1 | 1 | 59.99 | Delivered | Completed |
4 | 4 | 77.97 | Delivered | Completed |
8 | 3 | 119.98 | Delivered | Completed |
2 | 2 | 39.98 | Shipped | In Progress |
6 | 1 | 51.98 | Shipped | In Progress |
9 | 4 | 39.99 | Shipped | In Progress |
3 | 3 | 39.99 | Pending | In Progress |
7 | 2 | 699.99 | Pending | In Progress |
10 | 5 | 59.97 | Pending | In Progress |
5 | 5 | 699.99 | Canceled | Canceled |
Write an SQL query to return the requested data.
Exercise 2: Categorizing Customers by Spending
Sophia also wants to categorize Amazon customers based on their spending behavior.
Filter condition:
- Retrieve customer ID, name, and total spent.
- Add a new columnspending_category:
- 'High Spender' for customers with total spent greater than $5000.
- 'Moderate Spender' for total spent between $1000 and $5000.
- 'Low Spender' for total spent less than $1000.
- Sort by total_spent in descending order.
customer_id | name | total_spent | spending_category |
---|---|---|---|
3 | Charlie Lee | 8500.75 | High Spender |
1 | Alice Johnson | 5000.00 | High Spender |
4 | David Kim | 3000.00 | Moderate Spender |
2 | Bob Smith | 1200.50 | Moderate Spender |
5 | Emma Garcia | 400.20 | Low Spender |
Write an SQL query to return the requested data.