INNER JOIN vs OUTER JOIN in SQL
Compare INNER, LEFT, RIGHT and FULL OUTER JOIN on the same data. See which rows each join keeps and why a WHERE filter can change the result.
By Learn SQL Online ·
An INNER JOIN returns one row for every pair that matches the ON condition. An outer join returns those same pairs and keeps unmatched rows from one or both tables. SQL fills the columns on the missing side with NULL.
| Join | Matching pairs | Unmatched left rows | Unmatched right rows |
|---|---|---|---|
INNER JOIN |
Yes | No | No |
LEFT OUTER JOIN |
Yes | Yes | No |
RIGHT OUTER JOIN |
Yes | No | Yes |
FULL OUTER JOIN |
Yes | Yes | Yes |
The left table appears before the join and the right table appears after it. LEFT JOIN, RIGHT JOIN and FULL JOIN are the shorter forms of the three outer joins.
INNER JOIN returns matching pairs
In the Acme sample data, the departments table has a Research department without employees. The employees table has a contractor, Ivan Petrov, without a department. An inner join excludes both. Engineering appears once per matching employee because each match produces its own row.
SELECT d.name AS department, e.name AS employee
FROM departments AS d
INNER JOIN employees AS e ON e.department_id = d.id
ORDER BY d.id, e.id;
| department | employee |
|---|---|
| Engineering | Maya Chen |
| Engineering | Omar Haddad |
| Engineering | Lena Park |
| Engineering | Ravi Patel |
| Engineering | Sofia Rossi |
| Sales | Jack Miller |
| Sales | Ana Lopez |
| Sales | Ben Okafor |
| Sales | Chloe Martin |
| Marketing | Diego Alvarez |
LEFT and RIGHT OUTER JOIN keep one side
Use a LEFT JOIN when the report needs every department. Research appears with NULL in the employee column. The sort puts it first so you can see it in the preview.
SELECT d.name AS department, e.name AS employee
FROM departments AS d
LEFT JOIN employees AS e ON e.department_id = d.id
ORDER BY CASE WHEN e.id IS NULL THEN 0 ELSE 1 END, d.id, e.id;
| department | employee |
|---|---|
| Research | NULL |
| Engineering | Maya Chen |
| Engineering | Omar Haddad |
| Engineering | Lena Park |
| Engineering | Ravi Patel |
| Engineering | Sofia Rossi |
| Sales | Jack Miller |
| Sales | Ana Lopez |
| Sales | Ben Okafor |
| Sales | Chloe Martin |
With these tables in the same order, RIGHT JOIN keeps every employee, including Ivan, and drops the empty Research department.
SELECT d.name AS department, e.name AS employee
FROM departments AS d
RIGHT JOIN employees AS e ON e.department_id = d.id
ORDER BY CASE WHEN d.id IS NULL THEN 0 ELSE 1 END, e.id;
| department | employee |
|---|---|
| NULL | Ivan Petrov |
| Engineering | Maya Chen |
| Engineering | Omar Haddad |
| Engineering | Lena Park |
| Engineering | Ravi Patel |
| Engineering | Sofia Rossi |
| Sales | Jack Miller |
| Sales | Ana Lopez |
| Sales | Ben Okafor |
| Sales | Chloe Martin |
You get the same employee and department pairs by swapping the tables and using employees LEFT JOIN departments.
FULL OUTER JOIN keeps both sides
Use a FULL OUTER JOIN to see missing assignments on both sides. Research and Ivan both appear, each with NULL on the missing side.
SELECT d.name AS department, e.name AS employee
FROM departments AS d
FULL OUTER JOIN employees AS e ON e.department_id = d.id
ORDER BY CASE WHEN e.id IS NULL OR d.id IS NULL THEN 0 ELSE 1 END,
d.id, e.id;
| department | employee |
|---|---|
| NULL | Ivan Petrov |
| Research | NULL |
| Engineering | Maya Chen |
| Engineering | Omar Haddad |
| Engineering | Lena Park |
| Engineering | Ravi Patel |
| Engineering | Sofia Rossi |
| Sales | Jack Miller |
| Sales | Ana Lopez |
| Sales | Ben Okafor |
PostgreSQL, SQL Server and SQLite 3.39 or later support FULL OUTER JOIN. MySQL’s JOIN syntax supports LEFT and RIGHT outer joins. PostgreSQL join syntax · SQL Server join documentation · SQLite 3.39 release notes · MySQL join syntax
Why a WHERE filter can remove the rows you meant to keep
Suppose you want every department and any of its employees hired in 2023 or later. Put the date condition in ON so it determines which employees can match. Departments with no qualifying employee still appear.
SELECT d.name AS department, e.name AS employee
FROM departments AS d
LEFT JOIN employees AS e
ON e.department_id = d.id
AND e.hired >= '2023-01-01'
ORDER BY d.id, e.id;
| department | employee |
|---|---|
| Engineering | NULL |
| Sales | Chloe Martin |
| Marketing | NULL |
| Support | Hana Sato |
| Research | NULL |
Move e.hired >= '2023-01-01' to WHERE and the query filters the result after the join. Research disappears because its missing hire date makes the comparison unknown. Engineering also disappears because each of its employees was hired before 2023. The result has just Sales and Support. PostgreSQL documentation on ON and WHERE
Try it yourself
Open the SQL Playground, select Acme Co. (company data) and paste in the LEFT JOIN example. Add WHERE e.id IS NULL before ORDER BY. You should get just Research. Checking e.id works because every real employee has an ID, so a NULL there tells you the join found no match.