Lesson 14 / 28
Self Joins & Joining 3+ Tables
Join a table to itself, and chain several joins together.
Self join
A self join joins a table to itself — useful for hierarchies, like an employee and their manager in the same table.
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id;Joining three tables
Chain multiple JOINs to pull related data from several tables in one query.
SELECT c.name, o.id AS order_id, p.title, oi.quantity
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN order_items oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id;