SQL joins combine rows from two tables by matching a shared column. INNER JOIN returns only the rows that have a match in both tables. LEFT JOIN keeps every row from the left table, RIGHT JOIN keeps every row from the right table, and FULL JOIN keeps every row from both. Missing matches show up as NULL.
The fastest way to see the difference is to run all four on the same data.
What data do the examples use?
The examples use the Northbridge University database in Port Alder. Two tables matter here: student (120 rows) and instructor (20 rows). Each student has an advisor_id that points to an instructor. Eight students have no advisor yet, so their advisor_id is NULL. One instructor, Yuki Cohen, advises nobody.
Those two facts are where the join types differ. To keep the output short, the first queries look at five students only.
What does INNER JOIN return?
INNER JOIN returns only the rows that match on both sides. A student without an advisor disappears from the result.
SELECT s.first_name, s.last_name, i.last_name AS advisor FROM student s INNER JOIN instructor i ON s.advisor_id = i.id WHERE s.id IN (1, 3, 7, 21, 24);
| first_name | last_name | advisor |
|---|---|---|
| Anika | Park | Abara |
| Felix | Abara | Popescu |
Five students went in, two came out. JOIN on its own means INNER JOIN. Use it when you only care about complete pairs.
What is the difference between INNER JOIN and LEFT JOIN?
LEFT JOIN keeps all rows from the table written before the JOIN keyword. Where the right table has no match, its columns are NULL. The only change in the query is the keyword.
SELECT s.first_name, s.last_name, i.last_name AS advisor FROM student s LEFT JOIN instructor i ON s.advisor_id = i.id WHERE s.id IN (1, 3, 7, 21, 24);
| first_name | last_name | advisor |
|---|---|---|
| Anika | Park | Abara |
| Felix | Abara | Popescu |
| Jin | Singh | NULL |
| Mina | Lindqvist | NULL |
| Kenji | Osei | NULL |
All five students are back. Use LEFT JOIN when the left table is your main list and the other table only adds extra details.
How does RIGHT JOIN work?
RIGHT JOIN is a LEFT JOIN seen from the other side: it keeps every row of the right table. Here the right table is instructor.
SELECT i.id, i.last_name, s.first_name AS student FROM student s RIGHT JOIN instructor i ON s.advisor_id = i.id WHERE i.id IN (7, 18);
| id | last_name | student |
|---|---|---|
| 7 | Abara | Anika |
| 7 | Abara | Priya |
| 18 | Cohen | NULL |
Clara Abara appears twice because she advises two students. Yuki Cohen appears once with NULL, because no student points to her. Most people write a LEFT JOIN and swap the table order instead, since it reads from left to right.
What does FULL JOIN return?
FULL JOIN keeps unmatched rows from both sides. You get matched pairs, students without an advisor, and instructors without a student. It is the least common join, but it is useful for comparing two lists.
How many rows does each join return?
Here is the same join on all 120 students and all 20 instructors:
SELECT 'INNER JOIN' AS join_type, COUNT(*) AS result_rows FROM student s INNER JOIN instructor i ON s.advisor_id = i.id UNION ALL SELECT 'LEFT JOIN', COUNT(*) FROM student s LEFT JOIN instructor i ON s.advisor_id = i.id UNION ALL SELECT 'RIGHT JOIN', COUNT(*) FROM student s RIGHT JOIN instructor i ON s.advisor_id = i.id UNION ALL SELECT 'FULL JOIN', COUNT(*) FROM student s FULL JOIN instructor i ON s.advisor_id = i.id;
| join_type | result_rows |
|---|---|
| INNER JOIN | 112 |
| LEFT JOIN | 120 |
| RIGHT JOIN | 113 |
| FULL JOIN | 121 |
The numbers tell the story. 112 students have an advisor. LEFT JOIN adds the 8 who do not (120). RIGHT JOIN adds the one idle instructor to the 112 matches (113). FULL JOIN adds both groups (121).
How do you find rows that have no match?
Use a LEFT JOIN and keep only the rows where the right side is NULL. This finds departments with no students:
SELECT d.name FROM department d LEFT JOIN student s ON s.department_id = d.id WHERE s.id IS NULL;
| name |
|---|
| Data Science and Analytics |
Common mistakes with joins
Filtering the right table in WHERE. This turns your LEFT JOIN into an INNER JOIN. The NULL rows fail the condition and vanish:
SELECT s.first_name, i.last_name AS advisor FROM student s LEFT JOIN instructor i ON s.advisor_id = i.id WHERE s.id IN (1, 3, 7, 21, 24) AND i.title = 'Professor';
Only Anika is returned. If you want all five students and an advisor name only when the advisor is a professor, move the condition into ON:
SELECT s.first_name, i.last_name AS advisor FROM student s LEFT JOIN instructor i ON s.advisor_id = i.id AND i.title = 'Professor' WHERE s.id IN (1, 3, 7, 21, 24);
Now all five rows return. Felix shows NULL because his advisor is a lecturer.
Forgetting the `ON` condition. Without it, many databases pair every row with every row. Always check the row count after a join.
**Counting with COUNT(*) after a LEFT JOIN.** An unmatched row still counts as one row. Count a column from the right table instead, such as COUNT(s.id).
What to read next
- Practice joins in your browser: [Inner join](/learn/sql/inner-join) and [Outer joins](/learn/sql/outer-joins)
- Watch out for duplicated rows: [Join row multiplication](/learn/sql/join-row-multiplication)
- Next topic: [SQL GROUP BY explained](/blog/sql-group-by)
- Browse the whole course: [SQL course](/courses/sql)
- #sql
- #joins
- #database
- #beginners