← Back to PostgreSQL Course | Chapter 6: Joins | Lesson 3 of 7

RIGHT JOIN

RIGHT JOIN keeps every row from the right table and fills the left side with NULL when there is no match.

In this page:

  1. RIGHT JOIN
Syntax
sql
SELECT columns
FROM table1
RIGHT JOIN table2 ON table1.key = table2.key;

RIGHT JOIN

It is the mirror image of LEFT JOIN, and swapping the table order turns one into the other, so most developers use LEFT JOIN only. RIGHT JOIN is fully supported by PostgreSQL.

Older SQLite versions (including the one behind the site editor) do not support it, so it is shown in psql.

Note: Any RIGHT JOIN can be rewritten as a LEFT JOIN by swapping tables.

Example: RIGHT JOIN

bash
shop=# SELECT c.name, o.id AS order_id
shop-# FROM customers c RIGHT JOIN orders o ON o.customer_id = c.id
shop-# ORDER BY o.id;
 name | order_id
------+----------
 Ada  |       10
 Ada  |       11
 Bob  |       12
 (unknown customer_id 99 shows as an empty name)

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
Common Mistakes
  1. Mixing left and right joins confusingly
  2. Forgetting to swap ON conditions
  3. Assuming all databases support it
Chapter Summary
  • RIGHT JOIN keeps all right rows
  • Unmatched left columns are NULL
  • Equivalent to a swapped LEFT JOIN
  • Rarely needed
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.