SQL RIGHT JOIN
RIGHT JOIN (also RIGHT OUTER JOIN) is the mirror image of LEFT JOIN: it keeps every row of the right table, with matching left rows where they exist and NULLs where they don't.
SELECT columns
FROM left_table
RIGHT JOIN right_table ON left_table.column = right_table.column;Note: SQLite supports
RIGHT JOINandFULL JOINsince version 3.39 (2022). Older SQLite versions — still found in some tools — only haveLEFT JOIN.
Every product, sold or not
Here order_items is on the left and products on the right, so all products are kept:
SELECT p.product_id, p.name, SUM(oi.quantity) AS units_sold
FROM order_items AS oi
RIGHT JOIN products AS p ON p.product_id = oi.product_id
WHERE p.category_id IN (4, 7)
GROUP BY p.product_id
ORDER BY p.product_id;product_id | name | units_sold
-----------+-----------------+-----------
13 | Apples | 3
14 | Avocados | NULL
15 | Cherry Tomatoes | 18
16 | Bananas | 16
25 | Chef Knife | 9
26 | Cast Iron Pan | 19
27 | Coffee Grinder | NULL
(7 rows)
Avocados and the Coffee Grinder never sold, so their units_sold is NULL.
RIGHT JOIN = LEFT JOIN with the tables swapped
A RIGHT JOIN B gives the same rows as B LEFT JOIN A. This query is equivalent to the one above:
SELECT p.product_id, p.name, SUM(oi.quantity) AS units_sold
FROM products AS p
LEFT JOIN order_items AS oi ON oi.product_id = p.product_id
WHERE p.category_id IN (4, 7)
GROUP BY p.product_id
ORDER BY p.product_id;product_id | name | units_sold
-----------+-----------------+-----------
13 | Apples | 3
14 | Avocados | NULL
15 | Cherry Tomatoes | 18
16 | Bananas | 16
25 | Chef Knife | 9
26 | Cast Iron Pan | 19
27 | Coffee Grinder | NULL
(7 rows)
Because of this, many teams use only LEFT JOIN and always put the "keep everything" table first — queries read more consistently from top to bottom.
Employees and their orders
Which employees handled no orders at all? The employees table is on the right:
SELECT e.employee_id, e.first_name, e.title, COUNT(o.order_id) AS orders
FROM orders AS o
RIGHT JOIN employees AS e ON e.employee_id = o.employee_id
GROUP BY e.employee_id
HAVING COUNT(o.order_id) = 0
ORDER BY e.employee_id;employee_id | first_name | title | orders
------------+------------+--------------------+-------
1 | Grace | CEO | 0
2 | Victor | Sales Manager | 0
3 | Lin | Operations Manager | 0
7 | Marco | Warehouse Lead | 0
8 | Hana | Support Specialist | 0
10 | Elena | Data Analyst | 0
(6 rows)
When RIGHT JOIN is handy
RIGHT JOIN is useful when you add one more table at the end of a long chain of joins and want to keep all of its rows without rewriting the query:
SELECT c.name AS category, COUNT(p.product_id) AS products_over_20
FROM products AS p
RIGHT JOIN categories AS c
ON c.category_id = p.category_id AND p.price > 20
GROUP BY c.category_id
ORDER BY c.category_id;category | products_over_20
------------+-----------------
Beverages | 1
Bakery | 1
Dairy | 0
Produce | 0
Snacks | 0
Electronics | 4
Home | 3
Books | 3
(8 rows)
Categories without expensive products still appear with 0.
| Database | RIGHT JOIN support |
|---|---|
| PostgreSQL, MySQL, SQL Server, Oracle | yes |
| SQLite | since 3.39 |
Test yourself
A RIGHT JOIN B returns the same rows as…
- A LEFT JOIN B
- B LEFT JOIN A
- A INNER JOIN B
- B CROSS JOIN A
In orders RIGHT JOIN employees, which table's rows are all kept?
- orders
- employees
- both
- neither