TheAnalyst / SQLAll lessons ↗
Day 17 · Advanced SQL · 10–20 minutes

UNION and set operations

UNION combines compatible result sets and removes duplicates; UNION ALL keeps duplicates.

01 · Learn

The idea

UNION combines compatible result sets and removes duplicates; UNION ALL keeps duplicates. Read the query line by line and identify the input table, the transformation, and the shape of the result before running it.

SELECT country AS value FROM customers WHERE country = 'India' UNION SELECT country AS value FROM customers WHERE country = 'UAE' ORDER BY value;
Read it actively

Before running the example, predict the output columns and whether the query returns one row, many rows, or possibly no rows.

02 · Recall

Explain it without looking

In your own words, explain the main SQL feature in today’s lesson and why it answers the question.

03 · Solve

Run your own query

Combine the distinct India and UAE country values into one result.

Loading browser SQL engine…
04 · Repeat

Close the loop