DataBolt

SQL LEARNING PLATFORM

16

CHAPTER 16

JOINing Three+ Tables

Aggregating by category — the analytics workhorse.

4

Learning Blocks

SQL

Interactive Queries

In This Chapter

Concepts you'll master

1

Joining three tables in one query

2

Aliasing tables to keep queries readable

3

Understanding JOIN order and performance

SQL QUERY
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.

Exercise

Practice your SQL skills

SQL EDITOR
Press semicolon (;) to auto-run query

Tasks

Complete all SQL challenges

1

Show customer names along with their order totals.

Current Task
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).

Need help solving this task?