TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2480 4.66K
๐Ÿ”ฅ Now, Letโ€™s move to the next topic:

โœ… UNION & UNION ALL in SQL

๐Ÿง  1. What is UNION?
UNION is used to combine results from multiple SELECT queries.

"Merge data from two tables into one result.โ€

โšก 2. Rules for UNION
โœ” Same number of columns
โœ” Same datatype/order of columns

๐Ÿ“Š Example Tables

๐Ÿ‘จโ€๐Ÿ’ผ employeesโ‚‚024
name
โ€ข Amit
โ€ข Neha

๐Ÿ‘จโ€๐Ÿ’ผ employeesโ‚‚025
name
โ€ข Ravi
โ€ข Neha

๐Ÿ”ฅ 3. UNION Example

SELECT name FROM employees_2024
UNION
SELECT name FROM employees_2025;


โœ” Removes duplicates automatically

โœ… Result
name
โ€ข Amit
โ€ข Neha
โ€ข Ravi

โšก 4. UNION ALL

SELECT name FROM employees_2024
UNION ALL
SELECT name FROM employees_2025;


โœ” Keeps duplicates
โœ” Faster than UNION

โœ… Result
name
โ€ข Amit
โ€ข Neha
โ€ข Ravi
โ€ข Neha

๐Ÿ”ฅ 5. UNION vs UNION ALL

UNION
โ€ข Removes duplicates
โ€ข Slower
โ€ข Doesn't keep all rows

UNION ALL
โ€ข Doesn't remove duplicates
โ€ข Faster
โ€ข Keeps all rows

โšก 6. ORDER BY with UNION

SELECT name FROM employees_2024
UNION
SELECT name FROM employees_2025
ORDER BY name;


๐ŸŽฏ 7. Practice Tasks
1. Combine employee names using UNION
2. Combine employee names using UNION ALL
3. Identify duplicate removal
4. Sort UNION result using ORDER BY
5. Compare UNION vs UNION ALL output

โšก Mini Challenge ๐Ÿ”ฅ
๐Ÿ‘‰ Combine customer names from two branches and keep duplicates

๐Ÿ”ฅ Mini Challenge Solution ๐Ÿ’ฏ

๐Ÿ‘‰ Since duplicates should remain โ†’ use UNION ALL

โœ… Example Tables

๐Ÿข branch_a_customers
customer_name
โ€ข Amit
โ€ข Neha

๐Ÿข branch_b_customers
customer_name
โ€ข Ravi
โ€ข Neha

โœ… SQL Solution

SELECT customer_name
FROM branch_a_customers

UNION ALL

SELECT customer_name
FROM branch_b_customers;


โœ… Result
customer_name
โ€ข Amit
โ€ข Neha
โ€ข Ravi
โ€ข Neha

โœ” Duplicate Neha is preserved ๐Ÿ’ฏ

๐Ÿง  Why UNION ALL?
๐Ÿ‘‰ UNION โ†’ removes duplicates
๐Ÿ‘‰ UNION ALL โ†’ keeps duplicates + faster

Double Tap โค๏ธ For More
  • โค 9
More from @sqlanalyst
  1. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  2. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  3. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  6. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
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 โ†’