← Back to SQL, page 3

sql-0048 · Easy fresher

How do INNER JOIN and LEFT JOIN results differ? Show it with students and their enrolments.

An INNER JOIN returns only rows that have a match on both sides. A LEFT JOIN returns every row from the left table, and fills the right-side columns with NULL when there is no match. So a student with no enrolments disappears from an inner join but appears once, with NULLs, in a left join.

Full answer

A join combines rows from two tables using a condition, usually "this key equals that key".

sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);

With an INNER JOIN, a student appears only if at least one enrolment matches. Dara has none, so she is not in the result:

sql
SELECT s.name, e.course_id, e.marks
FROM students s
INNER JOIN enrolments e ON e.student_id = s.id
ORDER BY s.name, e.course_id;
namecourse_idmarks
Asha1088
Asha2092
Ben1075
Ben30NULL
Chen1064
Chen2071
Chen3080
Eli2055

With a LEFT JOIN, every student appears. Dara's row has NULL in the enrolment columns because nothing matched:

sql
SELECT s.name, e.course_id, e.marks
FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id
ORDER BY s.name, e.course_id;
namecourse_idmarks
Asha1088
Asha2092
Ben1075
Ben30NULL
Chen1064
Chen2071
Chen3080
DaraNULLNULL
Eli2055

Read the table names as a sentence: "students LEFT JOIN enrolments" means "all students, plus their enrolments if any". The table written first is the one whose rows are all kept.

Notice that Ben's Statistics row also shows marks NULL, but for a different reason: the enrolment exists and the mark is missing. To tell the two cases apart, test a column that can never be NULL in a real match, such as the right table's key: e.course_id IS NULL means "no enrolment at all".

A practical rule: use INNER JOIN when the related row must exist for the result to make sense, and LEFT JOIN when the left rows must all be listed, such as "every student with their marks, if any".

sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);

SELECT s.name, e.course_id, e.marks
FROM students s
INNER JOIN enrolments e ON e.student_id = s.id
ORDER BY s.name, e.course_id;

SELECT s.name, e.course_id, e.marks
FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id
ORDER BY s.name, e.course_id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • INNER JOIN keeps only matched rows
  • LEFT JOIN keeps every left row, with NULLs where nothing matched
  • The table written first is the preserved side in a LEFT JOIN
  • Check the right table's key to detect 'no match' rather than a nullable column

Common mistakes

  • Using INNER JOIN for a report that must list every student
  • Confusing a NULL from 'no match' with a NULL stored in the right table

Follow-ups

  • How do you find students who have no enrolments?
  • What happens if you add WHERE e.marks > 50 to the LEFT JOIN?

Tips

  • Name one row that appears in one result but not the other; it proves you read the data