RIGHT JOIN
Keep every row from the right table and add matches from the left — and why most people rewrite it as a LEFT JOIN.
"List every IPL franchise and the players we have for it — even franchises with no players yet." Here the table you must not lose rows from is teams. If you've already written FROM players, a RIGHT JOIN keeps every team.
Definition
RIGHT JOIN (also RIGHT OUTER JOIN) returns all rows from the right table (the one after
RIGHT JOIN), plus the matching rows from the left table. Where a right row has no match, the left table's columns are NULL.
- It is the mirror image of LEFT JOIN: now the right table is the primary source.
- In Venn terms: the whole right circle B, including the overlap.
- Rows of the left table that have no partner are lost.
Syntax
sql
SELECT a.column_name, b.column_name
FROM table_a AS a
RIGHT JOIN table_b AS b -- all rows of table_b are kept
ON a.key_column = b.key_column;Example — all teams, with players where available
sql
SELECT p.name, p.team, t.team AS t_team, t.team_name
FROM players AS p
RIGHT JOIN teams AS t
ON p.team = t.team;| name | team | t_team | team_name |
|---|---|---|---|
| Virat Kohli | RCB | RCB | Royal Challengers Bengaluru |
| Rohit Sharma | MI | MI | Mumbai Indians |
| Jos Buttler | RR | RR | Rajasthan Royals |
| MS Dhoni | CSK | CSK | Chennai Super Kings |
| David Warner | DC | DC | Delhi Capitals |
| Jasprit Bumrah | MI | MI | Mumbai Indians |
| NULL | NULL | KKR | Kolkata Knight Riders |
| NULL | NULL | GT | Gujarat Titans |
How it works: every row of teams (B) is kept. KKR and GT have no players, so the player columns (name, team) are NULL. Notice p.team is NULL but t.team shows the code — that's why we select both: t_team tells you which team had no partner.
Order of tables IS important — players without a team are lost
If the setup adds Shubman Gill (team 'GT2') and Ishan Kishan (team NULL), the RIGHT JOIN above still returns the same 8 rows: Gill and Kishan are in the left table and have no matching team, so RIGHT JOIN drops them.
Alternative to RIGHT JOIN — swap the tables and use LEFT JOIN
| Option 1 (RIGHT JOIN) | Option 2 (swap tables + LEFT JOIN) |
|---|---|
FROM players AS p RIGHT JOIN teams AS t ON p.team = t.team | FROM teams AS t LEFT JOIN players AS p ON p.team = t.team |
Same results! Both keep all teams and add players where they match. Only the default column order of SELECT * changes (players' columns first vs teams' columns first) — if you list columns explicitly, the output is identical.
sql
SELECT t.team, t.team_name, p.name
FROM teams AS t
LEFT JOIN players AS p
ON p.team = t.team;Tip: Most teams and style guides prefer LEFT JOIN only. Reading every query as "main table first, then extras" is easier than switching direction in your head. Learn RIGHT JOIN so you can read other people's code, but write LEFT JOIN.
Common mistakes
- ❌ Expecting RIGHT JOIN to keep all rows of the table after
FROM. ✅ It keeps all rows of the table after RIGHT JOIN. - ❌ Mixing LEFT and RIGHT in a multi-table query — very hard to reason about. ✅ Rewrite everything as LEFT JOINs from one main table.
- ❌ Reading the key from the left table in the output (
p.team) — it's NULL for unmatched rows. ✅ Show the right table's key (t.team).
SQL Server vs MySQL / PostgreSQL
RIGHT JOIN is supported by SQL Server, MySQL, PostgreSQL and (since version 3.39) SQLite. Older SQLite versions did not support it — another reason the swap-to-LEFT trick is worth knowing.
Interview tip
Q: Can every RIGHT JOIN be written as a LEFT JOIN?
A: Yes. A RIGHT JOIN B ON … is exactly B LEFT JOIN A ON … — just swap the table order.
Key points to remember
- RIGHT JOIN keeps ALL rows from the right table and adds matching rows from the left table.
- Unmatched right rows get NULL in the left table's columns; unmatched left rows are lost.
- RIGHT JOIN is the mirror image of LEFT JOIN — order of tables matters.
- A RIGHT JOIN B is the same as B LEFT JOIN A: swap the tables to get the same result.
- Prefer LEFT JOIN in your own code; it reads as 'main table first, extras after'.
- Select the right table's key (t.team) to see which rows had no partner.
Try it yourself
RIGHT JOIN: all teams, players where they exist
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Same result with swapped tables + LEFT JOIN
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Players without a team are lost in a RIGHT JOIN
This example runs a small setup step first — click to see it
INSERT INTO players (id, name, team, country, runs) VALUES (7, 'Shubman Gill', 'GT2', 'India', 600), (8, 'Ishan Kishan', NULL, 'India', 450);
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: rewrite as a LEFT JOIN
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
