RIGHT JOIN & FULL OUTER
Concepts covered: Full Outer Joins
LEFT JOIN protects the table in FROM. RIGHT JOIN protects the other one: every row of the table named after RIGHT JOIN comes out of the join, and the FROM table's columns are padded with NULL where no pair was accepted. To see how little that changes, write the customer report with the tables in the opposite order.
SELECT
c.name,
o.order_id,
o.total
FROM orders o
RIGHT JOIN customers c
ON o.cust_id = c.cust_id| order_id | cust_id | total |
|---|---|---|
| O1 | C1 | 50 |
| O2 | C1 | 30 |
| O3 | C3 | 75 |
| cust_id | name |
|---|---|
| C1 | Alice |
| C2 | Bob |
| C3 | Carol |
| C4 | Dave |
orders is now in FROM and customers is on the protected right side. The result holds the same 5 rows as the LEFT JOIN that opened this lesson. Alice pairs with O1 and O2 and Carol with O3, while Bob and Dave get NULL order columns. A RIGHT JOIN B and B LEFT JOIN A always return the same rows. Because any RIGHT JOIN can be rewritten this way, many teams write LEFT JOIN only. A query that reads "start with the rows I must keep, then attach extras" is faster to check than one where the protected table appears last.
FULL OUTER JOIN protects both tables
Sometimes neither table is the trusted one. Say a users table lists accounts, and a user_sessions table holds one row per user seen in the login system, with the email they signed in with, so each user_id appears in it at most once. You want to know where the 2 disagree. FULL OUTER JOIN returns every accepted pair, then every unmatched left row padded on the right, then every unmatched right row padded on the left.
SELECT u.user_id AS account_id, u.name, s.user_id AS session_user, s.email
FROM users u
FULL OUTER JOIN user_sessions s
ON u.user_id = s.user_id| user_id | name |
|---|---|
| U1 | Alice |
| U2 | Bob |
| U3 | Carol |
| user_id | |
|---|---|
| U1 | alice@co |
| U3 | carol@co |
| U4 | dan@co |
There are 3 accounts and 3 session rows, and the full join returns 4 rows. U1 and U3 appear in both tables, so each forms one pair. Bob's account, U2, has no session, so his row has NULL in both session columns. The session for U4 has no account: its row has NULL in both account columns. Each NULL tells you which side is missing.
| Join type | Pairs | Left-only rows | Right-only rows | Total |
|---|---|---|---|---|
| INNER JOIN | 2 | 0 | 0 | 2 |
| LEFT JOIN | 2 | 1 (U2) | 0 | 3 |
| RIGHT JOIN | 2 | 0 | 1 (U4) | 3 |
| FULL OUTER JOIN | 2 | 1 (U2) | 1 (U4) | 4 |
All 4 join types build the same pairs. They differ only in which unmatched rows they add back. That gives a check for any full join: its row count equals the LEFT JOIN count plus the RIGHT JOIN count minus the INNER JOIN count. Here 3 + 3 - 2 = 4.
warehouse_stock has 5 rows and store_stock has 4 rows. Each has a unique sku, and 3 skus appear in both tables. How many rows does a FULL OUTER JOIN on sku return?
- 9 rows: all 5 warehouse rows followed by all 4 store rows
- 6 rows: 3 pairs, 2 warehouse-only rows and 1 store-only row
- 3 rows: only the skus that both tables hold are returned
- 5 rows: every warehouse row, with store columns NULL for 2
Reading a reconciliation
Raw full-join output is awkward to scan, because the key lives in 2 columns and either may be NULL. 2 additions fix that. COALESCE(u.user_id, s.user_id) returns whichever key is present, giving one filled key column. A CASE expression on the 2 keys labels each row with where it was found.
SELECT
COALESCE(u.user_id, s.user_id) AS user_key,
CASE
WHEN s.user_id IS NULL THEN 'account only'
WHEN u.user_id IS NULL THEN 'session only'
ELSE 'both'
END AS found_in
FROM users u
FULL OUTER JOIN user_sessions s
ON u.user_id = s.user_id
ORDER BY user_key| user_id | name |
|---|---|
| U1 | Alice |
| U2 | Bob |
| U3 | Carol |
| user_id | |
|---|---|
| U1 | alice@co |
| U3 | carol@co |
| U4 | dan@co |
| user_key | found_in |
|---|---|
| U1 | both |
| U2 | account only |
| U3 | both |
| U4 | session only |
The 4 rows now read U1 both, U2 account only, U3 both, U4 session only. Add WHERE u.user_id IS NULL OR s.user_id IS NULL and only the 2 disagreements remain: an anti-join in both directions at once. The same query reconciles any 2 sources that should agree, such as a billing system's invoices against a ledger's entries.
Choose the join by naming the rows you refuse to lose. To keep the rows of one table, use LEFT JOIN with that table in FROM; to keep the rows of both tables, FULL OUTER JOIN; to keep neither, INNER JOIN. RIGHT JOIN adds no new answer to that list, so recognizing it when you read it is enough.
About this lesson
This section is part of the Joins: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.