Joins are the backbone of relational queries. Choosing the right join determines which records appear in your final output.
The Join Family
- INNER JOIN: Returns records that have matching values in both tables.
- LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table.
- RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table.
- FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table.
- CROSS JOIN: Returns the Cartesian product of the two tables.
INNER JOIN
SELECT u.name, o.amount
FROM users u
INNER JOIN orders o ON u.id = o.user_id;
Only rows where u.id matches o.user_id appear. Users without orders and orders without users are excluded.
LEFT JOIN
SELECT u.name, o.amount
FROM users u
LEFT JOIN orders o ON u.id = o.user_id;
All users appear. If a user has no orders, o.amount is NULL. This is the most common outer join used in interviews.
RIGHT JOIN
SELECT u.name, o.amount
FROM users u
RIGHT JOIN orders o ON u.id = o.user_id;
All orders appear. Orders without a valid user will have NULL for u.name. RIGHT JOIN is rarely used — most developers prefer LEFT JOIN and swap the table order.
FULL JOIN
SELECT u.name, o.amount
FROM users u
FULL JOIN orders o ON u.id = o.user_id;
All rows from both tables appear. Unmatched rows show NULLs on the opposite side. Useful for finding orphan records on either side.
CROSS JOIN
SELECT u.name, p.product_name
FROM users u
CROSS JOIN products p;
Every user is paired with every product. The result has rows(users) × rows(products) rows. Used for generating combinations like calendar dates × stores.
SELF JOIN
A SELF JOIN is a regular join where a table is joined with itself. It uses table aliases to distinguish the two roles:
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;
Common use cases: employee-manager hierarchies, finding duplicates, comparing rows within the same table.
JOIN Order Matters (with LEFT JOIN)
When chaining multiple left joins, the order of tables can change results. Each left join preserves all rows from its left input, so A LEFT JOIN B LEFT JOIN C keeps all rows from A, then all rows from (A + B) when joining C.
Q: Difference between LEFT JOIN and LEFT OUTER JOIN?
A: There is no difference. LEFT JOIN is shorthand for LEFT OUTER JOIN. Both perform the exact same operation.
Q: What is a SELF JOIN and when would you use it?
A: A SELF JOIN is a regular join, but the table is joined with itself. It is extremely useful for querying hierarchical data, such as an Employees table where one column refers to the ID of another employee (the manager).
Q: What is a CROSS JOIN?
A: A CROSS JOIN produces a result set which is the number of rows in the first table multiplied by the number of rows in the second table. It is used when you need every possible combination of rows.
Q: When does a LEFT JOIN become equivalent to INNER JOIN?
A: When you add a WHERE condition on a column from the right table, NULL rows are filtered out, effectively turning the LEFT JOIN into an INNER JOIN. Move such conditions into the ON clause to preserve left-side rows.
1. Find orders without users (orphan records).
SELECT o.order_id
FROM orders o
LEFT JOIN users u ON o.user_id = u.id
WHERE u.id IS NULL;2. Find users who never ordered.
SELECT u.name
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE o.order_id IS NULL;3. Get total revenue per user.
SELECT u.name, SUM(o.amount) as total_spent
FROM users u
INNER JOIN orders o ON u.id = o.user_id
GROUP BY u.name;4. Find employees and their managers (Self Join).
SELECT e.name AS Employee, m.name AS Manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;5. Find common records between two tables (Inner Join).
SELECT a.*
FROM TableA a
INNER JOIN TableB b ON a.id = b.id;6. Generate all user-product combinations.
SELECT u.name, p.product_name
FROM users u
CROSS JOIN products p;7. Find departments with no employees.
SELECT d.name
FROM departments d
LEFT JOIN employees e ON d.id = e.department_id
WHERE e.id IS NULL;Premium Content
Unlock Types of Joins and all premium lessons with a subscription.
From ₹199.99/year — See plans