EZ TuitionsAll tuitions

MySQL INNER JOIN vs LEFT JOIN: Trace Two Tables

An INNER JOIN keeps matching pairs. A LEFT JOIN also keeps a left-table row with no match, filling the right-table columns with NULL. On these supplied tables, the basic inner join returns three rows and the basic left join returns four, not one row per student.

Predict five SELECT results before reading the explanations. All names and rows are invented. This is a read-only reasoning exercise, not a setup tutorial or a physical query-plan trace.

Read both complete supplied tables

Student.StudentID is a unique non-NULL integer. StudentName is non-NULL text. Attempt.AttemptID is a unique non-NULL integer; its StudentID and Score are also non-NULL integers here. Multiple attempts may refer to the same student. Every supplied attempt refers to a supplied student; Charu has no attempt.

Student: all three original rows
StudentIDStudentName
11Asha
12Bilal
13Charu
Attempt: all three original rows
AttemptIDStudentIDScore
5011180
5021140
5031250

Match IDs, then account for missing pairs

  1. Take one Student row and find every Attempt row satisfying the entire ON condition.
  2. Write one result row per qualifying pair. Repeated student details are expected when several attempts match.
  3. For LEFT JOIN only, if no attempt qualifies, write one row retaining the student with NULL right-side columns.
  4. Apply WHERE, if present, to those joined rows. Keep only rows where its condition is true.
  5. Display the selected four columns in the explicit ORDER BY order.

This is a model for deriving the answers, not a promise that MySQL physically processes the query in that sequence. The aliases s and a name the Student and Attempt tables within each query.

Predict and check five joined results

INNER JOIN: keep each matching pair

SELECT s.StudentID, s.StudentName, a.AttemptID, a.Score
FROM Student AS s
INNER JOIN Attempt AS a
  ON s.StudentID = a.StudentID
ORDER BY s.StudentID, a.AttemptID;

Expected columns: StudentID | StudentName | AttemptID | Score

11 | Asha | 501 | 80
11 | Asha | 502 | 40
12 | Bilal | 503 | 50

Asha matches two different attempt rows, so she appears twice. Bilal matches one. Charu has no matching Attempt.StudentID and is absent. A join does not automatically reduce each student to one result row.

LEFT JOIN: also keep the student without a match

SELECT s.StudentID, s.StudentName, a.AttemptID, a.Score
FROM Student AS s
LEFT JOIN Attempt AS a
  ON s.StudentID = a.StudentID
ORDER BY s.StudentID, a.AttemptID;

Expected columns: StudentID | StudentName | AttemptID | Score

11 | Asha | 501 | 80
11 | Asha | 502 | 40
12 | Bilal | 503 | 50
13 | Charu | NULL | NULL

The three matched pairs remain. Charu contributes one extra row with NULL for the right-table columns. Her own StudentID and StudentName are retained. These NULLs describe an absent matching row, not a zero score or a new stored attempt.

Score threshold in ON: keep every student

SELECT s.StudentID, s.StudentName, a.AttemptID, a.Score
FROM Student AS s
LEFT JOIN Attempt AS a
  ON s.StudentID = a.StudentID AND a.Score >= 60
ORDER BY s.StudentID, a.AttemptID;

Expected columns: StudentID | StudentName | AttemptID | Score

11 | Asha | 501 | 80
12 | Bilal | NULL | NULL
13 | Charu | NULL | NULL

Only attempt 501 satisfies both the ID equality and the score threshold. Bilal has an attempt, but none that satisfies this ON condition. Bilal and Charu each receive one NULL-extended row. Asha does not receive another NULL row for her rejected attempt 502, because she already has a qualifying match.

The same threshold in WHERE: only the high-scoring pair remains

SELECT s.StudentID, s.StudentName, a.AttemptID, a.Score
FROM Student AS s
LEFT JOIN Attempt AS a
  ON s.StudentID = a.StudentID
WHERE a.Score >= 60
ORDER BY s.StudentID, a.AttemptID;

Expected columns: StudentID | StudentName | AttemptID | Score

11 | Asha | 501 | 80

First reason from the unfiltered LEFT JOIN result. WHERE keeps only rows whose right-side score is at least 60. The 40 and 50 fail, and the NULL score for Charu is not true for this comparison. Bilal and Charu therefore disappear. This query does not answer the request to keep every student.

Find students with no attempt at all

SELECT s.StudentID, s.StudentName, a.AttemptID, a.Score
FROM Student AS s
LEFT JOIN Attempt AS a
  ON s.StudentID = a.StudentID
WHERE a.AttemptID IS NULL
ORDER BY s.StudentID, a.AttemptID;

Expected columns: StudentID | StudentName | AttemptID | Score

13 | Charu | NULL | NULL

AttemptID is declared unique and non-NULL in every real attempt. After joining only on StudentID, its NULL value identifies the missing right-side row. Bilal is not included because attempt 503 exists, even though its score is below 60. Testing Score IS NULL would be unsafe for this purpose if real attempts were allowed to have missing scores.

Do not repair a different question

Moving a right-table condition from WHERE into ON changes the question for these left joins. Use the ON version when the request is every student with qualifying attempts if any. Use the WHERE version when only qualifying joined rows are wanted. Neither is a universal repair for the other.

Do not add DISTINCT merely to hide repeated names: attempts 501 and 502 are different records. Do not replace a missing attempt with score zero, and do not test NULL with equals. See the IS NULL supplied-table exercise for missing-value comparisons, or the WHERE and HAVING exercise for filtering before and after grouping.

Scope and limits

MySQL 8.4 documentation is the reference for this original practice. This is not an official exam question, an answer-key guarantee or a claim that a particular syllabus requires this server version. Queries are SELECT-only. There is no installation, data editing, NATURAL JOIN, aggregation, performance recommendation or live database connection.

No native MySQL execution was observed. Expected rows follow from the supplied tables and documented join semantics. ORDER BY fixes their shown order; the join itself does not promise that order. The label NULL denotes SQL NULL, not the text string "NULL".

Frequently asked questions

Why does Asha appear twice?

Two supplied attempts have StudentID 11. Each satisfies the equality with Asha, so each produces a distinct matching pair. Neither query uses DISTINCT or grouping.

Does LEFT JOIN always return exactly one row per student?

No. It returns each qualifying matching pair, plus a NULL-extended row for a left-table row with no qualifying match. On these data the basic LEFT JOIN has four result rows for three students.

Are ON and WHERE interchangeable here?

No. With LEFT JOIN, the ON threshold changes which attempts match while preserving each student before later filtering. The WHERE threshold filters the joined rows and removes rows with a NULL right-side score.

Why use AttemptID IS NULL to find missing matches?

A real supplied attempt cannot have a NULL AttemptID. With the ID-only join, a NULL right-side AttemptID therefore marks a missing attempt. Do not generalize that test to a right-side column that can be NULL in real rows.

Were these queries executed on MySQL?

No native MySQL execution was observed. This is original documentation-derived practice with independently calculated expected rows, not a server transcript or a claim about physical execution order.

Sources and limits

Primary documentation checked 27 November 2026. Table rows, questions and explanations are original EZ Tuitions practice.