Bird
Raised Fist0
MySQLquery~10 mins

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

Choose your learning style10 modes available
Concept Flow - UNION and UNION ALL
Start: Two SELECT queries
↓
Execute first SELECT
↓
Combine results
↓
UNION
↓
Remove duplicates
↓
Final result set
UNION and UNION ALL combine results from two SELECT queries; UNION removes duplicates, UNION ALL keeps all rows.
Execution Sample
MySQL
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?
1Execute first SELECT["Apple", "Banana", "Carrot"]["Apple", "Banana", "Carrot"]N/A
2Execute second SELECT["Carrot", "Date", "Eggplant"]["Apple", "Banana", "Carrot", "Date", "Eggplant"]N/A
3Combine results["Apple", "Banana", "Carrot", "Carrot", "Date", "Eggplant"]No
4UNION removes duplicates["Apple", "Banana", "Carrot", "Date", "Eggplant"]Yes
5UNION ALL keeps duplicates["Apple", "Banana", "Carrot", "Carrot", "Date", "Eggplant"]No
6EndFinal sets readyN/A
💡 Execution stops after combining and optionally removing duplicates depending on UNION or UNION ALL.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
First SELECT Result[]["Apple", "Banana", "Carrot"]["Apple", "Banana", "Carrot"]["Apple", "Banana", "Carrot"]["Apple", "Banana", "Carrot"]["Apple", "Banana", "Carrot"]["Apple", "Banana", "Carrot"]
Second SELECT Result[][]["Carrot", "Date", "Eggplant"]["Carrot", "Date", "Eggplant"]["Carrot", "Date", "Eggplant"]["Carrot", "Date", "Eggplant"]["Carrot", "Date", "Eggplant"]
Combined Result[][][]["Apple", "Banana", "Carrot", "Carrot", "Date", "Eggplant"]["Apple", "Banana", "Carrot", "Date", "Eggplant"]["Apple", "Banana", "Carrot", "Carrot", "Date", "Eggplant"]Depends on UNION or UNION ALL
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 execution_table step 4), while UNION ALL keeps all rows including duplicates (step 5).
Are the order of rows guaranteed in UNION or UNION ALL?
No, both UNION and UNION ALL do not guarantee order. To control order, use ORDER BY after the UNION query.
Can the two SELECT queries have different columns or data types?
No, both SELECT queries must have the same number of columns and compatible data types for UNION or UNION ALL to work.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What happens to the combined result?
ADuplicates are removed
BDuplicates are added
CRows are sorted alphabetically
DOnly the first SELECT result is kept
💡 Hint
Check the 'Duplicates Removed?' column at step 4 in execution_table.
At which step does UNION ALL keep duplicates in the combined result?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Duplicates Removed?' column for step 5 in execution_table.
If the first SELECT returns ["Apple", "Banana"] and the second SELECT returns ["Banana", "Cherry"], what will UNION return?
A["Apple", "Banana", "Banana", "Cherry"]
B["Apple", "Banana", "Cherry"]
C["Banana", "Cherry"]
D["Apple", "Cherry"]
💡 Hint
UNION removes duplicates after combining both results, see variable_tracker and execution_table.
Concept Snapshot
UNION and UNION ALL combine results from two SELECT queries.
UNION removes duplicate rows.
UNION ALL keeps all rows including duplicates.
Both require same number of columns and compatible types.
Use ORDER BY after UNION to sort results.
Full Transcript
This visual execution shows how UNION and UNION ALL work in MySQL. First, two SELECT queries run separately. Their results are combined. UNION removes duplicate rows from the combined result, while UNION ALL keeps all rows including duplicates. The execution table tracks each step: running queries, combining results, and removing duplicates if UNION is used. The variable tracker shows how the result sets change after each step. Key moments clarify common confusions like why duplicates are removed only in UNION and not UNION ALL, and that order is not guaranteed without ORDER BY. The quiz tests understanding of these steps and outcomes. Remember, both SELECT queries must have the same number of columns and compatible data types for UNION or UNION ALL to work.

Practice

(1/5)
1. What is the main difference between UNION and UNION ALL in MySQL?
easy
A. UNION sorts the result, UNION ALL does not.
B. UNION keeps all rows including duplicates, UNION ALL removes duplicates.
C. UNION only works with numeric columns, UNION ALL works with all data types.
D. UNION removes duplicate rows, UNION ALL keeps all rows including duplicates.

Solution

  1. Step 1: Understand UNION behavior

    UNION combines results from two queries and removes duplicate rows to return only unique rows.
  2. Step 2: Understand UNION ALL behavior

    UNION ALL combines results but keeps all rows, including duplicates, without filtering.
  3. Final Answer:

    UNION removes duplicate rows, UNION ALL keeps all rows including duplicates. -> Option D
  4. Quick Check:

    Duplicates removed = UNION, duplicates kept = UNION ALL [OK]
Hint: Remember: UNION = unique rows, UNION ALL = all rows [OK]
Common Mistakes:
  • Thinking UNION ALL removes duplicates
  • Confusing sorting behavior with UNION
  • Assuming UNION only works on numeric data
