0
0
SQLquery~20 mins

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

Choose your learning style9 modes available
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.