Union vs Join
JOIN adds columns (wider row): name next to marks. UNION stacks rows (longer list): 2024 names plus 2025 names. JOIN needs a key. UNION needs the same number of columns and similar types.
UNION removes duplicates. UNION ALL keeps them and is faster. Column names in the result come from the first SELECT.
Trap: UNION when you meant JOIN (you get two lists, not a combined report). Trap: JOIN when you meant UNION (you invent a fake key).
On the example next to this theory — Union vs Join — jOIN combines rows using a match condition between tables.
students.id
=
marks.student_id
│
▼
one result rowExam tip
JOIN widens. UNION lengthens. Draw both.