2. Which of the following is the correct syntax to combine two SELECT queries using UNION ALL in MySQL?
easy
A. SELECT * FROM table1 UNION ALL SELECT * FROM table2;
B. SELECT * FROM table1 UNIONALL SELECT * FROM table2;
C. SELECT * FROM table1 UNION, ALL SELECT * FROM table2;
D. SELECT * FROM table1 UNION; ALL SELECT * FROM table2;

Solution

  1. Step 1: Check correct keyword spacing

    The correct keyword is UNION ALL with a space between UNION and ALL.
  2. Step 2: Validate SQL syntax

    The syntax must be: SELECT ... UNION ALL SELECT ...; without commas or semicolons between the two SELECTs.
  3. Final Answer:

    SELECT * FROM table1 UNION ALL SELECT * FROM table2; -> Option A
  4. Quick Check:

    Correct keyword and syntax = SELECT * FROM table1 UNION ALL SELECT * FROM table2; [OK]
Hint: Use space between UNION and ALL, no commas [OK]
Common Mistakes:
  • Writing UNIONALL as one word
  • Adding commas between SELECT statements
  • Using semicolons between SELECTs
3. Given two tables:
Table A:
id | name
1 | Alice
2 | Bob

Table B:
id | name
2 | Bob
3 | Carol

What will be the result of this query?
SELECT id, name FROM A UNION SELECT id, name FROM B;
medium
A. 2 | Bob
3 | Carol
B. 1 | Alice
2 | Bob
2 | Bob
3 | Carol
C. 1 | Alice
2 | Bob
3 | Carol
D. 1 | Alice
3 | Carol

Solution

  1. Step 1: Combine rows from both tables

    Table A has (1, Alice) and (2, Bob). Table B has (2, Bob) and (3, Carol).
  2. Step 2: Apply UNION behavior

    UNION removes duplicates, so (2, Bob) appears only once in the result.
  3. Final Answer:

    1 | Alice
    2 | Bob
    3 | Carol
    -> Option C
  4. Quick Check:

    Duplicates removed, unique rows shown [OK]
Hint: UNION removes duplicates, so repeated rows appear once [OK]
Common Mistakes:
  • Including duplicate rows with UNION
  • Confusing UNION with UNION ALL output
  • Ignoring duplicate row removal
4. Consider this query:
SELECT id FROM table1
UNION ALL
SELECT id FROM table2 ORDER BY id;

What is wrong with this query?
medium
A. ORDER BY must be applied only once at the end of the UNION ALL query.
B. UNION ALL cannot be used with ORDER BY.
C. Each SELECT must have its own ORDER BY clause.
D. ORDER BY should come before UNION ALL.

Solution

  1. Step 1: Understand ORDER BY with UNION ALL

    ORDER BY applies to the entire combined result, not to individual SELECTs.
  2. Step 2: Identify correct placement

    ORDER BY must be placed after the last SELECT and UNION ALL, not inside one SELECT.
  3. Final Answer:

    ORDER BY must be applied only once at the end of the UNION ALL query. -> Option A
  4. Quick Check:

    ORDER BY after UNION ALL, not inside SELECT [OK]
Hint: Place ORDER BY after all UNION ALL parts [OK]
Common Mistakes:
  • Putting ORDER BY inside one SELECT only
  • Trying ORDER BY before UNION ALL
  • Assuming UNION ALL disallows ORDER BY
5. You have two tables:
Employees:
id | name
1 | John
2 | Jane
3 | Mike

Contractors:
id | name
2 | Jane
4 | Anna

You want to list all people from both tables, including duplicates, and sort by name ascending.
Which query achieves this correctly?
hard
A. SELECT id, name FROM Employees UNION SELECT id, name FROM Contractors ORDER BY name ASC;
B. SELECT id, name FROM Employees UNION ALL SELECT id, name FROM Contractors ORDER BY name ASC;
C. SELECT id, name FROM Employees UNION ALL SELECT id, name FROM Contractors ORDER BY id ASC;
D. SELECT id, name FROM Employees UNION ALL ORDER BY name ASC SELECT id, name FROM Contractors;

Solution

  1. Step 1: Choose UNION ALL to keep duplicates

    Since duplicates like Jane (id 2) should appear twice, use UNION ALL.
  2. Step 2: Apply ORDER BY after UNION ALL

    ORDER BY must come after the full UNION ALL to sort combined results by name ascending.
  3. Final Answer:

    SELECT id, name FROM Employees UNION ALL SELECT id, name FROM Contractors ORDER BY name ASC; -> Option B
  4. Quick Check:

    UNION ALL + ORDER BY at end = SELECT id, name FROM Employees UNION ALL SELECT id, name FROM Contractors ORDER BY name ASC; [OK]
Hint: Use UNION ALL for duplicates, ORDER BY after all SELECTs [OK]
Common Mistakes:
  • Using UNION instead of UNION ALL (removes duplicates)
  • Placing ORDER BY inside one SELECT
  • Incorrect syntax with ORDER BY placement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard