By LearnAllStudents Editorial Team. Source check dates are listed below.
An original two-table dataset
Scroll the table to see all columns. Keyboard: focus the table area and use the arrow keys.
| students.id | students.name | students.club_id |
|---|---|---|
| 1 | Lina | 10 |
| 2 | Omar | 20 |
| 3 | Ava | NULL |
Scroll the table to see all columns. Keyboard: focus the table area and use the arrow keys.
| clubs.id | clubs.title |
|---|---|
| 10 | Coding |
| 20 | Chess |
The student table has one row per student. club_id identifies the chosen club, while NULL records no club in this example. The club table has a unique id for each club. These are fictional teaching records, not real student data. The relationship and uniqueness rules matter: changing them can change the result even when the query text stays the same.
Inner join: keep the matching pairs
SELECT s.name, c.title
FROM students AS s
INNER JOIN clubs AS c ON s.club_id = c.id
ORDER BY s.id;The output is Lina with Coding and Omar with Chess: two rows. Ava has no matching club_id, so she is absent. Table aliases s and c clarify which table owns each column. The ON condition defines the relationship; joining on equal names rather than the stored identifiers would answer a different question. ORDER BY makes the displayed order explicit rather than relying on incidental storage order.
Left join: keep every student
SELECT s.name, c.title
FROM students AS s
LEFT JOIN clubs AS c ON s.club_id = c.id
ORDER BY s.id;This returns three rows. Ava remains and the club-title column is NULL. NULL is not the text 'NULL', an empty string or the number zero. Use IS NULL to test for it. A left join does not promise exactly one output row per left row: if a left row matches multiple right rows, it produces multiple pairs. Here uniqueness of clubs.id prevents that duplication.
A filter can change the result: WHERE c.title = 'Chess' removes the unmatched NULL row after the join. If the goal is to retain every student while matching only Chess, place the relevant right-table restriction in the ON condition and examine the intended output carefully.
Cross join: every possible pairing
SELECT s.name, c.title
FROM students AS s
CROSS JOIN clubs AS c
ORDER BY s.id, c.id;There are 3×2=6 rows: each student pairs once with each club. Ava appears twice because her stored club_id is irrelevant to a cross join. This query could list possible choices, but it does not show actual membership. If the club table were empty, there would be zero pairs. A missing join condition can produce a large unintended combination, so check counts on a small teaching dataset first.
Try it yourself
- Add a fourth student with club_id 10. What are the inner, left and cross join row counts now?
- Which join answers ‘show every student, even without a club’?
- What condition identifies students without a matching club after the left join?
- Can adding another right-table match increase the output count?
Answers: 3 inner, 4 left and 8 cross rows; left join with students on the left; c.id IS NULL for this dataset; yes, every matching pair can produce a row. Explain why Ava's row changes between join types, then change the dataset and predict again. This is more useful than remembering the names alone.
For parents: use row cards
Write student and club records on separate cards and physically pair them. Ask whether the task is actual membership, every student or every possible choice. Have your child predict the count before revealing the query. Real school data should not be pasted into an online SQL playground for this exercise; the fictional records above are sufficient. The course may require a narrower subset of SQL than this guide, so check the exact specification before exam revision.
How a tutor would approach this
Bring the table definitions, uniqueness constraints and expected output. Trace matching pairs before changing query syntax.
Frequently asked questions
How many rows does a cross join return?
For m left rows and n right rows it returns m×n row pairs, before any later filtering.
Does a left join keep unmatched rows?
It preserves unmatched left rows, filling right-table columns with NULL. Later filters can remove those rows.
Is NULL equal to zero?
No. It represents missing or unknown information and is tested using IS NULL in this example.
Can a join create more rows than either table?
Yes. Multiple matches and cross joins can produce more result rows than either input.
Official sources
- PostgreSQL official tutorial: joins between tables (checked 2026-11-27)
- PostgreSQL table expressions: cross joins and filter placement (checked 2026-11-27)
Related pages
Try a free 30-minute demo
Work through a question with a tutor. No payment needed.
Request a free 30-minute demo