Querydrill

Level 3: Joins Exercise 63 of 161 Easy Combine and compare sets

Stack two lists with UNION ALL

An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.

Try it yourself

Build one contact list: the name of every employee and every customer, with a second column that says employee or customer.

employees (11 rows)

idnamedept_idsalaryhire_datemanager_id
1Ava21300002019-03-15NULL
2Ben21050002020-07-011
3Cleo1820002021-01-107
4Dan1760002022-05-237
5Eve3540002022-09-1211
6Finn2980002021-11-301
7Gia1990002018-06-04NULL
8Hugo3610002020-02-18NULL
9Iris4880002019-10-07NULL
10Jack4720002023-04-179
11KaiNULL500002024-01-088

customers (8 rows)

idnamecountrysignup_date
1AnnaNL2024-01-05
2BrunoBR2024-02-11
3ChloeUS2024-03-02
4DevIN2024-03-19
5ElifTR2024-05-27
6FarahUS2024-06-14
7GusNL2024-08-01
8HanaJP2024-09-09

Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.

What this exercise teaches

UNION ALL puts the rows of two queries under each other. Plain UNION also removes duplicate rows, which costs a sort and can hide rows you wanted, so use UNION ALL unless you need the duplicates gone.

A hint if you get stuck

Write two SELECTs with the same number of columns and put UNION ALL between them. The second column is a fixed text value in each SELECT.

Background you need

Common mistakes on this exercise

  • Labels swapped.
  • Customers missing.

Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.

Keep going on the practice page

Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.

Open this exercise

More exercises in Joins