SQL RIGHT JOIN
RIGHT JOIN is the mirror image of LEFT JOIN: it keeps every row of the right table and fills the left table’s columns with NULL where nothing matches. Anything a right join does, a left join with the tables swapped does too, which is why you will see it less often, but you must be able to read it.
- Keep every row of the right table with RIGHT JOIN
- Find right-side rows with no match
- Rewrite a RIGHT JOIN as a LEFT JOIN
- Know when a RIGHT JOIN reads naturally
RIGHT JOIN syntax
SELECT columnsFROM left_table lRIGHT JOIN right_table r ON r.key = l.key;Sample data
| emp_id | first_name | dept_id |
|---|---|---|
| 100 | Meera | NULL |
| 101 | Asha | 1 |
| 102 | Rahul | 1 |
| 103 | Priya | 1 |
| 104 | Karan | 1 |
| 105 | Vikram | 2 |
| 106 | Neha | 2 |
| 107 | Arjun | 2 |
| 108 | Kavya | 3 |
| 109 | Rohan | 3 |
| 110 | Sneha | 4 |
| 111 | Amit | 1 |
| dept_id | dept_name | location |
|---|---|---|
| 1 | Engineering | Bengaluru |
| 2 | Sales | Mumbai |
| 3 | Marketing | Pune |
| 4 | HR | Delhi |
| 5 | Finance | Mumbai |
Every department, with its employees
SELECT d.dept_name, e.first_nameFROM employees eRIGHT JOIN departments d ON d.dept_id = e.dept_idORDER BY d.dept_id, e.first_name;| dept_name | first_name |
|---|---|
| Engineering | Amit |
| Engineering | Asha |
| Engineering | Karan |
| Engineering | Priya |
| Engineering | Rahul |
| Sales | Arjun |
| Sales | Neha |
| Sales | Vikram |
| Marketing | Kavya |
| Marketing | Rohan |
| HR | Sneha |
| Finance | NULL |
departmentsis on the right, so all 5 departments are kept.- Departments with employees get one row per employee.
- Finance has no employees: it appears once, with
NULLinfirst_name. - Meera, who has no department, is not in the result: she is in the left table and has no match.
Departments with no employees
SELECT d.dept_id, d.dept_nameFROM employees eRIGHT JOIN departments d ON d.dept_id = e.dept_idWHERE e.emp_id IS NULL;| dept_id | dept_name |
|---|---|
| 5 | Finance |
Rewrite as a LEFT JOIN
Swap the tables and change RIGHT to LEFT. The result is identical:
SELECT d.dept_name, e.first_nameFROM departments dLEFT JOIN employees e ON e.dept_id = d.dept_idORDER BY d.dept_id, e.first_name;| dept_name | first_name |
|---|---|
| Engineering | Amit |
| Engineering | Asha |
| Engineering | Karan |
| Engineering | Priya |
| Engineering | Rahul |
| Sales | Arjun |
| Sales | Neha |
| Sales | Vikram |
| Marketing | Kavya |
| Marketing | Rohan |
| HR | Sneha |
| Finance | NULL |
departmentsis now first (the left table), so all departments are kept.- The
ONcondition is the same equality, just written the other way round. - Many style guides use only
LEFT JOIN, because “start from the table you want all rows of” is easier to follow.
When RIGHT JOIN reads naturally
In a long chain of inner joins, you may want to keep all rows of a table you join last. A right join at the end does that without restructuring the query:
SELECT p.product_name, o.order_id, o.statusFROM orders oJOIN order_items oi ON oi.order_id = o.order_idRIGHT JOIN products p ON p.product_id = oi.product_idWHERE p.category = 'Furniture'ORDER BY p.product_name, o.order_id;| product_name | order_id | status |
|---|---|---|
| Desk Lamp | 1006 | Shipped |
| Desk Lamp | 1008 | Pending |
| Monitor Arm | NULL | NULL |
| Office Chair | 1002 | Delivered |
| Office Chair | 1008 | Pending |
| Standing Desk | 1004 | Cancelled |
orders JOIN order_itemsbuilds the order lines.RIGHT JOIN productskeeps every product, even those with no order line.- The Monitor Arm has never been ordered, so it appears with NULL order columns.
Exercise: Every product and its order lines
Using a RIGHT JOIN, list every product name with the order_ids it appears in, including products never ordered.
| product_name | order_id |
|---|---|
| Wireless Mouse | 1003 |
| Wireless Mouse | 1007 |
| Mechanical Keyboard | 1001 |
| Mechanical Keyboard | 1003 |
| Mechanical Keyboard | 1009 |
| USB-C Hub | 1006 |
| Office Chair | 1002 |
| Office Chair | 1008 |
| Standing Desk | 1004 |
| Notebook Pack | 1001 |
| Notebook Pack | 1005 |
| Notebook Pack | 1010 |
| Gel Pens | 1005 |
| Gel Pens | 1010 |
| Desk Lamp | 1006 |
| Desk Lamp | 1008 |
| Monitor Arm | NULL |
Hint 1
Put order_items first and products on the right.
SELECT p.product_name, oi.order_idFROM order_items oiRIGHT JOIN products p ON p.product_id = oi.product_idORDER BY p.product_id, oi.order_id;| product_name | order_id |
|---|---|
| Wireless Mouse | 1003 |
| Wireless Mouse | 1007 |
| Mechanical Keyboard | 1001 |
| Mechanical Keyboard | 1003 |
| Mechanical Keyboard | 1009 |
| USB-C Hub | 1006 |
| Office Chair | 1002 |
| Office Chair | 1008 |
| Standing Desk | 1004 |
| Notebook Pack | 1001 |
| Notebook Pack | 1005 |
| Notebook Pack | 1010 |
| Gel Pens | 1005 |
| Gel Pens | 1010 |
| Desk Lamp | 1006 |
| Desk Lamp | 1008 |
| Monitor Arm | NULL |
Quick quiz
1. A RIGHT JOIN B keeps all rows of…
The right table, B, is fully preserved.
2. Which query is equivalent to A RIGHT JOIN B ON ...?
Swapping the tables turns a right join into a left join.
RIGHT JOIN FAQ
What is the difference between LEFT JOIN and RIGHT JOIN?
LEFT JOIN keeps every row of the first (left) table; RIGHT JOIN keeps every row of the second (right) table. Swapping the table order converts one into the other.
Is RIGHT JOIN used in practice?
Less often than LEFT JOIN, because queries are usually easier to read when the table whose rows you keep comes first. It is fully supported in MySQL.
Is RIGHT OUTER JOIN the same as RIGHT JOIN?
Yes. The keyword OUTER is optional.