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
Math Operations

Dan Lee
Lesson Objectives
By the end of this lesson, you will:
- Learn how to use COALESCE() to replace NULL values with defaults
- Use NULLIF() to handle cases where two values are equal
- Write SQL queries to manage missing data in Amazon orders and customer transactions
🎬 Scenario: Analyzing Order Totals and Customer Spending
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” |
1. Using Basic Math Operations in SQL
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 |
Example Query: Calculating Total Revenue per Order
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 highest-value orders appear first.
2. Using ROUND() to Format Decimal Values
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 |
Syntax
1SELECT ROUND(number, decimal_places) FROM table_name;
Example Query: Rounding Total Revenue
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.
3. Using AVG() to Calculate Average Customer Spending
The AVG() function computes the average value of a numeric column.
Example Query: Finding Average Customer Spending
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.
✍️ SQL Exercises
Exercise 1: Calculating Total Revenue
Sophia wants to calculate the total revenue per order based on quantity and price.
Filter condition:
- Retrieve order ID, quantity, price, and total revenue.
- Round total revenue to two decimal places.
- Sort by 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.
Exercise 2: Finding Average Customer Spending
Sophia also wants to analyze average customer spending per membership type.
Filter condition:
- Retrieve membership status and average total spent.
- Round the average to two decimal places.
- Sort by 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.