SQL JOINs Explained With Examples
INNER, LEFT, RIGHT and FULL JOIN explained with two tiny tables, the exact results of each query, how to find unmatched rows, and the mistakes that trip people up.
Short answer: SQL JOINs combine rows from two tables by matching a column, usually a key. INNER JOIN keeps only matching rows, LEFT JOIN keeps every row from the left table, RIGHT JOIN keeps every row from the right table, and FULL JOIN keeps everything from both. The examples below use two tiny tables you can run yourself.
What Is a SQL JOIN and Why Do We Need It?
Good databases split data across tables so nothing is repeated: students in one table, their course enrolments in another. A JOIN puts the pieces back together for a query. If you are new to the language, start with what SQL is.
Our sample data (every example was tested in SQLite):
CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE enrolments (id INTEGER PRIMARY KEY, student_id INTEGER, course TEXT);
INSERT INTO students VALUES (1, 'Asha'), (2, 'Ben'), (3, 'Chitra');
INSERT INTO enrolments VALUES
(1, 1, 'Python'), (2, 1, 'SQL'), (3, 2, 'Python'), (4, 99, 'Maths');
Ben has one enrolment, Chitra has none, and the last enrolment points to student 99, who does not exist.
What Does INNER JOIN Return?
SELECT s.name, e.course
FROM students s
INNER JOIN enrolments e ON e.student_id = s.id;name | course |
|---|---|
Asha | Python |
Asha | SQL |
Ben | Python |
Only rows with a match on both sides appear. Chitra (no enrolment) and the Maths row (no student) are dropped. Asha appears twice because she has two enrolments.
What Does LEFT JOIN Return?
Change INNER JOIN to LEFT JOIN and you also get every student without a match, with NULL in the missing columns.
name | course |
|---|---|
Asha | Python |
Asha | SQL |
Ben | Python |
Chitra | NULL |
LEFT JOIN is the most useful join in practice. It answers questions such as "which students have no enrolment?":
SELECT s.name
FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id
WHERE e.id IS NULL;
That returns Chitra. The same join with COUNT lists every student with their number of courses, including 0 (Asha 2, Ben 1, Chitra 0):
SELECT s.name, COUNT(e.id) AS courses
FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id
GROUP BY s.id;
What Are RIGHT JOIN and FULL JOIN?
RIGHT JOIN is a LEFT JOIN with the tables swapped: it keeps every row from the right table, so the Maths enrolment appears with a NULL name. Most people simply reorder the tables and use LEFT JOIN. FULL JOIN (also called FULL OUTER JOIN) returns all of the rows above: the matches, Chitra with NULL, and Maths with NULL.
Support differs between databases. Recent SQLite and PostgreSQL support all four; MySQL has no FULL JOIN, so people combine a LEFT and a RIGHT JOIN with UNION. See MySQL vs PostgreSQL vs SQLite.
Which JOIN Should You Use?
You want | Use |
|---|---|
Only rows that match in both tables | INNER JOIN |
All rows from the main table, matched or not | LEFT JOIN |
Rows in the main table with no match | LEFT JOIN ... WHERE right.id IS NULL |
All rows from both tables | FULL JOIN |
Common Mistakes
Filtering a LEFT JOIN in WHERE. A condition like
WHERE e.course = 'Python'removes the NULL rows and turns your LEFT JOIN into an INNER JOIN. Put the condition in theONclause to keep unmatched rows.Forgetting the ON condition. Without it you get every combination of rows (a cross join).
Unexpected duplicates. One-to-many matches repeat the left row; this is correct, but it inflates totals if you sum the wrong column. Our lesson on the duplicate row trap explains it.
Using NULL with =. Test for missing matches with
IS NULL, not= NULL.
Interview Tip
Interviewers often ask "find customers who never ordered". Say LEFT JOIN with IS NULL on the right table's key, then mention that NOT EXISTS is an alternative. For practice, work through the chapters on INNER JOIN and aliases and LEFT, RIGHT, FULL, CROSS and self joins.
Key Takeaway
INNER keeps matches, LEFT keeps all of the left table, RIGHT keeps all of the right, FULL keeps both. Choose by asking which table's rows you must never lose, and always check the row count after a join.
Ready to start? Practise joins, filters and aggregates on a real practice database with step-by-step lessons. Begin with the free course SQL and Data Handling for AI: Beginner.
Related articles
More in SQL and Data
SQL Window Functions Explained: ROW_NUMBER, RANK and More
SQL window functions add a calculated value to every row without collapsing rows like GROUP BY. See ROW_NUMBER, RANK and DENSE_RANK side by side with real result rows.
6 min readNov 28, 2026SQL vs Excel: When to Use Each
A clear comparison of SQL and Excel: what each is good at, the same task done in both, and how to decide which to use or how to combine them.
4 min readNov 24, 2026NULL in SQL: 5 Common Mistakes and Fixes
NULL means unknown, not zero. Learn the five SQL NULL mistakes beginners make, from = NULL to NOT IN, and the exact fix for each, with a table you can run yourself.
5 min readNov 21, 2026SQL vs Python for Data Analysis
SQL and Python do different jobs in data analysis. See what each is best at, a side-by-side example, and which one to learn first.
4 min readNov 15, 2026Primary Key vs Foreign Key in SQL
What primary and foreign keys are, how they differ, and how a runnable customers-and-orders example shows the database enforcing them.
4 min readNov 13, 2026WHERE vs GROUP BY vs HAVING in SQL
WHERE filters rows, GROUP BY builds groups and HAVING filters groups. See one worked example, the order SQL runs them in, and the mistakes to avoid.
3 min readNov 10, 2026
Comments
Sign in to join the discussion.

