MySQL Tutorials for Business Analyst: How to use Unions in MySQL

What is a union?

Unions combine the results from multiple SELECT queries into a consolidated result set.

The only requirements for this to work is that the number of columns should be the same from all the SELECT queries which needs to be combined .

Suppose we have two tables as follows

MySQL UNION - Complete TutorialMySQL UNION - Complete Tutorial

 

Let’s now create a UNION query to combines both tables using DISTINCT

SELECT column1, column2 FROM `table1`
UNION DISTINCT
SELECT  column1,column2  FROM `table2`;

 

Here duplicate rows are removed and only unique rows are returned.

MySQL UNION - Complete Tutorial

Note: MySQL uses the DISTINCT clause as default when executing UNION queries if nothing is specified.

Let’s now create a UNION query to combines both tables using ALL

SELECT `column1`,` column1` FROM `table1`
UNION ALL
SELECT ` column1`,` column1`  FROM `table2`;

Here duplicate rows are included and since we use ALL.

MySQL UNION - Complete Tutorial

Why use unions

Suppose there is a flaw in your database design and you are using two different tables meant for the same purpose. You want to consolidate these two tables into one while omitting any duplicate records from creeping into the new table. You can use UNION in such cases.

Summary

  • The UNION command is used to combine more than one SELECT query results into a single query contain rows from all the select queries.
  • The number of columns and data types in the SELECT statements must be the same in order for the UNION command to work.
  • The DISTINCT clause is used to eliminate duplicate values from the UNION query result set. MySQL uses the DISTINCT clause as the default when executing UNION queries if nothing is specified.
  • The ALL clause is used to return all even the duplicate rows in the UNION query.

Practical examples using MySQL workbench

In our myFlixDB lets combine

membership_number and full_names from Members table

with

movie_id and title from movies table

We can use the following query

SELECT `membership_number`,`full_names` FROM `members`
UNION
SELECT `movie_id`,`title` FROM `movies`;

Executing the above script in MySQL workbench against the myflixdb gives us the following results shown below.

membership_number full_names
1 Janet Jones
2 Janet Smith Jones
3 Robert Phil
4 Gloria Williams
5 Leonard Hofstadter
6 Sheldon Cooper
7 Rajesh Koothrappali
8 Leslie Winkle
9 Howard Wolowitz
16 67% Guilty
6 Angels and Demons
4 Code Name Black
5 Daddy’s Little Girls
7 Davinci Code
2 Forgetting Sarah Marshal
9 Honey mooners
19 movie 3
1 Pirates of the Caribean 4
18 sample movie
17 The Great Dictator
3 X-Men

 

 

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!

 

Python Example – Write a Python program to create a union of sets.

Beginners Guide to SQL – SQL UNION Operation

PostgreSQL tutorial for Beginners – PostgreSQL – UNIONS Clause