TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2794 1.5K
๐Ÿ’ผ Interview Questions

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
  • โค 5
More from @sqlanalyst
  1. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  2. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’