Q1. What is UNION? - Combines results from multiple SELECT statements and removes duplicate rows.
Q2. What is UNION ALL? - Combines results while retaining duplicates.
Q3. Which is generally faster: UNION or UNION ALL? - UNION ALL is generally faster when duplicate removal isn't required.
Q4. What is the difference between UNION and JOIN? - JOIN combines related tables horizontally by adding columns. UNION combines compatible result sets vertically by adding rows.
Q5. What does INTERSECT do? - Returns rows common to both result sets.
Q6. What does EXCEPT do? - Returns rows present in the first result set but absent from the second.
Q7. What is MINUS? - In Oracle, MINUS is commonly used for the same type of operation as EXCEPT.
Q8. Do the columns have to have the same names? - No. The queries need compatible column positions and data types.
Q9. Can you use ORDER BY with UNION? - Yes. Normally, place the final ORDER BY after the complete set operation.
Q10. When would you choose UNION ALL instead of UNION? - When duplicate rows are valid or when you know the inputs are already unique.
๐ง Practice Questions
Practice 1 - Combine customers from two tables while keeping duplicates.
SELECT customer_id FROM customers_a
UNION ALL
SELECT customer_id FROM customers_b;
Practice 2 - Find customers who appear in both tables.
SELECT customer_id FROM customers_a
INTERSECT
SELECT customer_id FROM customers_b;
Practice 3 - Find customers who appear in customers_a but not customers_b.
SELECT customer_id FROM customers_a
EXCEPT
SELECT customer_id FROM customers_b;
Practice 4 - Combine two years of transactions and calculate total transaction value.
SELECT SUM(amount) AS total_amount FROM (
SELECT amount FROM transactions_2025
UNION ALL
SELECT amount FROM transactions_2026
) t;
Practice 5 - Find unique customer IDs across two sales channels.
SELECT customer_id FROM online_sales
UNION
SELECT customer_id FROM store_sales;
๐ฅ Mini Challenge
You have two tables: orders_2025 and orders_2026 with columns: order_id, customer_id, order_amount
Write SQL to:
1. Combine all orders from both years.
2. Calculate total order value.
3. Calculate total number of orders.
4. Find customers who ordered in both years.
5. Find customers who ordered in 2025 but not in 2026.
Solution:
-- 1. Combine all orders
SELECT order_id, customer_id, order_amount FROM orders_2025
UNION ALL
SELECT order_id, customer_id, order_amount FROM orders_2026;
-- 2. Total order value
SELECT SUM(order_amount) AS total_order_value FROM (
SELECT order_amount FROM orders_2025
UNION ALL
SELECT order_amount FROM orders_2026
) o;
-- 3. Total number of orders
SELECT COUNT(*) AS total_orders FROM (
SELECT order_id FROM orders_2025
UNION ALL
SELECT order_id FROM orders_2026
) o;
-- 4. Customers who ordered in both years
SELECT customer_id FROM orders_2025
INTERSECT
SELECT customer_id FROM orders_2026;
-- 5. Customers who ordered in 2025 but not 2026
SELECT customer_id FROM orders_2025
EXCEPT
SELECT customer_id FROM orders_2026;
๐ฏ Double Tap โค๏ธ For More