Last Chance to Join Data Science Interview MasterClass (this week) 🚀 | Just 2 spots remaining...

FREE

CASE

Course Author Daniel Lee
Instructor

Dan Lee

Learn SQL for free with interactive exercises designed to help aspiring Data Engineers, Data Analysts, and Data Scientists. Learn the basics and advanced SQL concepts by solving real-world problems from MANGO (Meta, Amazon, Netflix, Google, OpenAI)

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_idcustomer_idproduct_idorder_datequantitytotal_pricestatusdelivery_date
112“2024-01-10T14:30:00”159.99“Delivered”“2024-01-15T00:00:00”
223“2024-01-12T09:20:00”239.98“Shipped”“NaT”
335“2024-01-15T18:10:00”139.99“Pending”“NaT”
441“2024-01-18T20:45:00”377.97“Delivered”“2024-01-22T00:00:00”
554“2024-01-20T10:05:00”1699.99“Canceled”“NaT”
611“2024-01-25T16:15:00”251.98“Shipped”“NaT”
724“2024-01-27T12:40:00”1699.99“Pending”“NaT”
832“2024-01-29T08:55:00”2119.98“Delivered”“2024-02-02T00:00:00”
945“2024-02-01T14:00:00”139.99“Shipped”“NaT”
1053“2024-02-05T11:30:00”359.97“Pending”“NaT”

amazon_customers table:

customer_idnameemailcountrysignup_datemembership_statustotal_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_idcustomer_idtotal_pricestatusorder_category
1159.99DeliveredCompleted
4477.97DeliveredCompleted
83119.98DeliveredCompleted
2239.98ShippedIn Progress
6151.98ShippedIn Progress
9439.99ShippedIn Progress
3339.99PendingIn Progress
72699.99PendingIn Progress
10559.97PendingIn Progress
55699.99CanceledCanceled

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_idnametotal_spentspending_category
3Charlie Lee8500.75High Spender
1Alice Johnson5000.00High Spender
4David Kim3000.00Moderate Spender
2Bob Smith1200.50Moderate Spender
5Emma Garcia400.20Low 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_idcustomer_idtotal_pricestatusorder_category
1159.99DeliveredCompleted
4477.97DeliveredCompleted
83119.98DeliveredCompleted
2239.98ShippedIn Progress
6151.98ShippedIn Progress
9439.99ShippedIn Progress
3339.99PendingIn Progress
72699.99PendingIn Progress
10559.97PendingIn Progress
55699.99CanceledCanceled

Write an SQL query to return the requested data.

Upgrade to Pro

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_idnametotal_spentspending_category
3Charlie Lee8500.75High Spender
1Alice Johnson5000.00High Spender
4David Kim3000.00Moderate Spender
2Bob Smith1200.50Moderate Spender
5Emma Garcia400.20Low Spender

Write an SQL query to return the requested data.

Upgrade to Pro

👉 Found this lesson helpful?