Concept · Combining tables

RIGHT JOIN

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

Before continuing: This entry is part of the JOIN guide. If you are new to the topic, start with JOIN.

RIGHT JOIN example

All three goals appear because they are on the right. Support has no progress recorded and gets NULL. The progress row with id 4 disappears because it has no related goal on the right.

Result verified in SQLite 3.46
idnamepercentage
1Sales80
2SupportNULL
3Legal50
Keeping every goal
WITH targets(id, name) AS (
  VALUES (1, 'Sales'), (2, 'Support'), (3, 'Legal')
), progress(target_id, percentage) AS (
  VALUES (1, 80), (3, 50), (4, 20)
)
SELECT o.id, o.name, a.percentage
FROM progress AS a
RIGHT JOIN targets AS o
  ON a.target_id = o.id
ORDER BY o.id;

Check the result with small data

Support has no progress recorded yet.

Self-contained query · SQLite
WITH targets(id, name) AS (
  VALUES (1,'Sales'), (2,'Support')
), progress(target_id, percentage) AS (VALUES (1,80))
SELECT o.name, a.percentage
FROM progress a RIGHT JOIN targets o ON a.target_id=o.id
ORDER BY o.id;
Query result
namepercentage
Sales80
SupportNULL

RIGHT JOIN keeps every row from the right table (targets), including Support. This example can be rewritten as targets LEFT JOIN progress.

Check your understanding: Which table would you put on the left to use LEFT JOIN?

Show answer

targets: FROM targets AS o LEFT JOIN progress AS a ON a.target_id = o.id. The table you want to keep in full goes on the left.

Common mix-ups with RIGHT JOIN

Losing track of which table is kept

In long queries, reading from right to left adds mental effort. If your team mostly works with LEFT JOIN, swap the tables and keep one consistent reading direction.

The change is not just swapping a keyword: you must also swap the order of the tables and keep the condition correct.

Key differences between engines

PostgreSQL, MySQL and SQL Server support RIGHT JOIN. SQLite added it in version 3.39.0; older installations need the equivalent rewrite with LEFT JOIN. Before publishing a query, check which version of the engine will really run it.

Changing the direction without changing which rows are kept

A right join keeps the table written on the right. You can express an equivalent relationship by swapping the tables and using a left join, but you must keep the meaning of the condition and check the selected columns. SELECT * can change the column order when you swap the sources, even though the pairs represent the same thing.

Choose the form that makes the kept set easy to see: if the report starts from every category, put categories on the left and use LEFT JOIN to make that goal visible. Neither direction has a universal performance advantage; the plan depends on the engine, the statistics and the full query.

Test a category with no products and one with several. The first must be kept with no product, and the second must produce its pairs, not one row per category. To count products, count a required key from the optional side. Review the filters after swapping: a condition on a product in WHERE can remove the empty category and defeat the purpose of keeping every category.

Technical questions: PostgreSQL: Table Expressions and Joins (opens in a new tab).