Join ML Engineer Interview MasterClass (April Cohort) led by FAANG Data Scientists | Just 6 seats remaining...
ML Engineer MasterClass (April) | 6 seats left
By the end of this lesson, you will:
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 |
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 |
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;Sophia wants a simplified order status report.
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?
CASE statement groups order statuses into three categories:
Sophia also wants to classify customers based on their total 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?
CASE statement assigns customers a category based on their total spending.Sophia needs a simplified order report showing whether orders are Completed, In Progress, or Canceled.
Filter condition:
order_category:
'Completed' for Delivered orders.'In Progress' for Shipped or Pending orders.'Canceled' for Canceled orders.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.
Sophia also wants to categorize Amazon customers based on their spending behavior.
Filter condition:
spending_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.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.