Combine result sets with set operators
Set operators combine compatible query results vertically. They answer questions about membership and duplicates, rather than matching columns as a join does.
Understand the idea
Imagine two lists of customer IDs. A join puts related information side by side. A set operator compares or stacks the lists. Choose UNION ALL for preserving events and UNION only when removing duplicate result rows is intended.
| Concept | What it means |
|---|---|
| UNION ALL | Keep every row from both results, including duplicates. |
| UNION | Keep distinct rows appearing in either result. |
| INTERSECT | Keep distinct rows common to both results. |
| MINUS | Keep distinct rows from the left result absent on the right. |
A concrete example
Look at each OP group separately: UNION ALL has 1,1,2,2,3; UNION has 1,2,3; INTERSECT has 2; MINUS has 1.
Remember: UNION ALL preserves every row. Removing duplicates is a separate business decision, not an automatic improvement.
Explain it step by step · full theory
Let us understand the idea
Imagine two lists of customer IDs. A join puts related information side by side. A set operator compares or stacks the lists. Choose UNION ALL for preserving events and UNION only when removing duplicate result rows is intended.
The compared SELECT lists must have compatible positions and data types. A matching column name does not rescue a query where name and amount were accidentally swapped between the two branches.
Reason through the SQL
For A = [1,1,2] and B = [2,3], UNION ALL returns five rows, UNION returns 1,2,3, INTERSECT returns 2 and A MINUS B returns 1. MINUS is directional: B MINUS A returns 3.
Use parentheses when mixing operators, especially INTERSECT with UNION or MINUS. Put the final ORDER BY after the combined query so the displayed result has a defined order.
Words used in this lesson
- UNION ALL
- Keep every row from both results, including duplicates.
- UNION
- Keep distinct rows appearing in either result.
- INTERSECT
- Keep distinct rows common to both results.
- MINUS
- Keep distinct rows from the left result absent on the right.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Combine result sets with set operators
Begin with the question
Set operators combine compatible query results vertically. They answer questions about membership and duplicates, rather than matching columns as a join does.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Begin with the question
Set operators combine compatible query results vertically. They answer questions about membership and duplicates, rather than matching columns as a join does.
- Choose the operation
Imagine two lists of customer IDs. A join puts related information side by side. A set operator compares or stacks the lists. Choose UNION ALL for preserving events and UNION only when removing duplicate result rows is intended.
- Check the meaning
UNION ALL preserves every row. Removing duplicates is a separate business decision, not an automatic improvement.
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
Read first, then run manually in an authorized Snowflake learning session with a warehouse. Each WITH/VALUES example includes its own inputs and creates no stored tables. Expected results are illustrative, not live execution.
Compare membership and duplicate handling
WITH a AS (SELECT COLUMN1 AS ID FROM VALUES (1),(1),(2)),
b AS (SELECT COLUMN1 AS ID FROM VALUES (2),(3))
SELECT 'UNION ALL' AS OP, ID FROM (SELECT ID FROM a UNION ALL SELECT ID FROM b)
UNION ALL
SELECT 'UNION', ID FROM (SELECT ID FROM a UNION SELECT ID FROM b)
UNION ALL
SELECT 'INTERSECT', ID FROM (SELECT ID FROM a INTERSECT SELECT ID FROM b)
UNION ALL
SELECT 'MINUS', ID FROM (SELECT ID FROM a MINUS SELECT ID FROM b)
ORDER BY OP, ID;What to expect and why
Look at each OP group separately: UNION ALL has 1,1,2,2,3; UNION has 1,2,3; INTERSECT has 2; MINUS has 1.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Reverse the MINUS operands and explain why the answer changes.
Compare your reasoning
B MINUS A returns 3, while A MINUS B returns 1. MINUS keeps values unique to its left operand.