SQL Joins Explained: INNER, LEFT, RIGHT, FULL and SELF JOIN with Examples
On this page
Short answer: SQL joins combine rows from two or more tables using a related column. INNER JOIN returns only matching rows; LEFT JOIN returns all rows from the left table plus matches (NULLs where there’s no match); RIGHT JOIN is the mirror image; FULL OUTER JOIN returns all rows from both tables; CROSS JOIN returns every combination; and a SELF JOIN joins a table to itself. INNER and LEFT JOIN cover most real-world analysis.
Key takeaways
- INNER = matches only; LEFT = everything from the left table.
- Use LEFT JOIN … WHERE right.key IS NULL to find rows with no match.
- Filters in ON vs WHERE behave differently with outer joins.
- Joining on non-unique keys can duplicate rows; always check counts.
Example tables
customers
| customer_id | name | city |
|---|---|---|
| 1 | Asha | Mumbai |
| 2 | Rahul | Pune |
| 3 | Neha | Thane |
orders
| order_id | customer_id | amount |
|---|---|---|
| 101 | 1 | 500 |
| 102 | 1 | 300 |
| 103 | 2 | 700 |
| 104 | 4 | 200 |
Note: Neha (3) has no orders, and order 104 belongs to customer 4, who isn’t in the customers table.
INNER JOIN
SELECT c.name, o.order_id, o.amount
FROM customers c
INNER JOIN orders o ON c.customer_id = o.customer_id;
Result: 3 rows: Asha–101, Asha–102, Rahul–103. Neha and order 104 are excluded.
LEFT JOIN
SELECT c.name, o.order_id, o.amount
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id;
Result: 4 rows: the 3 matches plus Neha with NULL order_id and amount.
RIGHT JOIN
SELECT c.name, o.order_id, o.amount
FROM customers c
RIGHT JOIN orders o ON c.customer_id = o.customer_id;
Result: 4 rows: the 3 matches plus order 104 with a NULL name. Most analysts rewrite RIGHT JOINs as LEFT JOINs by swapping table order for readability.
FULL OUTER JOIN
SELECT c.name, o.order_id, o.amount
FROM customers c
FULL OUTER JOIN orders o ON c.customer_id = o.customer_id;
Result: 5 rows: matches, Neha (no order) and order 104 (no customer). MySQL doesn’t support FULL OUTER JOIN; emulate it with a LEFT JOIN UNION a RIGHT JOIN.
SELECT c.name, o.order_id FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id
UNION
SELECT c.name, o.order_id FROM customers c RIGHT JOIN orders o ON c.customer_id = o.customer_id;
CROSS JOIN
Returns every combination of rows (3 customers × 4 orders = 12 rows). Useful for generating combinations such as every product × every month.
SELECT p.product, m.month FROM products p CROSS JOIN months m;
SELF JOIN
Joins a table to itself, for example to find each employee’s manager:
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;
Anti-join: find rows with no match
-- Customers who never ordered
SELECT c.name
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;
NOT EXISTS is an equally valid (often preferred) alternative.
ON vs WHERE in outer joins
-- Keeps all customers; only orders above 400 are matched
SELECT c.name, o.amount
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id AND o.amount > 400;
-- Filters after the join, so customers without such orders disappear
SELECT c.name, o.amount
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.amount > 400;
The second query effectively becomes an INNER JOIN. This is a classic interview question.
Common join mistakes
- Duplicate rows from joining on non-unique keys, which inflates SUMs
- Missing join condition, which creates an accidental cross join
- Filtering the right table in WHERE after a LEFT JOIN
- Mismatched data types in join keys (text vs number)
- NULL keys, which never match with “=”
Joins in Pandas
The same logic applies in Python: pd.merge(customers, orders, on='customer_id', how='left'). See Pandas interview questions.
Frequently asked questions
Which join is used most?
INNER JOIN and LEFT JOIN cover the vast majority of analysis queries.
Is JOIN the same as INNER JOIN?
Yes. Writing just JOIN defaults to an INNER JOIN.
What’s the difference between JOIN and UNION?
JOIN combines columns from related tables side by side; UNION stacks rows from queries with the same columns.
Are joins slow?
Joins on indexed keys are efficient. Slowness usually comes from missing indexes, functions on join columns, or joining far more rows than needed.
Conclusion
With SQL joins explained through one small example, remember: INNER for matches, LEFT to keep everything from the main table, anti-joins for “never happened” questions, and always check row counts. Next, learn SQL window functions and practise SQL interview questions.
Master SQL hands-on in our MySQL course.
Learn with Mehul Prajapati in Borivali West
At Mehul Prajapati’s institute, you get 100% practical, hands-on training by industry experts with a minimum of 5+ years of experience, plus job-oriented programmes with placement support. Weekday and weekend batches are available, with multiple 2-hour slots between 8 am and 6 pm.
Address: 12th Floor, 1214, Gold Crest Business Centre, LT Rd, Above Westside Showroom, Borivali West, Mumbai, Maharashtra 400092
Call: +91 99301 12627 / +91 96645 45072
Visit the centre or call us to choose the right course and batch and to know the current fee structure.