Bird
Raised Fist0
SQLquery~20 mins

UNION combining result sets 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 Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this UNION query?
Consider two tables:

Table A:
id | name
1 | Alice
2 | Bob

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

What rows will this query return?
SELECT id, name FROM A
UNION
SELECT id, name FROM B;
A2 | Bob<br>3 | Carol
B1 | Alice<br>2 | Bob<br>2 | Bob<br>3 | Carol
C1 | Alice<br>2 | Bob<br>3 | Carol
D1 | Alice<br>3 | Carol
Attempts:
2 left
💡 Hint
UNION removes duplicate rows from the combined result.
📝 Syntax
intermediate
2:00remaining
Which UNION query is syntactically correct?
Choose the correct SQL UNION syntax from the options below:
ASELECT id, name FROM A UNION SELECT id, name FROM B ORDER BY name LIMIT 5;
BSELECT id, name FROM A UNION ALL SELECT id, name FROM B;
CSELECT id, name FROM A UNION SELECT id, name FROM B ORDER BY name;
DSELECT id, name FROM A UNION SELECT id, name FROM B;
Attempts:
2 left
💡 Hint
UNION combines result sets; ORDER BY applies to the whole UNION result and must be placed at the end.
optimization
advanced
2:00remaining
Which UNION query is more efficient for combining two large tables without duplicates?
Given two large tables A and B with the same columns, which query is best to combine all unique rows?
ASELECT * FROM A UNION SELECT * FROM B;
BSELECT * FROM A UNION ALL SELECT * FROM B;
CSELECT DISTINCT * FROM (SELECT * FROM A UNION ALL SELECT * FROM B) AS combined;
DSELECT * FROM A INTERSECT SELECT * FROM B;
Attempts:
2 left
💡 Hint
UNION removes duplicates automatically, UNION ALL does not.
🔧 Debug
advanced
2:00remaining
Why does this UNION query cause an error?
Given tables A(id INT, name VARCHAR) and B(id INT, age INT), why does this query fail?
SELECT id, name FROM A UNION SELECT id, age FROM B;
ABecause the column data types in the same position differ.
BBecause table B does not have a 'name' column.
CBecause UNION requires ORDER BY clause.
DBecause the number of columns in both SELECTs differ.
Attempts:
2 left
💡 Hint
UNION requires matching column types in order.
🧠 Conceptual
expert
2:00remaining
What is the difference between UNION and UNION ALL?
Choose the statement that correctly explains the difference between UNION and UNION ALL:
AUNION and UNION ALL behave the same but UNION ALL requires ORDER BY.
BUNION removes duplicate rows from the combined result; UNION ALL includes all rows, duplicates included.
CUNION is faster than UNION ALL because it skips duplicates.
DUNION includes all rows, duplicates included; UNION ALL removes duplicates.
Attempts:
2 left
💡 Hint
Think about how duplicates are handled in each.

Practice

(1/5)
1. What does the SQL UNION operator do when combining results from two SELECT queries?
easy
A. Combines rows but keeps all duplicates
B. Combines rows from both queries and removes duplicate rows
C. Joins tables based on a common column
D. Filters rows based on a condition

Solution

  1. Step 1: Understand UNION behavior

    The UNION operator combines rows from two or more SELECT queries into a single result set.
  2. Step 2: Check duplicate handling

    By default, UNION removes duplicate rows to ensure unique results.
  3. Final Answer:

    Combines rows from both queries and removes duplicate rows -> Option B
  4. Quick Check:

    UNION removes duplicates = A [OK]
Hint: UNION removes duplicates by default, unlike UNION ALL [OK]
Common Mistakes:
  • Confusing UNION with UNION ALL
  • Thinking UNION joins tables by columns
  • Assuming UNION filters rows
