Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

JOIN vs UNION & Set Operators
SQL

JOIN vs UNION & Set Operators

Understand the fundamental difference between combining columns (JOIN) and combining rows (UNION).

While both JOIN and UNION allow you to combine data from multiple tables, they do so in completely different ways.

JOIN (Horizontal Combination)

Joins combine columns from different tables. Use it when you want to see related data side-by-side.

Users: {id, name}  +  Orders: {id, user_id, amount}
     → Result: {user.name, order.amount}

UNION (Vertical Combination)

Unions combine rows from different tables. Use it when you want to stack results from one table on top of another.

Q1_Results: {name, sales}  +  Q2_Results: {name, sales}
     → Result: {name, sales}  -- all rows from both quarters stacked

UNION vs UNION ALL

ClauseBehaviourPerformance
UNIONRemoves duplicate rowsSlower (requires sort/distinct)
UNION ALLKeeps all rows (including duplicates)Faster (simple append)
-- UNION: removes duplicates
SELECT name FROM Customers_A
UNION
SELECT name FROM Customers_B;

-- UNION ALL: keeps duplicates
SELECT name FROM Customers_A
UNION ALL
SELECT name FROM Customers_B;

Requirements for UNION

Both queries must have:

  1. The same number of columns
  2. Compatible data types in the same order
  3. Column names from the first query determine output column names

INTERSECT

Returns rows that appear in both result sets:

SELECT name FROM Customers_A
INTERSECT
SELECT name FROM Customers_B;

EXCEPT (or MINUS in Oracle)

Returns rows from the first query that are not in the second:

SELECT name FROM Customers_A
EXCEPT
SELECT name FROM Customers_B;

Order of Operations

When combining set operators, use parentheses to control precedence. Without parentheses, INTERSECT binds more tightly than UNION and EXCEPT in most databases.

Practical Use Cases

  • UNION: Combining monthly sales tables, merging archived data with live data.
  • INTERSECT: Finding customers who bought in both campaigns.
  • EXCEPT: Finding products that were never sold, identifying discrepancies between two systems.

Q: Difference between UNION and UNION ALL?

A:

  • UNION: Combines row sets and removes duplicates. It is slower because it requires a distinct check.
  • UNION ALL: Combines row sets but keeps duplicates. It is faster because it simply appends the data.

Q: What is INTERSECT and EXCEPT?

A:

  • INTERSECT: Returns only the rows that are present in both query results.
  • EXCEPT (or MINUS in Oracle): Returns rows from the first query that are not present in the second query.

Q: What are the requirements for using UNION?

A: Both queries must have the same number of columns, and the columns must have compatible data types in the same order.

Q: Can you use ORDER BY with UNION?

A: Yes, but only at the end of the entire UNION query. You cannot sort individual SELECT statements within a UNION — you must sort the final combined result.

1. Combine two customer tables removing duplicates.

SELECT name, email FROM Customers_A
UNION
SELECT name, email FROM Customers_B;

2. Find customers present in both tables.

SELECT name, email FROM Customers_A
INTERSECT
SELECT name, email FROM Customers_B;

3. Find customers in A but not in B.

SELECT name, email FROM Customers_A
EXCEPT
SELECT name, email FROM Customers_B;

4. Remove duplicates without using DISTINCT.

-- Using UNION on the same table effectively removes duplicates
SELECT name FROM employees
UNION
SELECT name FROM employees;

5. Demonstrate UNION ALL behavior.

-- If 'John' exists in both, he will appear twice in the result
SELECT name FROM Table_1
UNION ALL
SELECT name FROM Table_2;

6. Combine sales data from two years with sorting.

SELECT product_id, sales_amount FROM sales_2023
UNION ALL
SELECT product_id, sales_amount FROM sales_2024
ORDER BY product_id;

My Private Notes

Notes are auto-saved locally to this device.