1. Home
  2. Database Management Systems
  3. SQL Joins (INNER, LEFT, RIGHT, FULL)

SQL Joins (INNER, LEFT, RIGHT, FULL)

Combine rows from two tables. Watch each join type match rows, fill in NULLs, and build its result table in 3D.

Interactive 3DBeginner11 min readDBMSUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

A JOIN combines rows from two tables using a related column — here Students.id and Enrollments.student_id. The join type decides what happens to rows that have no match. Press Run query.

Pseudocode

  1. SELECT s.id, s.name, e.student_id, e.course
  2. FROM Students s
  3. INNER JOIN Enrollments e
  4. ON s.id = e.student_id;
Read this model as text · 8 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. A JOIN combines rows from two tables using a related column — here Students.id and Enrollments.student_id. The join type decides what happens to rows that have no match. Press Run query.
  2. INNER JOIN on Students.id = Enrollments.student_id. Notice: Ravi (2) and John (4) have no enrollments, and enrollment 5 (AI) has no matching student.
  3. Asha (id 1) matches 2 enrollment rows → 2 result rows.
  4. Ravi (id 2) has no matching enrollment, so INNER JOIN drops this student.
  5. Meena (id 3) matches 1 enrollment row → 1 result row.
  6. John (id 4) has no matching enrollment, so INNER JOIN drops this student.
  7. Enrollment 5 (AI) never matched, so it is not in the result either. INNER JOIN keeps only matching pairs.
  8. INNER JOIN returned 3 rows: only rows that match in both tables. Try the other join types and compare the row counts.

Try this in the 3D model

  • Run INNER JOIN and count the rows. Then run LEFT JOIN. Which extra rows appear?
  • Run RIGHT JOIN. What happens to the enrollment with student_id 5?
  • Run FULL OUTER JOIN. Can you predict the number of rows before pressing Run?

Why joins?

In a normalized database, related data lives in different tables. Student names are in Students, course registrations in Enrollments. To answer “which courses is each student taking?” we join the tables on a related column:

Students.id  =  Enrollments.student_id

The example tables

Students

id name
1 Asha
2 Ravi
3 Meena
4 John

Enrollments

student_id course
1 DBMS
1 OS
3 CN
5 AI

Ravi and John have no enrollments, and enrollment 5 has no matching student — those rows are what make the join types different.

The four main joins

INNER JOIN — only matches

SELECT s.name, e.course
FROM Students s
INNER JOIN Enrollments e ON s.id = e.student_id;

Result: Asha–DBMS, Asha–OS, Meena–CN (3 rows). Unmatched rows on either side are dropped.

LEFT JOIN — all left rows

Every student appears; students without an enrollment get NULL in the course columns. Result: 5 rows (the 3 matches + Ravi and John with NULL).

RIGHT JOIN — all right rows

Every enrollment appears; enrollment 5 gets NULL for the student columns. Result: 4 rows.

FULL OUTER JOIN — everything

All rows from both tables, matched where possible: 6 rows. (MySQL doesn’t support FULL JOIN directly — combine a LEFT and a RIGHT join with UNION.)

Venn-diagram summary

Join Keeps
INNER A ∩ B (matches only)
LEFT All of A, plus matches from B
RIGHT All of B, plus matches from A
FULL OUTER A ∪ B
CROSS Every combination (no ON condition)

Useful patterns

-- Students with no enrollment (an "anti-join")
SELECT s.name
FROM Students s
LEFT JOIN Enrollments e ON s.id = e.student_id
WHERE e.student_id IS NULL;

-- Number of courses per student (0 for nobody)
SELECT s.name, COUNT(e.course) AS courses
FROM Students s
LEFT JOIN Enrollments e ON s.id = e.student_id
GROUP BY s.name;

A self join joins a table with itself (e.g. employees and their managers in the same table).

How databases execute joins

  • Nested loop: for each row of A, scan B — O(n·m), fine for small tables or when B has an index.
  • Hash join: build a hash table on the smaller table, then probe it — O(n + m).
  • Sort-merge join: sort both on the key and merge — great when data is already sorted.

The query optimiser chooses automatically; EXPLAIN shows which one it used.

Common mistakes

  • Forgetting the ON condition → an accidental CROSS JOIN with huge results.
  • Filtering the right table in WHERE after a LEFT JOIN (e.g. WHERE e.course = 'DBMS'), which silently turns it back into an INNER JOIN — put such conditions in the ON clause instead.
  • Comparing with = NULL instead of IS NULL.

Complexity at a glance

Case / operationTimeWhy
Nested loop joinO(n × m)Compare every pair of rows.
Hash joinO(n + m)Build a hash table on one table.
Sort-merge joinO(n log n + m log m)
Extra spaceO(n + m) for hash join

Quick check

Test yourself — pick an answer to see if you got it.

1. Which join returns only rows that have a match in both tables?

2. In a LEFT JOIN, what appears for a left row with no match?

3. Students has 4 rows, Enrollments has 4 rows. How many rows does a CROSS JOIN return?

4. How do you find students who are NOT enrolled in any course?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in SQL Joins (INNER, LEFT, RIGHT, FULL). Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.