0
0
SQLquery~10 mins

UNION combining result sets in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - UNION combining result sets
Execute Query 1
Get Result Set 1
Execute Query 2
Get Result Set 2
Combine Result Sets
Remove Duplicates
Return Final Result
The UNION operation runs two queries, combines their results, removes duplicates, and returns one final list.
Execution Sample
SQL
SELECT name FROM students
UNION
SELECT name FROM teachers;
This query combines names from students and teachers tables, removing duplicates.
Execution Table
StepActionQuery ResultCombined ResultNotes
1Execute first SELECT['Alice', 'Bob', 'Charlie']['Alice', 'Bob', 'Charlie']First query result collected
2Execute second SELECT['Bob', 'Diana', 'Eve']['Alice', 'Bob', 'Charlie', 'Bob', 'Diana', 'Eve']Second query result collected and appended
3Remove duplicatesN/A['Alice', 'Bob', 'Charlie', 'Diana', 'Eve']Duplicates like 'Bob' removed
4Return final resultN/A['Alice', 'Bob', 'Charlie', 'Diana', 'Eve']Final combined unique list returned
💡 All rows combined and duplicates removed, final unique result returned
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
ResultSet1[]['Alice', 'Bob', 'Charlie']['Alice', 'Bob', 'Charlie']['Alice', 'Bob', 'Charlie']['Alice', 'Bob', 'Charlie']
ResultSet2[][]['Bob', 'Diana', 'Eve']['Bob', 'Diana', 'Eve']['Bob', 'Diana', 'Eve']
CombinedResult[]['Alice', 'Bob', 'Charlie']['Alice', 'Bob', 'Charlie', 'Bob', 'Diana', 'Eve']['Alice', 'Bob', 'Charlie', 'Diana', 'Eve']['Alice', 'Bob', 'Charlie', 'Diana', 'Eve']
Key Moments - 3 Insights
Why does the final result not include duplicate names like 'Bob' twice?
Because UNION removes duplicates after combining both result sets, as shown in step 3 of the execution_table.
What happens if the two SELECT queries return different columns or data types?
UNION requires both queries to have the same number of columns and compatible data types; otherwise, it will cause an error before combining.
Does UNION preserve the order of rows from the original queries?
No, UNION does not guarantee order; to control order, use ORDER BY after the UNION operation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Combined Result after step 2?
A['Alice', 'Bob', 'Charlie', 'Bob', 'Diana', 'Eve']
B['Alice', 'Bob', 'Charlie', 'Diana', 'Eve']
C['Bob', 'Diana', 'Eve']
D['Alice', 'Bob', 'Charlie']
💡 Hint
Check the Combined Result column in row for step 2 in execution_table
At which step are duplicates removed according to the execution_table?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for the step mentioning 'Remove duplicates' in the Action column
If the second SELECT returned ['Bob', 'Diana', 'Eve', 'Frank'], how would the final Combined Result change?
A['Alice', 'Bob', 'Charlie', 'Diana', 'Eve']
B['Bob', 'Diana', 'Eve', 'Frank']
C['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank']
D['Alice', 'Bob', 'Charlie', 'Bob', 'Diana', 'Eve', 'Frank']
💡 Hint
Refer to variable_tracker CombinedResult after step 3 and consider adding 'Frank' without duplicates
Concept Snapshot
UNION combines results from two SELECT queries.
It removes duplicate rows from combined results.
Both queries must have same number of columns and compatible types.
Use ORDER BY after UNION to sort results.
UNION returns unique rows from both queries.
Full Transcript
The UNION operation in SQL runs two SELECT queries separately, collects their results, then combines these results into one list. After combining, it removes any duplicate rows so that each row appears only once in the final output. This process requires both queries to have the same number of columns with compatible data types. The final combined unique list is then returned. If you want the results in a specific order, you add an ORDER BY clause after the UNION. This visual trace showed step-by-step how the first query's results are collected, then the second's, how they are combined, duplicates removed, and the final list returned.