JournalSQLFiltering and sets3 min
For Those Who Do Not Know UNION and UNION ALL
I know we didn't cover this in the course content, but it's generally easy and it's better to know it in case you need it in the final exam.
- UNION
- UNION ALL
- duplicates
- set operators
Introduction
I know we didn't cover this in the course content, but it's generally easy and it's better to know it in case you need it in the final exam鈥攜ou never know if you'll be asked about it or asked to write a query that requires UNION. So it's good to be prepared for situations like that 馃檪
Types of Set Operators
We have roughly 6 types of set operators, though I will only talk about the first two for now:
UNIONUNION ALLEXCEPTEXCEPT ALLINTERSECTINTERSECT ALL
As you can see, they are basically 3 main types: Union, Except, and Intersect.
What Are UNION and UNION ALL?
We can think of set operators like Venn diagrams.
Regarding UNION and UNION ALL, if we imagine these two circles represent two tables as shown in the image:

Where:
UNIONcombines all rows/records between both tables without duplicates.UNION ALLcombines all rows/records between both tables even if there are duplicates (that's why the overlapping area between the two circles is shaded in black).
Example
Suppose we have an employees table at a mobile company, for example:
id | name
---|-----------
1 | Zeyad
2 | Zeyad
3 | Zeyad
4 | Mariam
5 | Mariam
6 | Mariam
7 | Mariam
8 | Ahmed
9 | Ahmed
10 | TamerEveryone is Mariam 馃檪
And suppose we have a customers table:
id | name
---|-----------
1 | Zeyad
2 | Zeyad
3 | Ahmed
4 | Ahmed
5 | Ahmed
6 | Ahmed
7 | MariamOne Mariam is enough 馃檪
What Is UNION?
As we said:
UNION combines all rows/records between both tables without duplicates.
To combine rows between any two tables, the general syntax is:
SELECT column_name
FROM first_table
UNION
SELECT column_name
FROM second_tableNotice that I wrote the same column_name because I want to combine values from the same column name in both tables. However, you can of course combine data from two differently named columns.
Note: Be careful鈥攚hen combining two different columns, they must have the same data type. For example, if both columns are INTEGER, then it works fine.
Using UNION

If I ask you to combine the names from the employees table and the customers table without duplicate names:
First, we get name from the employees table:
SELECT name
FROM employeesNext, we get name from the customers table:
SELECT name
FROM customersThen we combine these two queries using UNION:
SELECT name
FROM employees
UNION
SELECT name
FROM customersThe output will be:
| name
|-----------
| Zeyad
| Ahmed
| Mariam
| TamerWhat Is UNION ALL?
As we said:
UNION ALL combines all rows/records between both tables even if there are duplicates (that's why the overlapping area between the two circles is shaded in black).
It is the same as UNION, except UNION ALL keeps all duplicate rows.
Using UNION ALL

If I ask you to combine the name values from the employees table and the customers table even with duplicates, you write:
SELECT name
FROM employees
UNION ALL
SELECT name
FROM customersAnd the output will be:
| name
|-----------
| Zeyad
| Zeyad
| Zeyad
| Zeyad
| Zeyad
| Zeyad
| Mariam
| Mariam
| Mariam
| Mariam
| Mariam
| Ahmed
| Ahmed
| Ahmed
| Ahmed
| Ahmed
| Ahmed
| TamerHere is also an explanation by Coach @abdelkarimoutik that will help you, where he also discusses them in terms of execution speed: https://macdiscussions.udacity.com/t/tips-using-union-and-union-all/70924?u=sayed_hakam&source_topic_id=88644