# LEFT, RIGHT & FULL Joins

> Keep rows that have no partner with LEFT, RIGHT and FULL joins, and learn why ON and WHERE behave differently.

Subject: SQL | Lesson 14 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/sql/outer-joins
Previous: [INNER JOIN](https://codewithanimation.in/sql/inner-join.md) | Next: [Self & Cross Joins](https://codewithanimation.in/sql/self-and-cross-joins.md)

## Outer joins keep the lonely rows

An outer join works like an inner join, then adds back the rows that found no partner and fills the missing side with `NULL`.

`LEFT JOIN` keeps every row of the table written before the word JOIN. `RIGHT JOIN` keeps every row of the table written after it. `FULL JOIN` keeps both. The word `OUTER` is optional, so `LEFT JOIN` and `LEFT OUTER JOIN` are the same.

## Show everyone, even with nothing to show

Reports often need every customer, including the ones with no tickets yet. An inner join would hide them. An outer join lists them with `NULL` so you can see who is missing.

## List every customer with their tickets

```sql
SELECT c.name, t.topic
FROM customers AS c
LEFT JOIN tickets AS t
  ON c.id = t.customer_id;
```

Asha has two tickets, Ben has one, and Chen, Dara and Eli have none, so they get `NULL` as the topic. That is 6 rows. A `RIGHT JOIN` here would keep the 4 tickets instead, and ticket 204, with no customer, would show `NULL` as the name. A `FULL JOIN` keeps both, giving 7 rows.

To find only the customers with no tickets, add `WHERE t.id IS NULL`. The padded rows are the only ones where the ticket id is NULL.

## Common mistake: WHERE can undo your outer join

A test on the right table in `WHERE` runs after the join. The padded rows have `NULL` there, the test is unknown, and those rows are removed. Your `LEFT JOIN` quietly turns into an `INNER JOIN`.

Put the test in `ON` instead: `ON c.id = t.customer_id AND t.topic = 'Login'`. Now the test only decides which tickets match, and every customer stays. For outer joins, `ON` and `WHERE` are not interchangeable.

## Tip: Prefer LEFT, and filter the right side in ON

Write the table you want to keep on the left and use `LEFT JOIN`, so you rarely need `RIGHT`. Put conditions on the right table in `ON`, and use `WHERE` for conditions on the left table.

## Try it: Switch the join and the test

Step through the four join types, then try the three extra tests. Compare WHERE with ON for the Login topic.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/outer-joins

## Check yourself

### Question 1

`customers` has 5 rows. Asha has 2 tickets, Ben has 1, the other three have none. How many rows does `customers LEFT JOIN tickets` return?

- A. 6
- B. 3
- C. 5

Correct answer: A

Asha makes 2 rows, Ben 1, and Chen, Dara and Eli one row each with NULL. That is 6.

- Why not B: 3 is the INNER JOIN count. LEFT JOIN also keeps the three customers with no tickets.
- Why not C: Asha has two tickets, so she makes two rows. The total is 6.

### Question 2

How do you list only the customers that have no tickets?

- A. LEFT JOIN tickets, then WHERE t.id IS NULL
- B. INNER JOIN tickets, then WHERE t.id IS NULL
- C. LEFT JOIN tickets, then WHERE t.id = NULL

Correct answer: A

LEFT JOIN pads customers with no ticket with NULL, and IS NULL picks exactly those rows.

- Why not B: An INNER JOIN has no rows with a missing ticket, so nothing is left to find.
- Why not C: = NULL is never true. Use IS NULL.

### Question 3

A LEFT JOIN is followed by `WHERE t.topic = 'Login'`. What happens to customers with no tickets?

- A. They are removed, because their topic is NULL and the test is not true
- B. They stay, because LEFT JOIN keeps every customer
- C. The query raises an error

Correct answer: A

WHERE runs after the join. NULL = 'Login' is unknown, so the padded rows are filtered out. Move the test into ON to keep them.

- Why not B: LEFT JOIN keeps them during the join, but WHERE runs afterwards and removes them.
- Why not C: The query is valid. It just behaves like an INNER JOIN.

## Remember this

- LEFT JOIN keeps every left row, RIGHT keeps every right row, FULL keeps both, and missing sides become NULL.
- LEFT JOIN plus WHERE right_key IS NULL finds the left rows that have no partner.
- A WHERE test on the right table removes the padded rows and turns a LEFT JOIN into an INNER JOIN.
- To filter the right table and still keep every left row, put the test in ON.
