0
0
PostgreSQLquery~20 mins

UNION and UNION ALL in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Union Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Difference in output between UNION and UNION ALL

Consider two tables fruits and more_fruits with the following data:

fruits:       more_fruits:
+-------+    +------------+
| name  |    | name       |
+-------+    +------------+
| apple |    | banana     |
| banana|    | apple      |
| cherry|    | cherry     |
+-------+    +------------+

What will be the result of this query?

SELECT name FROM fruits
UNION
SELECT name FROM more_fruits;
PostgreSQL
SELECT name FROM fruits
UNION
SELECT name FROM more_fruits;
A[{"name": "apple"}, {"name": "banana"}, {"name": "cherry"}]
B[{"name": "apple"}, {"name": "banana"}, {"name": "cherry"}, {"name": "apple"}]
C[{"name": "apple"}, {"name": "banana"}]
D[{"name": "banana"}, {"name": "cherry"}]
Attempts:
2 left
💡 Hint

UNION removes duplicates, UNION ALL does not.

query_result
intermediate
2:00remaining
Output of UNION ALL with duplicates

Using the same tables fruits and more_fruits as before, what is the output of this query?

SELECT name FROM fruits
UNION ALL
SELECT name FROM more_fruits;
PostgreSQL
SELECT name FROM fruits
UNION ALL
SELECT name FROM more_fruits;
A[{"name": "apple"}, {"name": "banana"}, {"name": "cherry"}]
B[{"name": "apple"}, {"name": "banana"}, {"name": "cherry"}, {"name": "apple"}]
C[{"name": "apple"}, {"name": "banana"}, {"name": "cherry"}, {"name": "banana"}, {"name": "apple"}, {"name": "cherry"}]
D[{"name": "banana"}, {"name": "cherry"}]
Attempts:
2 left
💡 Hint

UNION ALL keeps all rows including duplicates.

🧠 Conceptual
advanced
1:30remaining
Why use UNION instead of UNION ALL?

Which of the following is the main reason to use UNION instead of UNION ALL?

ATo filter rows based on a condition
BTo combine results faster without removing duplicates
CTo join tables based on matching columns
DTo combine results and remove duplicate rows
Attempts:
2 left
💡 Hint

Think about what happens to duplicates.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in UNION query

Which of the following queries will cause a syntax error in PostgreSQL?

ASELECT id, name FROM users UNION ALL SELECT id FROM customers;
BSELECT id, name FROM users UNION SELECT id, name FROM customers;
CSELECT id, name FROM users UNION ALL SELECT id, name FROM customers;
D;sremotsuc MORF eman ,di TCELES NOINU sresu MORF eman ,di TCELES
Attempts:
2 left
💡 Hint

Check if both SELECT statements have the same number of columns.

optimization
expert
2:30remaining
Performance impact of UNION vs UNION ALL

You have two large tables with millions of rows each. You want to combine their data without duplicates. Which query is likely to perform better and why?

Option 1:
SELECT * FROM table1
UNION
SELECT * FROM table2;

Option 2:
SELECT * FROM table1
UNION ALL
SELECT * FROM table2;
-- then remove duplicates in application code
AOption 2 performs better because UNION ALL is faster and removing duplicates in application is cheaper.
BOption 1 performs worse because UNION requires sorting and duplicate elimination.
CBoth options perform the same because duplicates removal cost is equal.
DOption 1 performs better because the database removes duplicates efficiently.
Attempts:
2 left
💡 Hint

Consider how databases handle UNION internally.