0
0
PostgreSQLquery~10 mins

UNION and UNION ALL in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - UNION and UNION ALL
Start: Two SELECT queries
Execute first SELECT
Combine results
UNION: Remove duplicates
Final result set returned
End
Two SELECT queries run separately, then their results combine. UNION removes duplicates; UNION ALL keeps all rows.
Execution Sample
PostgreSQL
SELECT name FROM fruits
UNION
SELECT name FROM vegetables;
Combines names from fruits and vegetables tables, removing duplicates.
Execution Table
StepActionFirst SELECT ResultSecond SELECT ResultCombined ResultDuplicates Removed?Final Output
1Execute first SELECT[Apple, Banana, Orange]N/A
2Execute second SELECT[Apple, Banana, Orange][Carrot, Banana, Tomato]N/A
3Combine results[Apple, Banana, Orange][Carrot, Banana, Tomato][Apple, Banana, Orange, Carrot, Banana, Tomato]N/A
4Apply UNION (remove duplicates)[Apple, Banana, Orange][Carrot, Banana, Tomato][Apple, Banana, Orange, Carrot, Tomato]Yes[Apple, Banana, Orange, Carrot, Tomato]
5Return final result (UNION)[Apple, Banana, Orange, Carrot, Tomato]
6Apply UNION ALL (keep duplicates) [alternative after step 3][Apple, Banana, Orange, Carrot, Banana, Tomato]No[Apple, Banana, Orange, Carrot, Banana, Tomato]
💡 Execution stops after combining and applying UNION or UNION ALL rules to produce final output.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4 (UNION)After Step 6 (UNION ALL)
First SELECT Resultempty[Apple, Banana, Orange][Apple, Banana, Orange][Apple, Banana, Orange][Apple, Banana, Orange][Apple, Banana, Orange]
Second SELECT Resultemptyempty[Carrot, Banana, Tomato][Carrot, Banana, Tomato][Carrot, Banana, Tomato][Carrot, Banana, Tomato]
Combined Resultemptyemptyempty[Apple, Banana, Orange, Carrot, Banana, Tomato][Apple, Banana, Orange, Carrot, Tomato][Apple, Banana, Orange, Carrot, Banana, Tomato]
Final Outputemptyemptyemptyempty[Apple, Banana, Orange, Carrot, Tomato][Apple, Banana, Orange, Carrot, Banana, Tomato]
Key Moments - 3 Insights
Why does UNION remove some rows but UNION ALL does not?
UNION removes duplicate rows after combining both SELECT results (see step 4 in execution_table), while UNION ALL keeps all rows including duplicates (see step 6).
Are the two SELECT queries executed separately or together?
They are executed separately first (steps 1 and 2), then their results are combined (step 3).
Does the order of rows stay the same after UNION?
No, UNION may reorder rows because it removes duplicates and returns a distinct set (step 4). UNION ALL preserves all rows in combined order (step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what happens to duplicates?
ADuplicates are removed
BDuplicates are kept
CDuplicates are doubled
DDuplicates are ignored only in first SELECT
💡 Hint
Check the 'Duplicates Removed?' and 'Final Output' columns at step 4 in execution_table.
At which step do the two SELECT query results combine?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the step where 'Combined Result' column first shows both SELECT results together.
If we use UNION ALL instead of UNION, what changes in the final output?
ADuplicates are removed
BDuplicates remain in the output
COnly first SELECT results are shown
DOutput is empty
💡 Hint
Compare 'Final Output' at step 4 (UNION) and step 6 (UNION ALL) in execution_table.
Concept Snapshot
UNION combines results of two SELECT queries and removes duplicates.
UNION ALL combines results and keeps all duplicates.
Both require same number of columns and compatible types.
UNION sorts and removes duplicates; UNION ALL is faster and keeps all rows.
Use UNION when duplicates matter; UNION ALL for full combined data.
Full Transcript
This visual execution shows how UNION and UNION ALL work in PostgreSQL. First, two SELECT queries run separately to get their results. Then, these results combine into one list. UNION removes duplicate rows from this combined list before returning it, while UNION ALL keeps all rows including duplicates. The execution table traces each step: running each SELECT, combining results, removing duplicates for UNION, and returning the final output. Variables track the state of results after each step. Key moments clarify why duplicates are removed only by UNION and how the queries execute separately before combining. The quiz tests understanding of when duplicates are removed and when results combine. The snapshot summarizes the main differences and usage tips for UNION and UNION ALL.