devShark · Quick guide

SQL joins without guesswork

A join combines related rows. INNER JOIN keeps matches; LEFT JOIN also keeps every row from the left side.

How it works

In this query, every learner remains in the result even if they have no passed attempt, because the status condition belongs to ON; putting a.status = 'passed' in WHERE instead would discard the rows whose right-hand values are NULL, and multiple matching attempts can still produce several rows for one learner.

SELECT l.id, a.id AS attempt_id
FROM learners AS l
LEFT JOIN attempts AS a
  ON a.learner_id = l.id
  AND a.status = 'passed';
Read the documentation

Common misconception

A right-table filter in WHERE can accidentally remove unmatched LEFT JOIN rows.

Quick practice

1. Which join preserves every left row?

LEFT JOIN

2. Where is the relationship expressed?

The ON clause.

3. Can joins multiply rows?

Yes.

Practice in a quiz

More guides