Skip to main content
Albero Academy

SQL Joins — INNER, LEFT, RIGHT, FULL

Understand the four classical joins, the difference between INNER and LEFT, common pitfalls, and how to find missing matches.

Chapter 2SQL15 min readBeginner friendly
  • #SQL
  • #Joins

Real databases never live in a single table. Customers in one table, orders in another, products in a third. Joins are how you stitch them back together for analysis.

Why we join

Take two tables: customers(id, name) and orders(id, customer_id, amount). To answer "which customers placed which orders", you need to connect them on the matching key — that's a join.

INNER JOIN

Returns rows with a match in both tables.

SQL
SELECT c.name, o.amount, o.order_date
FROM customers c
INNER JOIN orders o
ON o.customer_id = c.id;

LEFT JOIN

Returns all rows from the left table plus matches from the right. Where there's no match, the right side is NULL.

SQL
-- All customers, including those who never ordered
SELECT c.name, o.amount
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.id;

RIGHT & FULL OUTER JOIN

SQL
-- RIGHT — mirror image of LEFT (rarely used; flip the tables instead)
SELECT c.name, o.amount
FROM customers c
RIGHT JOIN orders o ON o.customer_id = c.id;
-- FULL OUTER — every row from BOTH tables
SELECT c.name, o.amount
FROM customers c
FULL OUTER JOIN orders o ON o.customer_id = c.id;
JoinReturns
INNERrows that match in BOTH tables
LEFTall rows from LEFT + matches from right
RIGHTall rows from RIGHT + matches from left
FULL OUTERall rows from both, NULL where no match

Finding missing matches

The classic interview pattern — "find customers who never placed an order":

SQL
SELECT c.id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;

This is faster than the equivalent NOT IN on most engines, and unlike NOT IN it handles NULLs correctly.

Self joins

A self join is when you join a table to itself — for example, an employees table where each row has a manager_id:

SQL
-- Get every employee with their manager's name
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m
ON m.id = e.manager_id;

Key takeaways

  1. 01INNER JOIN keeps only matched rows. LEFT JOIN keeps all left rows + matches.
  2. 02Always alias your tables (c, o, e, m) — it makes joins readable.
  3. 03LEFT JOIN ... WHERE x IS NULL is the canonical "find rows in A but not B" pattern.
  4. 04RIGHT JOIN exists but is rarely written — just flip the tables and use LEFT.
  5. 05Self joins solve hierarchy problems (employees → managers, comments → parent comments).