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

PRO SQL Modules

SQL Basics
1h 20m • 4 lessons
  1. SQL SELECT
  2. LIMIT
  3. ORDER BY
  4. DISTINCT
Data Filtering
1h 20m • 4 lessons
  1. WHERE
  2. LIKE
  3. IN
  4. BETWEEN
Aggregations
40m • 2 lessons
  1. GROUP BY
  2. HAVING
Multiple Tables
1h 40m • 5 lessons
  1. INNER JOIN
  2. LEFT & RIGHT JOIN
  3. FULL OUTER JOIN
  4. SELF JOIN
  5. INTERSECT & EXCEPT
Query Restructuring
40m • 2 lessons
  1. Subquery
  2. Common Table Expressions (CTE)
Data Transformation
1h 40m • 5 lessons
  1. CASE
  2. String
  3. Date Manipulation
  4. Math Operations
  5. COALESCE
Analytical SQL
2h • 6 lessons
  1. Ranking - ROW_NUMBER(), RANK(), DENSE_RANK()
  2. Running Totals
  3. LAG and LEAD
  4. FIRST_VALUE, LAST_VALUE, Nth_VALUE
  5. PARTITION BY
  6. SQL BETWEEN and PRECEDING
Unlock Premium
FREE

Math Operations

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 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_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

amazon_products table:

product_idproduct_namecategorypricestock_quantity
1“Wireless Mouse”“Electronics”25.99100
2“Bluetooth Headphones”“Electronics”59.9950
3“Yoga Mat”“Sports”19.99200
4“Smartphone”“Electronics”699.9930
5“Laptop Stand”“Office Supplies”39.9975

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”

1. Using Basic Math Operations in SQL

SQL supports basic arithmetic operations for performing calculations:

OperatorDescriptionExample
+Additionprice + tax
-Subtractiontotal_price - discount
*Multiplicationquantity * unit_price
/Divisiontotal / 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_idquantitypricetotal_revenue
51699.99699.99
71699.99699.99
8259.99119.98
4325.9977.97
1159.9959.99
10319.9959.97
6225.9951.98
3139.9939.99
9139.9939.99
2219.9939.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_idquantitypricetotal_revenue
51699.99699.99
71699.99699.99
8259.99119.98
4325.9977.97
1159.9959.99
10319.9959.97
6225.9951.98
3139.9939.99
9139.9939.99
2219.9939.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_statusavg_spent
Business8500.75
Prime4000.00
Regular800.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_idquantitypricetotal_revenue
51699.99699.99
71699.99699.99
8259.99119.98
4325.9977.97
1159.9959.99
10319.9959.97
6225.9951.98
3139.9939.99
9139.9939.99
2219.9939.98

Write an SQL query to return the requested data.

Upgrade to Pro

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_statusavg_spent
Business8500.75
Prime4000.00
Regular800.35

Write an SQL query to return the requested data.

Upgrade to Pro
👉 Found this lesson helpful?