What if you could combine endless lists instantly without missing or repeating anyone?
Why UNION and UNION ALL in PostgreSQL? - Purpose & Use Cases
Imagine you have two separate lists of customer names from two different stores, and you want to see all customers combined in one list.
You try to write down each list on paper and then manually check for duplicates or just combine them.
This manual way is slow and tiring. You might miss some names or count some twice by accident.
It's hard to keep track and update the list when new customers come in.
Using UNION and UNION ALL in SQL lets you combine these lists quickly and correctly.
UNION removes duplicates automatically, while UNION ALL keeps all entries, even if repeated.
List1 = ['Alice', 'Bob'] List2 = ['Bob', 'Charlie'] Combined = [] for name in List1: if name not in Combined: Combined.append(name) for name in List2: if name not in Combined: Combined.append(name)
SELECT name FROM store1_customers UNION SELECT name FROM store2_customers;
It makes combining data from multiple sources fast, reliable, and easy to update.
A company merges customer lists from two branches to send a single newsletter without sending duplicates.
Manually combining lists is slow and error-prone.
UNION removes duplicates when merging data.
UNION ALL keeps all data including duplicates.
Practice
UNION and UNION ALL in PostgreSQL?Solution
Step 1: Understand the behavior of UNION
UNIONcombines results from two queries and removes duplicate rows to give unique results.Step 2: Understand the behavior of UNION ALL
UNION ALLcombines results but keeps all rows, including duplicates.Final Answer:
UNIONremoves duplicates,UNION ALLkeeps duplicates. -> Option AQuick Check:
Duplicates removed = UNION, duplicates kept = UNION ALL [OK]
- Thinking UNION ALL removes duplicates
- Believing UNION allows different column counts
- Confusing column name requirements
UNION ALL in PostgreSQL?Solution
Step 1: Check the correct keyword spacing
The correct keyword isUNION ALLwith a space between UNION and ALL.Step 2: Verify the SELECT statements are complete
Both queries must be valid SELECT statements with matching column counts and types.Final Answer:
SELECT name FROM students UNION ALL SELECT name FROM teachers; -> Option AQuick Check:
Correct syntax uses 'UNION ALL' with space [OK]
- Writing UNIONALL as one word
- Missing SELECT in second query
- Adding WHERE without condition
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;
Solution
Step 1: Combine values from both tables using UNION
UNIONremoves duplicates, so 'banana' appears only once.Step 2: Order the combined results alphabetically
Ordering by value gives: 'apple', 'banana', 'cherry'.Final Answer:
apple, banana, cherry -> Option DQuick Check:
UNION removes duplicates, so only one 'banana' [OK]
- Including duplicate 'banana' with UNION
- Ignoring ORDER BY effect
- Confusing UNION with UNION ALL
SELECT id FROM table1 UNION ALL SELECT id FROM table2;
It returns an error. What is the most likely cause?
Solution
Step 1: Check column counts in both SELECT statements
UNION ALL requires both SELECTs to have the same number of columns.Step 2: Identify error cause
If column counts differ, PostgreSQL throws an error.Final Answer:
The two SELECT statements have different numbers of columns. -> Option BQuick Check:
Column count mismatch causes UNION ALL error [OK]
- Thinking table names must match
- Believing UNION ALL disallows SELECT
- Ignoring column count mismatch
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?
Solution
Step 1: Understand requirement to keep duplicates
To keep duplicates, useUNION ALLbecauseUNIONremoves duplicates.Step 2: Check WHERE clause usage
Both queries filter department = 'Sales', so WHERE must be in both queries.Final Answer:
SELECT id, name FROM employees UNION ALL SELECT id, name FROM contractors WHERE department = 'Sales'; Because UNION ALL keeps duplicates. -> Option CQuick Check:
Keep duplicates = UNION ALL with correct WHERE clauses [OK]
- Using UNION instead of UNION ALL
- Omitting WHERE in second query
- Thinking UNION ALL is invalid
Ready to prove it?
Pass the test and this pattern moves unknown
