6.3 — Multi-Table Joins
What You'll Learn
- How to join three or more tables in SQL
- How this compares to using multiple VLOOKUPs in Excel
- Syntax and best practices
Sample Tables
Table sales:
SaleID | ProductID | CustomerID | Amount |
---|---|---|---|
1 | 101 | 1001 | 120 |
2 | 102 | 1002 | 80 |
3 | 103 | 1003 | 50 |
Table products:
ProductID | ProductName |
---|---|
101 | Apples |
102 | Oranges |
103 | Bananas |
Table customers:
CustomerID | CustomerName |
---|---|
1001 | John Smith |
1002 | Jane Doe |
1003 | Mike Johnson |
Joining Multiple Tables
In Excel, you might use several VLOOKUPs to bring in data from different sheets. In SQL, you can join as many tables as you need.
Example: Join sales, products, and customers
sql
SELECT s.SaleID, s.Amount, p.ProductName, c.CustomerName
FROM sales s
INNER JOIN products p
ON s.ProductID = p.ProductID
INNER JOIN customers c
ON s.CustomerID = c.CustomerID;
- Each
JOIN
adds another table to your results. - Use table aliases (
s
,p
,c
) for readability.
Key Points
- You can join as many tables as needed, as long as you define the relationships.
- Use clear aliases to keep queries readable.
Next Steps
Next, you’ll learn how to migrate your Excel workflows to SQL for more efficient, scalable analysis.