Beginners Guide to SQL – SQL UNION Operation

Hits: 43

(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:

SELECT 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

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

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

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!