Bird
Raised Fist0
SQLquery~20 mins

UNION ALL with duplicates in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
UNION ALL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of UNION ALL with duplicate rows
Consider two tables Employees_A and Employees_B with the following data:

Employees_A
id | name
1 | Alice
2 | Bob

Employees_B
id | name
2 | Bob
3 | Charlie

What is the result of the following query?
SELECT id, name FROM Employees_A
UNION ALL
SELECT id, name FROM Employees_B;
A
1 | Alice
2 | Bob
3 | Charlie
2 | Bob
B
1 | Alice
2 | Bob
3 | Charlie
C
1 | Alice
3 | Charlie
D
1 | Alice
2 | Bob
2 | Bob
3 | Charlie
Attempts:
2 left
💡 Hint
UNION ALL includes all rows from both queries, including duplicates.
query_result
intermediate
2:00remaining
Counting rows after UNION ALL
Given two tables Orders_2023 and Orders_2024 each with 5 rows, where 2 rows are identical in both tables, what is the total number of rows returned by this query?
SELECT * FROM Orders_2023
UNION ALL
SELECT * FROM Orders_2024;
A10
B8
C5
D7
Attempts:
2 left
💡 Hint
UNION ALL does not remove duplicates, so all rows from both tables are included.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in UNION ALL query
Which of the following SQL queries will cause a syntax error?
ASELECT id, name FROM Customers UNION ALL SELECT id, name FROM Clients ORDER BY name;
BSELECT id, name FROM Customers UNION ALL SELECT id, name FROM Clients;
CSELECT id, name FROM Customers UNION ALL SELECT id FROM Clients;
DSELECT id, name FROM Customers UNION ALL SELECT id, name FROM Clients WHERE active = 1;
Attempts:
2 left
💡 Hint
UNION ALL requires the same number of columns in both SELECT statements.
optimization
advanced
2:00remaining
Performance impact of UNION ALL vs UNION
Which statement about performance is true when comparing UNION ALL and UNION?
AUNION ALL is slower because it removes duplicates.
BUNION ALL is faster because it does not remove duplicates.
CUNION and UNION ALL have the same performance.
DUNION ALL requires sorting the result set.
Attempts:
2 left
💡 Hint
Removing duplicates requires extra work.
🧠 Conceptual
expert
2:00remaining
Effect of UNION ALL on duplicate rows with NULL values
Consider two tables Table1 and Table2 each having a row with values (NULL, 'X'). What will be the result of this query?
SELECT col1, col2 FROM Table1
UNION ALL
SELECT col1, col2 FROM Table2;

Specifically, how many rows with (NULL, 'X') will appear in the result?
A2 rows
B0 rows
C1 row
DDepends on the database settings
Attempts:
2 left
💡 Hint
UNION ALL includes all rows even if they contain NULLs and duplicates.

Practice

(1/5)
1. What does the SQL statement UNION ALL do when combining results from two queries?
easy
A. It combines rows but removes duplicates.
B. It combines all rows from both queries including duplicates.
C. It only returns rows that appear in both queries.
D. It returns rows only from the first query.

Solution

  1. Step 1: Understand UNION ALL behavior

    UNION ALL combines results from two queries and keeps all rows, including duplicates.
  2. Step 2: Compare with UNION

    Unlike UNION, UNION ALL does not remove duplicate rows.
  3. Final Answer:

    It combines all rows from both queries including duplicates. -> Option B
  4. Quick Check:

    UNION ALL keeps duplicates = A [OK]
Hint: UNION ALL keeps duplicates, UNION removes them [OK]
Common Mistakes:
  • Confusing UNION ALL with UNION
  • Thinking duplicates are removed
  • Assuming it returns only unique rows
2. Which of the following is the correct syntax to combine two SELECT queries using UNION ALL?
easy
A. SELECT col1 FROM table1 UNIONALL SELECT col1 FROM table2;
B. SELECT col1 FROM table1 UNION ALL FROM table2;
C. SELECT col1 FROM table1 UNION ALL SELECT col1 FROM table2;
D. SELECT col1 FROM table1 UNION ALL SELECT FROM table2;

