RIGHT JOIN

Updated · 6 min read
easyPostgreSQL 18sqlright-joinouter-joinjoins

RIGHT JOIN keeps every row of the right table, the one written second, and fills the left table's columns with NULL where nothing matched. It's the exact mirror of LEFT JOIN: A RIGHT JOIN B gives the same rows as B LEFT JOIN A. Because of that, you'll rarely need to write one, but you'll certainly read them in other people's queries. This short lesson shows how it works, proves the mirror rule on real rows, and explains why most teams turn every right join into a left join.

In One Minute

  • FROM a RIGHT JOIN b ON ... keeps every row of b, the table written second.
  • Unmatched rows get NULL in a's columns.
  • a RIGHT JOIN b returns the same rows as b LEFT JOIN a.
  • RIGHT OUTER JOIN is the same thing; OUTER is optional.
  • Most style guides say: write LEFT JOIN instead, with the table you're keeping first.
RIGHT JOIN keeps every row of the right-hand table

The Basics

SQL
SELECT columns
FROM left_table
RIGHT JOIN right_table ON left_table.key = right_table.key;

Every course, with any enrollments it has. courses is written second, so it's the table that's kept whole:

SQL▶ Try it
SELECT e.student_id, c.title
FROM enrollments AS e
RIGHT JOIN courses AS c ON c.course_id = e.course_id
ORDER BY c.course_id, e.student_id;
Output
 student_id |       title
------------+-------------------
        101 | Java Basics
        102 | Java Basics
        101 | Java OOP
        101 | Java Collections
        101 | SQL Basics
        102 | SQL Basics
        103 | JavaScript Basics
       NULL | Git Basics
(8 rows)

All six courses appear. Git Basics has no enrollments, so its student_id is NULL. The enrollments, written first, are only included where they match.

The Mirror of LEFT JOIN

Swap the two tables and change RIGHT to LEFT, and you get the same rows. The same question, written as a left join:

SQL▶ Try it
SELECT e.student_id, c.title
FROM courses AS c
LEFT JOIN enrollments AS e ON e.course_id = c.course_id
ORDER BY c.course_id, e.student_id;
Output
 student_id |       title
------------+-------------------
        101 | Java Basics
        102 | Java Basics
        101 | Java OOP
        101 | Java Collections
        101 | SQL Basics
        102 | SQL Basics
        103 | JavaScript Basics
       NULL | Git Basics
(8 rows)

Identical, row for row. Only the order of the tables in FROM changed. The ON condition and the SELECT list stay the same; the column order in the result is whatever SELECT says, not the table order.

enrollments RIGHT JOIN courses and courses LEFT JOIN enrollments both keep every course, including Git Basics

Why Most Teams Avoid It

A query reads top to bottom, and it's easiest when the table you're keeping comes first. With left joins only, the rule is always the same: the first table is the one whose rows all survive, and each later join adds optional detail. Mix in a right join and the reader has to work out which table is kept. In a query with several joins, a right join near the end can even keep rows the earlier joins seemed to filter out, as this picture shows:

With LEFT JOIN only, the first table is the one kept; a RIGHT JOIN makes the reader hunt for it

SQLite added RIGHT JOIN only in version 3.39 (2022), so very old SQLite databases don't support it at all; there, the left join form is the only option. PostgreSQL, MySQL, SQL Server and Oracle all support both.

Common Mistakes

Mixing up which table is kept. In a RIGHT JOIN b, it's b, the table written second.

Mixing LEFT and RIGHT in one query. It works, but it's hard to read. Use left joins throughout.

Expecting different results from the mirror. a RIGHT JOIN b and b LEFT JOIN a always return the same rows.

Interview Corner

What's the difference between LEFT JOIN and RIGHT JOIN?

LEFT JOIN keeps every row of the first table; RIGHT JOIN keeps every row of the second. A RIGHT JOIN B returns the same rows as B LEFT JOIN A.

Why do many teams avoid RIGHT JOIN?

Any right join can be written as a left join by swapping the tables, and left joins read more naturally: the first table is always the one whose rows are kept.

Practice

Challenge: This query uses RIGHT JOIN to list every student with their payments. Rewrite it as a LEFT JOIN that returns exactly the same rows.

SQL
SELECT s.first_name, p.payment_id
FROM payments AS p
RIGHT JOIN students AS s ON s.student_id = p.student_id
ORDER BY s.student_id, p.payment_id;
Show solution
SQL▶ Try it
SELECT s.first_name, p.payment_id
FROM students AS s
LEFT JOIN payments AS p ON p.student_id = s.student_id
ORDER BY s.student_id, p.payment_id;
Output
 first_name | payment_id
------------+------------
 Srihitha   |          1
 Srihitha   |          2
 Srihitha   |          3
 Srihitha   |          4
 Srihitha   |          5
 Advitha    |       NULL
 Navika     |          6
 Navika     |          7
(8 rows)

Students moved to the front, RIGHT became LEFT, and nothing else changed. Advitha still appears with NULL.

Quick check: RIGHT JOIN

2 questions
1In enrollments RIGHT JOIN courses, which table keeps every row?
2Which query returns the same rows as payments RIGHT JOIN students ON ...?

Key Takeaways

You want toWrite
Every course, with enrollmentsFROM courses c LEFT JOIN enrollments e ON e.course_id = c.course_id
The same with RIGHT JOINFROM enrollments e RIGHT JOIN courses c ON c.course_id = e.course_id

Was this tutorial helpful?

Lesson 38 of 115