sql · level 3 · unit 1 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
stacks one result on top of another.
A join puts tables side by side. UNION stacks results instead, like adding one list to the bottom of another. Both queries must give the same number of columns, and the column names come from the first query.
UNION throws away repeated rows, so a pupil in two clubs appears once. keeps every row. It is faster, and it is right whenever repeats matter, such as counting sign-ups.
ORDER BY goes once, at the very end, and sorts the whole stack.
SELECT pupil FROM chess
UNION
SELECT pupil FROM choir
ORDER BY pupil; Ava and Mia are in both clubs, yet each appears only once.
guess first
Leo plays chess and football. How many sign-ups?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
SELECT COUNT(*) AS signups FROM (
SELECT pupil FROM chess
UNION ALL
SELECT pupil FROM football
);
your turn
Your turn. Make one mailing list.
The school wants one list of every pupil in any club, each name once, sorted A to Z. Fill both blanks with the right keyword.
result
Your answer appears here as a table.
fix it
The two halves do not line up. Fix it!
result
Your answer appears here as a table.
- Read the error. Count the columns on each side of
UNION ALL. - The top query gives two columns,
pupilandclub. The bottom one gives onlypupil. - Add , 'football' after pupil on line 3, then press run.
nailed it
you stacked results like a pro. :D
UNIONstacks results and removes repeats.UNION ALLkeeps every row, repeats included.- Both sides need the same number of columns.
ORDER BYgoes last.
new words in your collection
- UNION
- UNION ALL
This lesson isn’t saved. A free account remembers every lesson you finish.