SQL Tutorial › Joins & Set Operators › RIGHT JOIN

SQL RIGHT JOIN

IntermediateLesson 34 of 698 min read4 queries run on MySQL 8.4

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.

Advertisement
In this lesson
  • 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

MySQL
SELECT columnsFROM left_table lRIGHT JOIN right_table r ON r.key = l.key;

Sample data

employees · 12 rows (some columns)
emp_idfirst_namedept_id
100MeeraNULL
101Asha1
102Rahul1
103Priya1
104Karan1
105Vikram2
106Neha2
107Arjun2
108Kavya3
109Rohan3
110Sneha4
111Amit1
departments · 5 rows
dept_iddept_namelocation
1EngineeringBengaluru
2SalesMumbai
3MarketingPune
4HRDelhi
5FinanceMumbai

Every department, with its employees

SQL
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;
ResultMySQL 8.4
dept_namefirst_name
EngineeringAmit
EngineeringAsha
EngineeringKaran
EngineeringPriya
EngineeringRahul
SalesArjun
SalesNeha
SalesVikram
MarketingKavya
MarketingRohan
HRSneha
FinanceNULL
12 rows in set
How this query works, step by step
  1. departments is on the right, so all 5 departments are kept.
  2. Departments with employees get one row per employee.
  3. Finance has no employees: it appears once, with NULL in first_name.
  4. Meera, who has no department, is not in the result: she is in the left table and has no match.
Advertisement

Departments with no employees

SQL
SELECT d.dept_id, d.dept_nameFROM employees eRIGHT JOIN departments d ON d.dept_id = e.dept_idWHERE e.emp_id IS NULL;
ResultMySQL 8.4
dept_iddept_name
5Finance
1 row in set

Rewrite as a LEFT JOIN

Swap the tables and change RIGHT to LEFT. The result is identical:

SQL
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;
ResultMySQL 8.4
dept_namefirst_name
EngineeringAmit
EngineeringAsha
EngineeringKaran
EngineeringPriya
EngineeringRahul
SalesArjun
SalesNeha
SalesVikram
MarketingKavya
MarketingRohan
HRSneha
FinanceNULL
12 rows in set
How this query works, step by step
  1. departments is now first (the left table), so all departments are kept.
  2. The ON condition is the same equality, just written the other way round.
  3. Many style guides use only LEFT JOIN, because “start from the table you want all rows of” is easier to follow.
Advertisement

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:

SQL
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;
ResultMySQL 8.4
product_nameorder_idstatus
Desk Lamp1006Shipped
Desk Lamp1008Pending
Monitor ArmNULLNULL
Office Chair1002Delivered
Office Chair1008Pending
Standing Desk1004Cancelled
6 rows in set
How this query works, step by step
  1. orders JOIN order_items builds the order lines.
  2. RIGHT JOIN products keeps every product, even those with no order line.
  3. The Monitor Arm has never been ordered, so it appears with NULL order columns.
Advertisement

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.

Expected resultMySQL 8.4
product_nameorder_id
Wireless Mouse1003
Wireless Mouse1007
Mechanical Keyboard1001
Mechanical Keyboard1003
Mechanical Keyboard1009
USB-C Hub1006
Office Chair1002
Office Chair1008
Standing Desk1004
Notebook Pack1001
Notebook Pack1005
Notebook Pack1010
Gel Pens1005
Gel Pens1010
Desk Lamp1006
Desk Lamp1008
Monitor ArmNULL
17 rows in set
Hint 1

Put order_items first and products on the right.

Quick quiz

1. A RIGHT JOIN B keeps all rows of…

2. Which query is equivalent to A RIGHT JOIN B ON ...?

Advertisement

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.