2. Which of the following is the correct syntax to combine two SELECT queries using UNION?
easy
A. SELECT col1 FROM table1 UNION ALL JOIN SELECT col1 FROM table2;
B. SELECT col1 FROM table1 JOIN SELECT col1 FROM table2;
C. SELECT col1 FROM table1 WHERE UNION SELECT col1 FROM table2;
D. SELECT col1 FROM table1 UNION SELECT col1 FROM table2;

Solution

  1. Step 1: Review correct UNION syntax

    The correct syntax is to write two SELECT statements separated by the UNION keyword.
  2. Step 2: Identify invalid options

    Options B, C, and D misuse JOIN or WHERE with UNION, which is incorrect syntax.
  3. Final Answer:

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

    UNION syntax = SELECT ... UNION SELECT ... [OK]
Hint: UNION joins SELECTs directly, no JOIN or WHERE needed [OK]
Common Mistakes:
  • Using JOIN instead of UNION
  • Placing WHERE before UNION
  • Combining UNION ALL with JOIN incorrectly
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 SELECT id FROM B;
medium
A. [1, 2, 3, 4]
B. [1, 2, 2, 3, 3, 4]
C. [2, 3]
D. [1, 4]

Solution

  1. Step 1: List rows from both tables

    Table A has ids 1, 2, 3; Table B has ids 2, 3, 4.
  2. Step 2: Apply UNION behavior

    UNION combines all rows and removes duplicates, so final list is 1, 2, 3, 4.
  3. Final Answer:

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

    UNION removes duplicates = [1, 2, 3, 4] [OK]
Hint: UNION removes duplicates, so no repeated ids appear [OK]
Common Mistakes:
  • Expecting duplicates to appear
  • Confusing UNION with UNION ALL
  • Listing only common ids
4. Consider this SQL query:
SELECT name FROM employees UNION SELECT name, department FROM managers;
What is the issue with this query?
medium
A. The number of columns in both SELECT statements differ
B. UNION cannot be used with SELECT statements
C. The query will return duplicate rows
D. The query is missing a WHERE clause

Solution

  1. Step 1: Check column counts in SELECTs

    The first SELECT returns 1 column (name), the second returns 2 columns (name, department).
  2. Step 2: Understand UNION column rules

    UNION requires all SELECT statements to have the same number of columns with compatible types.
  3. Final Answer:

    The number of columns in both SELECT statements differ -> Option A
  4. Quick Check:

    UNION needs same columns = C [OK]
Hint: UNION needs same column count in all SELECTs [OK]
Common Mistakes:
  • Ignoring column count mismatch
  • Thinking UNION filters duplicates only
  • Assuming WHERE clause is mandatory
5. You have two tables:
Sales2023(product, amount)
Apple, 100
Banana, 150

Sales2024(product, amount)
Banana, 200
Cherry, 300

Write a query using UNION to list all unique products sold in both years, sorted alphabetically.
hard
A. SELECT product, amount FROM Sales2023 UNION SELECT product, amount FROM Sales2024 ORDER BY amount;
B. SELECT product FROM Sales2023 UNION ALL SELECT product FROM Sales2024 ORDER BY product;
C. SELECT product FROM Sales2023 UNION SELECT product FROM Sales2024 ORDER BY product;
D. SELECT product FROM Sales2023 JOIN Sales2024 ON product ORDER BY product;

Solution

  1. Step 1: Select product column from both tables

    We want unique products, so select only the product column from both tables.
  2. Step 2: Use UNION to combine and remove duplicates

    UNION combines both lists and removes duplicates, giving unique products.
  3. Step 3: Sort results alphabetically

    ORDER BY product sorts the final list alphabetically.
  4. Final Answer:

    SELECT product FROM Sales2023 UNION SELECT product FROM Sales2024 ORDER BY product; -> Option C
  5. Quick Check:

    UNION + ORDER BY product = B [OK]
Hint: Use UNION to remove duplicates, ORDER BY to sort [OK]
Common Mistakes:
  • Using UNION ALL which keeps duplicates
  • Selecting amount column when only product needed
  • Using JOIN instead of UNION