If we have more selections and we want to combine their results, we can use UNION. It is used if we are only interested in unique values.
If we want all records and do not consider the uniqueness of the result, we use UNION ALL. What needs to be met?
Must take care to ensure that the selections match the type. This means that they must have the same number of fields, data types, and also all fields should be in the same order.
Structure:
SELECT column_name FROM table_name1 UNION SELECT column_name FROM table_name2;
Example1:
We have tables with this data:
Table Staff
STAFFID | STAFF_NAME | SUPERVISORID |
---|---|---|
1 | Frank | 3 |
2 | Helena | 3 |
3 | Julia | - |
4 | Thomas | 2 |
Table Friends
FRIENDID | FRIEND | COLORID |
---|---|---|
1 | Frank | 1 |
2 | Helena | 2 |
3 | Julia | - |
4 | Thomas | 4 |
SELECT Staff_Name FROM Staff UNION SELECT Friend FROM Friends;
As a result, we see that unique values return to us.
STAFF_NAME |
---|
Frank |
Helena |
Julia |
Thomas |
Example2:
We have tables with this data:
Table Staff
STAFFID | STAFF_NAME | SUPERVISORID |
---|---|---|
1 | Frank | 3 |
2 | Helena | 3 |
3 | Julia | - |
4 | Thomas | 2 |
Table Friends
FRIENDID | FRIEND | COLORID |
---|---|---|
1 | Frank | 1 |
2 | Helena | 2 |
3 | Julia | - |
4 | Thomas | 4 |
SELECT Staff_Name FROM Staff UNION ALL SELECT Friend FROM Friends;
STAFF_NAME |
---|
Frank |
Helena |
Julia |
Thomas |
Frank |
Helena |
Julia |
Thomas |
Example3:
We have tables with this data:
Table Customer
CustomerID | Name | Hair | Salary |
---|---|---|---|
1 | Alechandro | brown | 100 |
2 | Katerina | blonde | 150 |
3 | Lucie | blonde | 30 |
4 | Thomas | black | 40 |
5 | Theodor | black | 120 |
6 | Thomassino | black | 99 |
7 | Knor | brown | 50 |
8 | Thor | blonde | 10 |
9 | Lucie | pink | 70 |
Table Friends
FRIENDID | FRIEND | COLORID |
---|---|---|
1 | Frank | 1 |
2 | Helena | 2 |
3 | Julia | - |
4 | Thomas | 4 |
SELECT NAME FROM Customer UNION SELECT Friend FROM Friends;
As a result, we see that unique values return to us.
NAME |
---|
Alechandro |
Frank |
Helena |
Julia |
Katerina |
Knor |
Lucie |
Theodor |
Thomas |
Thomassino |
Thor |
Example4:
We have tables with this data:
Table Customer
CustomerID | Name | Hair | Salary |
---|---|---|---|
1 | Alechandro | brown | 100 |
2 | Katerina | blonde | 150 |
3 | Lucie | blonde | 30 |
4 | Thomas | black | 40 |
5 | Theodor | black | 120 |
6 | Thomassino | black | 99 |
7 | Knor | brown | 50 |
8 | Thor | blonde | 10 |
9 | Lucie | pink | 70 |
Table Friends
FRIENDID | FRIEND | COLORID |
---|---|---|
1 | Frank | 1 |
2 | Helena | 2 |
3 | Julia | - |
4 | Thomas | 4 |
SELECT NAME FROM Customer UNION ALL SELECT Friend FROM Friends;
NAME |
---|
Alechandro |
Katerina |
Lucie |
Thomas |
Theodor |
Thomassino |
Knor |
Thor |
Lucie |
Frank |
Helena |
Julia |
Thomas |
Example5:
We have tables with this data:
Table Customer
CustomerID | Name | Hair | Salary |
---|---|---|---|
1 | Alechandro | brown | 100 |
2 | Katerina | blonde | 150 |
3 | Lucie | blonde | 30 |
4 | Thomas | black | 40 |
5 | Theodor | black | 120 |
6 | Thomassino | black | 99 |
7 | Knor | brown | 50 |
8 | Thor | blonde | 10 |
9 | Lucie | pink | 70 |
Table Friends
FRIENDID | FRIEND | COLORID |
---|---|---|
1 | Frank | 1 |
2 | Helena | 2 |
3 | Julia | - |
4 | Thomas | 4 |
SELECT NAME FROM Customer WHERE HAIR = 'brown' UNION SELECT Friend FROM Friends WHERE FRIEND = 'Frank';
We can also use the WHERE condition:
NAME |
---|
Alechandro |
Frank |
Knor |
Example6:
We have tables with this data:
Table Customer
CustomerID | Name | Hair | Salary |
---|---|---|---|
1 | Alechandro | brown | 100 |
2 | Katerina | blonde | 150 |
3 | Lucie | blonde | 30 |
4 | Thomas | black | 40 |
5 | Theodor | black | 120 |
6 | Thomassino | black | 99 |
7 | Knor | brown | 50 |
8 | Thor | blonde | 10 |
9 | Lucie | pink | 70 |
Table Friends
FRIENDID | FRIEND | COLORID |
---|---|---|
1 | Frank | 1 |
2 | Helena | 2 |
3 | Julia | - |
4 | Thomas | 4 |
SELECT NAME FROM Customer WHERE HAIR = 'black' UNION ALL SELECT Friend FROM Friends WHERE FRIEND = 'Thomas';
We can also use the WHERE condition:
NAME |
---|
Thomas |
Theodor |
Thomassino |
Thomas |
Example7:
We have tables with this data:
Table Customer
CustomerID | Name | Hair | Salary |
---|---|---|---|
1 | Alechandro | brown | 100 |
2 | Katerina | blonde | 150 |
3 | Lucie | blonde | 30 |
4 | Thomas | black | 40 |
5 | Theodor | black | 120 |
6 | Thomassino | black | 99 |
7 | Knor | brown | 50 |
8 | Thor | blonde | 10 |
9 | Lucie | pink | 70 |
Table Friends
FRIENDID | FRIEND | COLORID |
---|---|---|
1 | Frank | 1 |
2 | Helena | 2 |
3 | Julia | - |
4 | Thomas | 4 |
SELECT NAME AS Hero_Name FROM Customer WHERE HAIR = 'black' UNION SELECT Friend FROM Friends WHERE FRIEND = 'Thomas';
We can also use the WHERE condition:
HERO_Name |
---|
Theodor |
Thomas |
Thomassino |
I give people clear, functional, and proven instructions. I gently guide them so that they can fulfill their IT dreams.