(SQL Example for Citizen Data Scientist & Business Analyst)
SQL | Union Clause
The Union Clause is used to combine two separate select statements and produce the result set as a union of both the select statements.
NOTE:
- The fields to be used in both the select statements must be in same order, same number and same data type.
- The Union clause produces distinct values in the result set, to fetch the duplicate values too UNION ALL must be used instead of just UNION.
Basic Syntax:
SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2; Resultant set consists of distinct values.
SELECT column_name(s) FROM table1 UNION ALL SELECT column_name(s) FROM table2; Resultant set consists of duplicate values too.
Queries
- To fetch distinct ROLL_NO from Student and Student_Details table.
SELECT ROLL_NO FROM Student UNION SELECT ROLL_NO FROM Student_Details;
Output:
ROLL_NO 1 2 3 4 - To fetch ROLL_NO from Student and Student_Details table including duplicate values.
SELECT ROLL_NO FROM Student UNION ALL SELECT ROLL_NO FROM Student_Details;
Output:
ROLL_NO 1 2 3 4 3 2 - To fetch ROLL_NO , NAME from Student table WHERE ROLL_NO is greater than 3 and ROLL_NO , Branch from Student_Details table WHERE ROLL_NO is less than 3 , including duplicate values and finally sorting the data by ROLL_NO.
SELECT ROLL_NO,NAME FROM Student WHERE ROLL_NO>3 UNION ALL SELECT ROLL_NO,Branch FROM Student_Details WHERE ROLL_NO<3 ORDER BY 1; Note:The column names in both the select statements can be different but the data type must be same.And in the result set the name of column used in the first select statement will appear.
Output:
ROLL_NO NAME 1 Information Technology 2 Computer Science 4 SURESH
MySQL Tutorials for Business Analyst: How to use Unions in MySQL
Learn to Code SQL Example – SQL | Union Clause
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.