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 spending patterns and order totals.
Can you calculate total revenue per order and round values for easier reporting?
Your task is to use SQL math functions to analyze and format financial data. Use the following tables to solve the questions.
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 |
amazon_products table:
| product_id | product_name | category | price | stock_quantity |
|---|---|---|---|---|
| 1 | “Wireless Mouse” | “Electronics” | 25.99 | 100 |
| 2 | “Bluetooth Headphones” | “Electronics” | 59.99 | 50 |
| 3 | “Yoga Mat” | “Sports” | 19.99 | 200 |
| 4 | “Smartphone” | “Electronics” | 699.99 | 30 |
| 5 | “Laptop Stand” | “Office Supplies” | 39.99 | 75 |
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” |
SQL supports basic arithmetic operations for performing calculations:
| Operator | Description | Example |
|---|---|---|
+ | Addition | price + tax |
- | Subtraction | total_price - discount |
* | Multiplication | quantity * unit_price |
/ | Division | total / count |
1SELECT order_id, quantity, price, quantity * price AS total_revenue
2FROM amazon_orders
3JOIN amazon_products ON amazon_orders.product_id = amazon_products.product_id
4ORDER BY total_revenue DESC;| order_id | quantity | price | total_revenue |
|---|---|---|---|
| 5 | 1 | 699.99 | 699.99 |
| 7 | 1 | 699.99 | 699.99 |
| 8 | 2 | 59.99 | 119.98 |
| 4 | 3 | 25.99 | 77.97 |
| 1 | 1 | 59.99 | 59.99 |
| 10 | 3 | 19.99 | 59.97 |
| 6 | 2 | 25.99 | 51.98 |
| 3 | 1 | 39.99 | 39.99 |
| 9 | 1 | 39.99 | 39.99 |
| 2 | 2 | 19.99 | 39.98 |
What’s Happening?
quantity * price calculates total revenue for each order.The ROUND() function formats numeric results to a specific number of decimal places.
| Why use ROUND()? |
|---|
| Improve readability of reports |
| Format financial calculations |
| Avoid long decimal values |
SELECT ROUND(number, decimal_places) FROM table_name;1SELECT order_id, quantity, price,
2 ROUND(quantity * price, 2) AS total_revenue
3FROM amazon_orders
4JOIN amazon_products ON amazon_orders.product_id = amazon_products.product_id
5ORDER BY total_revenue DESC;| order_id | quantity | price | total_revenue |
|---|---|---|---|
| 5 | 1 | 699.99 | 699.99 |
| 7 | 1 | 699.99 | 699.99 |
| 8 | 2 | 59.99 | 119.98 |
| 4 | 3 | 25.99 | 77.97 |
| 1 | 1 | 59.99 | 59.99 |
| 10 | 3 | 19.99 | 59.97 |
| 6 | 2 | 25.99 | 51.98 |
| 3 | 1 | 39.99 | 39.99 |
| 9 | 1 | 39.99 | 39.99 |
| 2 | 2 | 19.99 | 39.98 |
What’s Happening?
ROUND(quantity * price, 2) ensures two decimal places for currency formatting.The AVG() function computes the average value of a numeric column.
1SELECT membership_status,
2 ROUND(AVG(total_spent), 2) AS avg_spent
3FROM amazon_customers
4GROUP BY membership_status
5ORDER BY avg_spent DESC;| membership_status | avg_spent |
|---|---|
| Business | 8500.75 |
| Prime | 4000.00 |
| Regular | 800.35 |
What’s Happening?
AVG(total_spent) calculates average spending per membership type.ROUND(..., 2) ensures clean financial reporting.Sophia wants to calculate the total revenue per order based on quantity and price.
Filter condition:
total_revenue in descending order.| order_id | quantity | price | total_revenue |
|---|---|---|---|
| 5 | 1 | 699.99 | 699.99 |
| 7 | 1 | 699.99 | 699.99 |
| 8 | 2 | 59.99 | 119.98 |
| 4 | 3 | 25.99 | 77.97 |
| 1 | 1 | 59.99 | 59.99 |
| 10 | 3 | 19.99 | 59.97 |
| 6 | 2 | 25.99 | 51.98 |
| 3 | 1 | 39.99 | 39.99 |
| 9 | 1 | 39.99 | 39.99 |
| 2 | 2 | 19.99 | 39.98 |
Write an SQL query to return the requested data.
Sophia also wants to analyze average customer spending per membership type.
Filter condition:
avg_spent in descending order.| membership_status | avg_spent |
|---|---|
| Business | 8500.75 |
| Prime | 4000.00 |
| Regular | 800.35 |
Write an SQL query to return the requested data.