Learn to Code SQL Example – SQL | Union Clause

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

  1. The fields to be used in both the select statements must be in same order, same number and same data type.
  2. 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.

table1

table12

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

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!