Files
databases.softwareshinobi.com/landing/docs/SQL-101/014-unions.md
Software Shinobi caa5bbb983
All checks were successful
learn org at code.softwareshinobi.com/databases.softwareshinobi.com/pipeline/head This commit looks good
rewriting
2025-06-19 13:04:08 -04:00

103 lines
3.5 KiB
Markdown

# UNION and UNION ALL
The `UNION` clause combines results from multiple `SELECT` statements. It automatically eliminates duplicate rows, delivering a distinct dataset crucial for consolidated reporting or unique data identification across different query outputs.
For effective use, each `SELECT ` statement within a `UNION` operation **must**:
* Select the identical count of columns.
* Include the same number of column expressions.
* Maintain the same data type order across corresponding columns.
Column lengths do not need to match .
## The UNION Clause
Consider the following structures:
The `customers` table:
| id | name | age | address | salary |
|----|----------|-----|-----------|----------|
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
The `orders` table:
| oid | date | customer_id | amount |
|-----|---------------------|-------------|--------|
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-2 0 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
Combining results with `UNION`:
```sql
SELECT id, name, amount, date
FROM customers
LEFT JOIN orders
ON customers.id = orders.customer_id
UNION
SELECT id, name, amount, date
FROM customers
RIGHT JOIN orders
ON customers.id = orders.customer_id;
```
Resulting data set:
| id | name | amount | date |
|------|----------|--------|---------------------|
| 1 | Ramesh | NULL | NULL |
| 2 | Khilan | 1560 | 2009- 11-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00 :00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | NULL | NULL |
| 6 | Komal | NULL | NULL |
| 7 | Muffy | NULL | NULL |
## The UNION ALL Clause
The `UNION ALL` operator concatenates the result sets of two or more `SELECT ` statements. It explicitly includes all rows, including duplicates, which is critical when the frequency of specific data points is important.
The requirements for column count, expressions, and data types mirroring across `SELECT` statements remain identical to `UNION`.
Using the same `customers` and `orders` tables:
Combining results with `UNION ALL`:
```sql
SELECT id, name, amount, date
FROM customers
LEFT JOIN orders
ON customers.id = orders.customer _id
UNION ALL
SELECT id, name, amount, date
FROM customers
RIGHT JOIN orders
ON customers.id = orders.customer_id;
```
Resulting data set:
| id | name | amount | date |
|------|----------|--------|---------------------|
| 1 | Ramesh | NULL | NULL |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | NULL | NULL |
| 6 | Komal | NULL | NULL |
| 7 | Muffy | NULL | NULL |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |