---
title: "Difference Between UNION vs JOIN in SQL"  
description: "Difference Between UNION vs JOIN in SQL"  
author: "Anubhav Sharma"  
published: 2026-05-12  
updated: 2026-05-12  
canonical: https://www.mindstick.com/interview/34510/difference-between-union-vs-join-in-sql  
category: "database"  
tags: ["database", "sql server"]  
reading_time: 4 minutes  

---

# Difference Between UNION vs JOIN in SQL

`UNION` and `JOIN` are both used to combine data in SQL, but they work very differently.

## UNION

`UNION` combines **rows** from multiple SELECT queries into a single result set.

## Example

```plaintext
SELECT Name FROM Employees
UNION
SELECT Name FROM Customers
```

## Result

```plaintext
John
Aman
Sara
```

It stacks results vertically.

## UNION Rules

- Number of columns must match
- Data types should be compatible
- Removes duplicate rows automatically

## UNION ALL

Keeps duplicates.

```plaintext
SELECT Name FROM Employees
UNION ALL
SELECT Name FROM Customers
```

## JOIN

`JOIN` combines columns from related tables using a condition.

## Example

```plaintext
SELECT
    Orders.OrderId,
    Customers.Name
FROM Orders
INNER JOIN Customers
ON Orders.CustomerId = Customers.Id
```

## Result

```plaintext
101   John
102   Sara
```

It combines data horizontally.

## JOIN Types

| JOIN Type | Meaning |
| --- | --- |
| INNER JOIN | Matching records only |
| LEFT JOIN | All left + matched right |
| RIGHT JOIN | All right + matched left |
| FULL JOIN | All records from both |
| CROSS JOIN | Cartesian product |

## Visual Difference

## UNION

```plaintext
Table A
1
2

Table B
3
4

UNION Result
1
2
3
4
```

## JOIN

```plaintext
Customers          Orders
1 John             101 CustomerId=1

JOIN Result
1 John 101
```

## Main Difference

| Feature | UNION | JOIN |
| --- | --- | --- |
| Combines | Rows | Columns |
| Works On | Multiple SELECT results | Related tables |
| Direction | Vertical | Horizontal |
| Requires Relation | No | Yes |
| Duplicate Handling | Removes duplicates | No duplicate removal |
| Column Count Must Match | Yes | No |

## Real-World Example

## UNION Use Case

Combine data from:

- CurrentEmployees
- FormerEmployees

```plaintext
SELECT Name FROM CurrentEmployees
UNION
SELECT Name FROM FormerEmployees
```

## JOIN Use Case

Show order with customer details.

```plaintext
SELECT
    Customers.Name,
    Orders.Total
FROM Customers
JOIN Orders
ON Customers.Id = Orders.CustomerId
```

## Performance Difference

## UNION

- Slower because duplicates are checked
- `UNION ALL` is faster

## JOIN

Performance depends on:

- Indexes
- Join condition
- Table size

## Conclusion

Use:

- **UNION** → Combines rows vertically
- **JOIN** → Combines columns horizontally

## Answers

### Answer by Anubhav Sharma

`UNION` and `JOIN` are both used to combine data in SQL, but they work very differently.

## UNION

`UNION` combines **rows** from multiple SELECT queries into a single result set.

## Example

```plaintext
SELECT Name FROM Employees
UNION
SELECT Name FROM Customers
```

## Result

```plaintext
John
Aman
Sara
```

It stacks results vertically.

## UNION Rules

- Number of columns must match
- Data types should be compatible
- Removes duplicate rows automatically

## UNION ALL

Keeps duplicates.

```plaintext
SELECT Name FROM Employees
UNION ALL
SELECT Name FROM Customers
```

## JOIN

`JOIN` combines columns from related tables using a condition.

## Example

```plaintext
SELECT
    Orders.OrderId,
    Customers.Name
FROM Orders
INNER JOIN Customers
ON Orders.CustomerId = Customers.Id
```

## Result

```plaintext
101   John
102   Sara
```

It combines data horizontally.

## JOIN Types

| JOIN Type | Meaning |
| --- | --- |
| INNER JOIN | Matching records only |
| LEFT JOIN | All left + matched right |
| RIGHT JOIN | All right + matched left |
| FULL JOIN | All records from both |
| CROSS JOIN | Cartesian product |

## Visual Difference

## UNION

```plaintext
Table A
1
2

Table B
3
4

UNION Result
1
2
3
4
```

## JOIN

```plaintext
Customers          Orders
1 John             101 CustomerId=1

JOIN Result
1 John 101
```

## Main Difference

| Feature | UNION | JOIN |
| --- | --- | --- |
| Combines | Rows | Columns |
| Works On | Multiple SELECT results | Related tables |
| Direction | Vertical | Horizontal |
| Requires Relation | No | Yes |
| Duplicate Handling | Removes duplicates | No duplicate removal |
| Column Count Must Match | Yes | No |

## Real-World Example

## UNION Use Case

Combine data from:

- CurrentEmployees
- FormerEmployees

```plaintext
SELECT Name FROM CurrentEmployees
UNION
SELECT Name FROM FormerEmployees
```

## JOIN Use Case

Show order with customer details.

```plaintext
SELECT
    Customers.Name,
    Orders.Total
FROM Customers
JOIN Orders
ON Customers.Id = Orders.CustomerId
```

## Performance Difference

## UNION

- Slower because duplicates are checked
- `UNION ALL` is faster

## JOIN

Performance depends on:

- Indexes
- Join condition
- Table size

## Conclusion

Use:

- **UNION** → Combines rows vertically
- **JOIN** → Combines columns horizontally


---

Original Source: https://www.mindstick.com/interview/34510/difference-between-union-vs-join-in-sql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
