Okay, let's break down the differences between INNER JOIN, LEFT JOIN, and RIGHT JOIN in PostgreSQL (and most other SQL databases, as these are standard SQL concepts).
The core idea of a JOIN is to combine rows from two or more tables based on a related column between them. The different join types determine how rows that don't have a match in the other table are handled.
Let's use two simple tables as an example:
Table: Departments
| dept_id | dept_name |
|---|
| 1 | Sales |
| 2 | IT |
| 3 | HR |
Table: Employees
| emp_id | emp_name | dept_id |
|---|
| 101 | Alice | 1 |
| 102 | Bob | 1 |
| 103 | Charlie | 2 |
| 104 | David | NULL |
Notice:
Sales (dept 1) and IT (dept 2) have matching employees.
HR (dept 3) has no matching employees.
David (emp 104) has no matching department.
We will join Departments (left table) and Employees (right table) on Departments.dept_id = Employees.dept_id.
1. INNER JOIN (or just JOIN)
-
What it does: Returns only the rows where the join condition is met in both tables.
-
Analogy: Finds the intersection or common part between the two tables based on the join condition.
-
How it handles non-matches: Rows from either table that do not have a match in the other table are excluded from the result set.
-
Query Example:
SELECT
d.dept_name,
e.emp_name
FROM
Departments d
INNER JOIN
Employees e ON d.dept_id = e.dept_id;
-
Result Set:
| dept_name | emp_name |
|---|
| Sales | Alice |
| Sales | Bob |
| IT | Charlie |
-
Explanation:
- 'HR' is excluded because it has no matching employee.
- 'David' is excluded because he has no matching department (his
dept_id is NULL, which doesn't match any department dept_id).
2. LEFT JOIN (or LEFT OUTER JOIN)
-
What it does: Returns all rows from the left table (the one listed before the LEFT JOIN keyword), plus any matching rows from the right table.
-
Analogy: Keep everything from the left side, and add corresponding items from the right where available.
-
How it handles non-matches:
- Rows from the left table that do not have a match in the right table are included. The columns from the right table for these rows will contain
NULL values.
- Rows from the right table that do not have a match in the left table are excluded.
-
Query Example:
SELECT
d.dept_name,
e.emp_name
FROM
Departments d -- This is the LEFT table
LEFT JOIN
Employees e ON d.dept_id = e.dept_id;
-
Result Set:
| dept_name | emp_name |
|---|
| Sales | Alice |
| Sales | Bob |
| IT | Charlie |
| HR | NULL |
-
Explanation:
- All departments ('Sales', 'IT', 'HR') are included because
Departments is the left table.
- Matching employees ('Alice', 'Bob', 'Charlie') are included with their departments.
- 'HR' has no matching employee, so
emp_name is NULL for that row.
- 'David' is excluded because he has no matching department, and
Employees is the right table.
3. RIGHT JOIN (or RIGHT OUTER JOIN)
-
What it does: Returns all rows from the right table (the one listed after the RIGHT JOIN keyword), plus any matching rows from the left table.
-
Analogy: Keep everything from the right side, and add corresponding items from the left where available. It's the mirror image of a LEFT JOIN.
-
How it handles non-matches:
- Rows from the right table that do not have a match in the left table are included. The columns from the left table for these rows will contain
NULL values.
- Rows from the left table that do not have a match in the right table are excluded.
-
Query Example:
SELECT
d.dept_name,
e.emp_name
FROM
Departments d -- This is the LEFT table
RIGHT JOIN
Employees e ON d.dept_id = e.dept_id; -- This is the RIGHT table
-
Result Set:
| dept_name | emp_name |
|---|
| Sales | Alice |
| Sales | Bob |
| IT | Charlie |
| NULL | David |
-
Explanation:
- All employees ('Alice', 'Bob', 'Charlie', 'David') are included because
Employees is the right table.
- Matching departments ('Sales', 'IT') are included with their employees.
- 'David' has no matching department, so
dept_name is NULL for that row.
- 'HR' is excluded because it has no matching employee, and
Departments is the left table.
Summary of Differences:
| Join Type | Matching Rows | Non-Matching Rows (Left Table) | Non-Matching Rows (Right Table) |
|---|
| INNER | Included | Excluded | Excluded |
| LEFT | Included | Included (Right columns are NULL) | Excluded |
| RIGHT | Included | Excluded | Included (Left columns are NULL) |
In practice, LEFT JOIN is more commonly used than RIGHT JOIN because you can achieve the same result by simply swapping the order of the tables in the FROM clause and using LEFT JOIN. For example, the RIGHT JOIN query above could be written as a LEFT JOIN by swapping Departments and Employees:
SELECT
d.dept_name,
e.emp_name
FROM
Employees e -- Now this is the LEFT table
LEFT JOIN
Departments d ON e.dept_id = d.dept_id; -- Now this is the RIGHT table
This would produce the exact same result as the RIGHT JOIN example.