(SQL Tutorials for Citizen Data Scientist)
SQL UNION Operation
In this tutorial you will learn how to combine the results of two or more SQL queries.
The UNION Operator
The UNION
operator is used to combine the results of two or more SELECT
queries into a single result set. The union operation is different from using joins that combine columns from two tables. The union operation creates a new table by placing all rows from two source tables into a single result table, placing the rows on top of one another.
These are basic rules for combining the result sets of two SELECT
queries by using UNION
:
- The number and the order of the columns must be the same in all queries.
- The data types of the corresponding columns must be compatible.
When these criteria are met, the tables are union-compatible:
Syntax
The basic syntax of UNION
can be given with:
column_list
FROM table1_name
UNION SELECT
column_list
FROM table2_name
;To understand the union operation in a better way, let’s assume that some hypothetical fields, like first_name and last_name exists in our employees and customers tables. Please note that these fields do not actually exist in our demo database tables.
+----+------------+-----------+--------+ | id | first_name | last_name | salary | +----+------------+-----------+--------+ | 1 | Ethan | Hunt | 5000 | | 2 | Tony | Montana | 6500 | | 3 | Sarah | Connor | 8000 | | 4 | Rick | Deckard | 7200 | | 5 | Martin | Blank | 5600 | +----+------------+-----------+--------+ |
+----+------------+-----------+----------+ | id | first_name | last_name | city | +----+------------+-----------+----------+ | 1 | Maria | Anders | Berlin | | 2 | Fran | Wilson | Madrid | | 3 | Dominique | Perrier | Paris | | 4 | Martin | Blank | Turin | | 5 | Thomas | Hardy | Portland | +----+------------+-----------+----------+ |
|
Table: employees |
Table: customers |
Let’s perform a union operation to combine the results of two queries.
The following statement returns the first and last names of all the customers and employees:
Example
SELECT first_name, last_name FROM employees
UNION
SELECT first_name, last_name FROM customers;
After executing the above statement, the result set will look something like this:
+---------------+--------------+ | first_name | last_name | +---------------+--------------+ | Ethan | Hunt | | Tony | Montana | | Sarah | Connor | | Rick | Deckard | | Martin | Blank | | Maria | Anders | | Fran | Wilson | | Dominique | Perrier | | Thomas | Hardy | +---------------+--------------+
The UNION
operation eliminates the duplicate rows from the combined result set, by default. That’s why the above query returns only 9 rows, because if you notice the name “Martin Blank” appears in both the employees and customers tables.
However, if you want to keep the duplicate rows you can use the ALL
keyword, as follow:
Example
SELECT first_name, last_name FROM employees
UNION ALL
SELECT first_name, last_name FROM customers;
Beginners Guide to SQL – SQL UNION Operation
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.