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
Recall & Review
beginner
What does the SQL UNION operator do?
The UNION operator combines the results of two or more SELECT queries into a single result set, removing duplicate rows.
Click to reveal answer
beginner
How does UNION differ from UNION ALL?
UNION removes duplicate rows from the combined result, while UNION ALL includes all rows, even duplicates.
Click to reveal answer
intermediate
What must be true about the SELECT statements used with UNION?
Each SELECT must have the same number of columns, and the columns must have compatible data types in the same order.
Click to reveal answer
intermediate
Can you use ORDER BY with UNION? If yes, where?
Yes, ORDER BY can be used only once at the end of the entire UNION query to sort the combined result set.
Click to reveal answer
intermediate
What happens if the SELECT statements in a UNION have different column names?
The column names in the final result come from the first SELECT statement; later SELECTs' column names are ignored.
Click to reveal answer
What does the UNION operator do in SQL?
ACombines results and keeps duplicates
BFilters rows based on a condition
CJoins tables based on a key
DCombines results and removes duplicates
✗ Incorrect
UNION combines results from multiple SELECTs and removes duplicate rows.
Which of the following is true about SELECT statements in a UNION?
AThey must have the same number of columns
BThey can have different numbers of columns
CThey must have the same column names
DThey must come from the same table
✗ Incorrect
All SELECTs in a UNION must have the same number of columns with compatible data types.
What is the difference between UNION and UNION ALL?
AUNION ALL removes duplicates; UNION keeps duplicates
BUNION removes duplicates; UNION ALL keeps duplicates
CUNION sorts results; UNION ALL does not
DUNION works only with two SELECTs; UNION ALL works with many
✗ Incorrect
UNION removes duplicate rows, while UNION ALL includes all rows.
Where can you place ORDER BY when using UNION?
AOnly at the end of the entire UNION query
BAfter each SELECT statement
CBefore the first SELECT
DORDER BY is not allowed with UNION
✗ Incorrect
ORDER BY can only be used once at the end to sort the combined result.
If the first SELECT has columns (id, name) and the second SELECT has columns (user_id, username), what will the final column names be after UNION?
Auser_id, name
Buser_id, username
Cid, name
Did, username
✗ Incorrect
The final column names come from the first SELECT statement.
Explain how the UNION operator works and what rules must be followed when combining SELECT statements.
Think about how two lists of data can be merged without repeats.
You got /5 concepts.
Describe the difference between UNION and UNION ALL and when you might use each.
Consider if you want to keep or remove repeated rows.
You got /4 concepts.
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
Step 1: Understand UNION behavior
The UNION operator combines rows from two or more SELECT queries into a single result set.
Step 2: Check duplicate handling
By default, UNION removes duplicate rows to ensure unique results.
Final Answer:
Combines rows from both queries and removes duplicate rows -> Option B
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
Step 1: Review correct UNION syntax
The correct syntax is to write two SELECT statements separated by the UNION keyword.
Step 2: Identify invalid options
Options B, C, and D misuse JOIN or WHERE with UNION, which is incorrect syntax.
Final Answer:
SELECT col1 FROM table1 UNION SELECT col1 FROM table2; -> Option D
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
Step 1: List rows from both tables
Table A has ids 1, 2, 3; Table B has ids 2, 3, 4.
Step 2: Apply UNION behavior
UNION combines all rows and removes duplicates, so final list is 1, 2, 3, 4.
Final Answer:
[1, 2, 3, 4] -> Option A
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
Step 1: Check column counts in SELECTs
The first SELECT returns 1 column (name), the second returns 2 columns (name, department).
Step 2: Understand UNION column rules
UNION requires all SELECT statements to have the same number of columns with compatible types.
Final Answer:
The number of columns in both SELECT statements differ -> Option A
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
Step 1: Select product column from both tables
We want unique products, so select only the product column from both tables.
Step 2: Use UNION to combine and remove duplicates
UNION combines both lists and removes duplicates, giving unique products.
Step 3: Sort results alphabetically
ORDER BY product sorts the final list alphabetically.
Final Answer:
SELECT product FROM Sales2023 UNION SELECT product FROM Sales2024 ORDER BY product; -> Option C
Quick Check:
UNION + ORDER BY product = B [OK]
Hint: Use UNION to remove duplicates, ORDER BY to sort [OK]