Module 5 · Advanced queries

Combine results with UNION

By the end you will be able to put together two lists and decide whether to keep repeats.

About 12 min

0 of 52 lessons

Before you start, you need

Practice: choose between subqueries and CTEs

Combine lists that represent the same thing

You have one list of customers with pending orders and another with shipped orders. You want to combine them into one. UNION stacks the rows of one list with those of the other; it does not look for matching pairs like a JOIN.

The queries must return the same number of columns in compatible positions. In this case both return customer_id: the values represent the same thing. Mixing cities and departments in one column would not produce a useful list.

UNION removes repeated rows. A customer who is in both lists appears once. UNION ALL keeps every occurrence, including several orders from the same customer.

Choose according to the question: a list of distinct customers, or every occurrence. The column headings come from the first query, and an ORDER BY at the end sorts the whole list.

Minimal syntax
SELECT column_name FROM ...
UNION
SELECT column_name FROM ...;

See it in action

Which customers have pending or shipped orders?

SQL
SELECT customer_id
FROM orders
WHERE status = 'pending'
UNION
SELECT customer_id
FROM orders
WHERE status = 'shipped'
ORDER BY customer_id;
Example result: 8 of 10 rows
customer_id
1
2
3
4
5
7
8
9
  1. Each branch produces customer ids. They have one column and the same meaning.
  2. UNION combines the lists and keeps each id only once, even if a customer has several orders.
  3. UNION ALL keeps every occurrence. It is useful when the question needs to represent each order or each source.
  4. The final ORDER BY sorts the combined result. The column names come from the first branch.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Combine customers from two lists

Not started

Use UNION to combine the customer_id values from pending and shipped orders. Return customer_id without repeats and sort by customer_id.

Expected result: 10 rows with the column customer_id, in that order. Keep the row order requested.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Keep every occurrence

Not started

Combine customer_id from pending and shipped orders with UNION ALL. Return customer_id and sort by it.

Expected result: 15 rows with the column customer_id, in that order. Keep the row order requested.

View tables and data

Open this panel to see the full data.

Key takeaways

  • The branches need the same number of columns and a compatible meaning.
  • UNION removes repeats.
  • UNION ALL keeps every occurrence.
  • The final ORDER BY goes after the branches.

Next: Create categories with CASE