Here's how these three types of SQL joins differ:
INNER JOIN
An INNER JOIN returns only the rows where there's a match in both tables. If a row in either table doesn't have a corresponding match in the other table, it's excluded from the results.
Example:
-- Customers table
customer_id | name
1 | Alice
2 | Bob
3 | Charlie
-- Orders table
order_id | customer_id | product
101 | 1 | Laptop
102 | 1 | Mouse
103 | 4 | Keyboard
-- INNER JOIN query
SELECT c.name, o.product
FROM Customers c
INNER JOIN Orders o ON c.customer_id = o.customer_id;
-- Result:
name | product
Alice | Laptop
Alice | Mouse
Notice that Charlie (no orders) and the order for customer_id 4 (no matching customer) are both excluded.
LEFT JOIN (or LEFT OUTER JOIN)
A LEFT JOIN returns all rows from the left table, plus matched rows from the right table. If there's no match in the right table, NULL values appear for those columns.
Example using the same tables:
SELECT c.name, o.product
FROM Customers c
LEFT JOIN Orders o ON c.customer_id = o.customer_id;
-- Result:
name | product
Alice | Laptop
Alice | Mouse
Bob | NULL
Charlie | NULL
All customers appear in the results, even if they have no orders.
FULL OUTER JOIN
A FULL OUTER JOIN returns all rows when there's a match in either table. It combines the results of both LEFT and RIGHT joins, showing NULL values where there's no match.
Example:
SELECT c.name, o.order_id, o.product
FROM Customers c
FULL OUTER JOIN Orders o ON c.customer_id = o.customer_id;
-- Result:
name | order_id | product
Alice | 101 | Laptop
Alice | 102 | Mouse
Bob | NULL | NULL
Charlie | NULL | NULL
NULL | 103 | Keyboard
This shows all customers and all orders, including the order for the non-existent customer_id 4.
The key distinction is what data you want to preserve: INNER JOIN keeps only matching data, LEFT JOIN keeps all left table data, and FULL OUTER JOIN keeps everything from both tables.