Bird
Raised Fist0
PostgreSQLquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between UNION and UNION ALL in PostgreSQL?
easy
A. UNION removes duplicate rows, while UNION ALL keeps all rows including duplicates.
B. UNION requires matching column names, UNION ALL does not.
C. UNION ALL removes duplicates, UNION keeps duplicates.
D. UNION can combine different numbers of columns, UNION ALL cannot.

Solution

  1. Step 1: Understand the behavior of UNION

    UNION combines results from two queries and removes duplicate rows to give unique results.
  2. Step 2: Understand the behavior of UNION ALL

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

    UNION removes duplicates, UNION ALL keeps duplicates. -> Option A
  4. Quick Check:

    Duplicates removed = UNION, duplicates kept = UNION ALL [OK]
Hint: UNION removes duplicates; UNION ALL keeps all rows [OK]
Common Mistakes:
  • Thinking UNION ALL removes duplicates
  • Believing UNION allows different column counts
  • Confusing column name requirements
2. Which of the following is the correct syntax to combine two SELECT queries using UNION ALL in PostgreSQL?
easy
A. SELECT name FROM students UNION ALL SELECT name FROM teachers;
B. SELECT name FROM students UNIONALL SELECT name FROM teachers;
C. SELECT name FROM students UNION ALL FROM teachers;
D. SELECT name FROM students UNION ALL SELECT name FROM teachers WHERE;

Solution

  1. Step 1: Check the correct keyword spacing

    The correct keyword is UNION ALL with a space between UNION and ALL.
  2. Step 2: Verify the SELECT statements are complete

    Both queries must be valid SELECT statements with matching column counts and types.
  3. Final Answer:

    SELECT name FROM students UNION ALL SELECT name FROM teachers; -> Option A
  4. Quick Check:

    Correct syntax uses 'UNION ALL' with space [OK]
Hint: Use space between UNION and ALL, both queries must be valid [OK]
Common Mistakes:
  • Writing UNIONALL as one word
  • Missing SELECT in second query
  • Adding WHERE without condition
3. Given two tables:
Table A:
id | value
1 | 'apple'
2 | 'banana'

Table B:
id | value
2 | 'banana'
3 | 'cherry'

What is the result of this query?
SELECT value FROM A UNION SELECT value FROM B ORDER BY value;
medium
A. apple, banana, banana, cherry
B. apple, banana, cherry, banana
C. banana, cherry
D. apple, banana, cherry

Solution

  1. Step 1: Combine values from both tables using UNION

    UNION removes duplicates, so 'banana' appears only once.
  2. Step 2: Order the combined results alphabetically

    Ordering by value gives: 'apple', 'banana', 'cherry'.
  3. Final Answer:

    apple, banana, cherry -> Option D
  4. Quick Check:

    UNION removes duplicates, so only one 'banana' [OK]
Hint: UNION removes duplicates; check ordering carefully [OK]
Common Mistakes:
  • Including duplicate 'banana' with UNION
  • Ignoring ORDER BY effect
  • Confusing UNION with UNION ALL
4. Consider this query:
SELECT id FROM table1 UNION ALL SELECT id FROM table2;

It returns an error. What is the most likely cause?
medium
A. The tables have different names.
B. The two SELECT statements have different numbers of columns.
C. UNION ALL cannot be used with SELECT statements.
D. The query is missing a semicolon.

Solution

  1. Step 1: Check column counts in both SELECT statements

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

    If column counts differ, PostgreSQL throws an error.
  3. Final Answer:

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

    Column count mismatch causes UNION ALL error [OK]
Hint: Ensure both SELECTs have same column count for UNION ALL [OK]
Common Mistakes:
  • Thinking table names must match
  • Believing UNION ALL disallows SELECT
  • Ignoring column count mismatch
5. You want to combine two queries:
SELECT id, name FROM employees WHERE department = 'Sales'
SELECT id, name FROM contractors WHERE department = 'Sales'

and keep all rows including duplicates. Which query correctly does this and why?
hard
A. SELECT id, name FROM employees UNION ALL SELECT id, name FROM contractors; Because WHERE is not needed in second query.
B. SELECT id, name FROM employees UNION SELECT id, name FROM contractors WHERE department = 'Sales'; Because UNION removes duplicates.
C. SELECT id, name FROM employees UNION ALL SELECT id, name FROM contractors WHERE department = 'Sales'; Because UNION ALL keeps duplicates.
D. SELECT id, name FROM employees UNION SELECT id, name FROM contractors; Because UNION ALL is invalid here.

Solution

  1. Step 1: Understand requirement to keep duplicates

    To keep duplicates, use UNION ALL because UNION removes duplicates.
  2. Step 2: Check WHERE clause usage

    Both queries filter department = 'Sales', so WHERE must be in both queries.
  3. Final Answer:

    SELECT id, name FROM employees UNION ALL SELECT id, name FROM contractors WHERE department = 'Sales'; Because UNION ALL keeps duplicates. -> Option C
  4. Quick Check:

    Keep duplicates = UNION ALL with correct WHERE clauses [OK]
Hint: Use UNION ALL to keep duplicates; apply WHERE in each SELECT [OK]
Common Mistakes:
  • Using UNION instead of UNION ALL
  • Omitting WHERE in second query
  • Thinking UNION ALL is invalid
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