Solution

  1. Step 1: Check correct UNION ALL syntax

    The correct syntax is to write two SELECT statements separated by 'UNION ALL'.
  2. Step 2: Identify syntax errors in options

    SELECT col1 FROM table1 UNIONALL SELECT col1 FROM table2; writes UNIONALL as one word (missing space), SELECT col1 FROM table1 UNION ALL FROM table2; misses the second SELECT keyword, SELECT col1 FROM table1 UNION ALL SELECT FROM table2; misses columns after the second SELECT. SELECT col1 FROM table1 UNION ALL SELECT col1 FROM table2; uses correct syntax.
  3. Final Answer:

    SELECT col1 FROM table1 UNION ALL SELECT col1 FROM table2; -> Option C
  4. Quick Check:

    Correct UNION ALL syntax = B [OK]
Hint: UNION ALL needs space and two SELECTs [OK]
Common Mistakes:
  • Writing UNIONALL as one word
  • Missing SELECT in second query
  • Incorrect keyword order
3. Given two tables:
Table A: id
1
2
3

Table B: id
2
3
4

What is the result of:
SELECT id FROM A UNION ALL SELECT id FROM B;
medium
A. [1, 2, 3, 2, 3, 4]
B. [1, 2, 3, 4]
C. [1, 2, 3]
D. [2, 3, 4]

Solution

  1. Step 1: List rows from first query

    SELECT id FROM A returns [1, 2, 3].
  2. Step 2: List rows from second query

    SELECT id FROM B returns [2, 3, 4].
  3. Step 3: Combine results with UNION ALL

    UNION ALL keeps duplicates, so combined list is [1, 2, 3, 2, 3, 4].
  4. Final Answer:

    [1, 2, 3, 2, 3, 4] -> Option A
  5. Quick Check:

    UNION ALL keeps duplicates = A [OK]
Hint: UNION ALL stacks all rows, duplicates included [OK]
Common Mistakes:
  • Removing duplicates like UNION
  • Listing only unique values
  • Ignoring order of combined rows
4. Consider this SQL query:
SELECT name FROM employees UNION ALL SELECT name FROM departments;
It returns an error. What is the most likely cause?
medium
A. The two SELECT statements have different numbers of columns.
B. UNION ALL cannot be used with SELECT statements.
C. The keyword ALL is not allowed after UNION.
D. The tables employees and departments must have the same name.

Solution

  1. Step 1: Check column counts in both SELECTs

    UNION ALL requires both SELECTs to have the same number of columns.
  2. Step 2: Identify error cause

    If employees and departments tables have different columns selected, error occurs.
  3. Final Answer:

    The two SELECT statements have different numbers of columns. -> Option A
  4. Quick Check:

    Column count mismatch causes error = C [OK]
Hint: Both SELECTs must have same columns count [OK]
Common Mistakes:
  • Thinking UNION ALL disallows duplicates only
  • Assuming table names must match
  • Believing ALL keyword is invalid
5. You have two tables:
Sales_2023: product_id, quantity_sold
Sales_2024: product_id, quantity_sold

You want to create a combined list of all sales including duplicates for analysis. Which query correctly uses UNION ALL and also adds a column year to identify the source year?
hard
A. SELECT product_id, quantity_sold, 'year' FROM Sales_2023 UNION ALL SELECT product_id, quantity_sold, 'year' FROM Sales_2024;
B. SELECT product_id, quantity_sold FROM Sales_2023 UNION ALL SELECT product_id, quantity_sold, '2024' AS year FROM Sales_2024;
C. SELECT product_id, quantity_sold, year FROM Sales_2023 UNION ALL SELECT product_id, quantity_sold, year FROM Sales_2024;
D. SELECT product_id, quantity_sold, '2023' AS year FROM Sales_2023 UNION ALL SELECT product_id, quantity_sold, '2024' AS year FROM Sales_2024;

Solution

  1. Step 1: Add year column with literal values

    Use '2023' and '2024' as string literals to add a year column in each SELECT.
  2. Step 2: Ensure both SELECTs have same columns

    Both SELECTs must have product_id, quantity_sold, and year columns for UNION ALL.
  3. Step 3: Combine with UNION ALL

    SELECT product_id, quantity_sold, '2023' AS year FROM Sales_2023 UNION ALL SELECT product_id, quantity_sold, '2024' AS year FROM Sales_2024; correctly combines both tables with year column and keeps duplicates.
  4. Final Answer:

    SELECT product_id, quantity_sold, '2023' AS year FROM Sales_2023 UNION ALL SELECT product_id, quantity_sold, '2024' AS year FROM Sales_2024; -> Option D
  5. Quick Check:

    Matching columns with year literals + UNION ALL = D [OK]
Hint: Add year as literal in both SELECTs for UNION ALL [OK]
Common Mistakes:
  • Mismatched columns count in SELECTs
  • Using column name 'year' without value
  • Forgetting to alias literals as year