DataBolt

All Lessons

Introduction to SQL
SQL Lesson 1: Your Sample Database
SQL Lesson 2: SELECT — Reading data
SQL Lesson 3: WHERE — Filtering rows
SQL Lesson 4: AND, OR, NOT
SQL Lesson 5: BETWEEN, IN, LIKE
SQL Lesson 6: NULL — The Mystery Value
SQL Lesson 7: ORDER BY
SQL Lesson 8: LIMIT & OFFSET
SQL Lesson 9: Aggregate Functions
SQL Lesson 10: GROUP BY
SQL Lesson 11: HAVING
SQL Lesson 12: INNER JOINs
SQL Lesson 13: LEFT JOINs
SQL Lesson 14: RIGHT JOINs
SQL Lesson 15: SELF JOINs
SQL Lesson 16: UNION JOINs
SQL Lesson 17: Joining Multiple Tables
SQL Lesson 18: Subqueries
SQL Lesson 19: CTEs (WITH)
SQL Lesson 20: CASE Statements
SQL Lesson 21: Window Functions
SQL Lesson 22: String Functions
SQL Lesson 23: Date & Time Functions
SQL Lesson 24: INSERT, UPDATE, DELETE
SQL Lesson 25: CREATE TABLE & DDL
SQL Lesson 26: Indexes & Performance
SQL Lesson 27: Transactions & ACID
SQL Lesson 28: SQL Execution Order
SQL Lesson 29: 50 Practice Problems

CHAPTER 18

JOINing Three+ Tables

Aggregating by category — the analytics workhorse.

IN THIS CHAPTER

  • Joining three tables in one query
  • Aliasing tables to keep queries readable
  • Understanding JOIN order and performance

Customer names, order dates, and product names in each order

SELECT c.name AS customer, o.order_date, p.name AS product, oi.qty,
oi.unit_price
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
JOIN order_items oi ON o.order_id = oi.order_id
JOIN products p ON oi.product_id = p.product_id
ORDER BY o.order_date, c.name;

Pro Tips: Always use short, meaningful table aliases (c for customers, o for orders, p for products). Avoid single letters where they are ambiguous. In a 6-table JOIN, good aliases are the difference between readable code and maintenance hell.

Included Tables in Exercise

Table: Orders

order_idcustomer_idorder_datetotalstatus
112024-01-1579999completed
222024-01-203498completed
312024-02-1065000completed
432024-02-14499pending
522024-03-01248completed
652024-03-152999shipped
712024-04-05149completed

Table: Customers

customer_idnamecitycountry
1AnanyaHyderabadIndia
2RohanBangaloreIndia
3SamMumbaiIndia
4LisaLondonUK
5RaviDelhiIndia

Table: order_items

item_idorder_idproduct_idqtyunit_price
111179999
22312999
3221499
435165000
5421499
654299
756150
86312999

Table: products

product_idnamecategorypricestock
1iPhone 15Electronics7999950
2SQL BookBooks499200
3HeadphonesElectronics299975
4NotebookStationery99500
5LaptopElectronics6500020
6Pen SetStationery1490
7MouseElectronics59925

Exercise 👇

Exercise:

Tasks

1.👉Show customer names along with their order totals.
2.Show customer names and their order dates.
3.Find customers who have completed orders.
4.Show product names along with quantity ordered.
5.Find total amount spent by each customer.
6.Find number of orders placed by each customer (include customers with no orders).
7.Find total quantity sold for each product.
8.Show customer name, product name, and quantity ordered (combine all tables).
Stuck? Read this task's