SlideShare a Scribd company logo
1 of 13
SQL UNION

Reference :-


       http://www.w3resource.com/sql/sql-union.php



       http://dev.mysql.com/doc/refman/5.0/en/union.html
SQL UNION


Description:-


The SQL UNION operator combines the results of two or more queries and makes a
result set which includes fetched rows from the participating queries in the UNION.
Basic rules for combining two or more queries using UNION :
1.) number of columns and order of columns of all queries must be same.
2.) the data types of the columns on involving table in each query must be same or
compatible.
3.) Usually returned column names are taken from the first query.
By default the UNION behalves like UNION [DISTINCT] , i.e. eliminated the duplicate
rows; however, using ALL keyword with UNION returns all rows, including duplicates.
SQL UNION

Example of SQL UNION


Code:-

         SELECT prod_code,prod_name

         FROM product

         UNION

         SELECT prod_code,prod_name

         FROM purchase;
SQL UNION ALL

Example of SQL UNION ALL


Code:-

         SELECT prod_code,prod_name,com_name

         FROM product

         UNION ALL

         SELECT prod_code,prod_name,com_name

         FROM purchase;
SQL UNION ALL

Explanation



 In the above example the optional clause ALL have been added with UNION for which,
 all the rows from each query have been available in the result set. Here in the above
 output the marking rows are non-unique but it has been displayed. If ignored ALL
 clause, the marking rows would have come once.
Sql UNION ALL using where

   Example of Sql UNION ALL using where

Code:-
           SELECT prod_code,prod_name,com_name

           FROM product

           WHERE life>6

           UNION ALL

           SELECT prod_code,prod_name,com_name

           FROM purchase

           WHERE pur_qty>10
Sql UNION ALL using where


Explanation:-



 In the above example the two queries have been set using two different criterias
 including WHERE clause. So all the retrieve rows (including duplicates) have displayed
 in the result set. Here in this example the marking rows are identical, but it has been
 displayed for the ALL clause along with UNION. If ignored ALL clause the marking rows
 would have come once.
Sql UNION a table to itself

    Example of Sql UNION a table to itself

Code:-
           SELECT prod_code,prod_name,com_name

           FROM purchase

           WHERE pur_qty>6

           UNION ALL

           SELECT prod_code,prod_name,com_name

           FROM purchase

           WHERE pur_amount>100000
Sql UNION a table to itself


Explanation:-

 In the above example the two queries have been set using two different criterias for a
 same table. So all the retrieved rows ( including duplicates ) have displayed. Here in
 this example the marking rows are identical, but it has been displayed for the ALL
 clause along with UNION.
Sql UNION with different column names

     Example of Sql UNION a table to itself

 Code:-
          SELECT prod_code,prod_name,life

          FROM product

          WHERE life>6

          UNION

          SELECT prod_code,prod_name,pur_qty

          FROM purchase

          WHERE pur_qty<20
Sql UNION with different column names


Explanation:-



 n the above example the two queries have been set using two different criterias and
 different columns. The different columns in two statements are 'life' and 'pur_qty'. But
 as the data type are same for both the columns so, result have displayed. Usually
 returned column names are taken from the first query.
Sql UNION with Inner Join
         Example of Sql UNION a table to itself
          SELECT product.prod_code,product.prod_name,
Code:-
          purchase.pur_qty, purchase.pur_amount

          FROM product

          INNER JOIN purchase

          ON product.prod_code =purchase.prod_code

          UNION

          SELECT product.prod_code,product.prod_name,

          purchase.pur_qty, purchase.pur_amount

          FROM product

          INNER JOIN purchase

          ON product.prod_name =purchase.prod_name;
Sql UNION with Inner Join


Explanation:-



 n the above example the union made by two queries. The queries are two inner join
 statement. In the first query the join take place between two tables where the
 prod_code of both tables are same and in the 2nd query the join take place between
 two tables where the prod_name of both tables are same.

More Related Content

What's hot (20)

SQL Joins.pptx
SQL Joins.pptxSQL Joins.pptx
SQL Joins.pptx
 
Sql join
Sql  joinSql  join
Sql join
 
Mysql joins
Mysql joinsMysql joins
Mysql joins
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
SQL for interview
SQL for interviewSQL for interview
SQL for interview
 
Normalization in SQL | Edureka
Normalization in SQL | EdurekaNormalization in SQL | Edureka
Normalization in SQL | Edureka
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 
Triggers
TriggersTriggers
Triggers
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
 
SQL commands
SQL commandsSQL commands
SQL commands
 
SQL
SQLSQL
SQL
 
joins in database
 joins in database joins in database
joins in database
 
Advanced sql
Advanced sqlAdvanced sql
Advanced sql
 
Subqueries
SubqueriesSubqueries
Subqueries
 

Similar to SQL UNION

Similar to SQL UNION (20)

Sql ch 5
Sql ch 5Sql ch 5
Sql ch 5
 
Sql clauses by Manan Pasricha
Sql clauses by Manan PasrichaSql clauses by Manan Pasricha
Sql clauses by Manan Pasricha
 
Sql Tuning
Sql TuningSql Tuning
Sql Tuning
 
Sql wksht-6
Sql wksht-6Sql wksht-6
Sql wksht-6
 
Chapter9 more on database and sql
Chapter9 more on database and sqlChapter9 more on database and sql
Chapter9 more on database and sql
 
Sql
SqlSql
Sql
 
Sql joins
Sql joinsSql joins
Sql joins
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
 
Les07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column SubqueriesLes07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column Subqueries
 
advanced sql(database)
advanced sql(database)advanced sql(database)
advanced sql(database)
 
OER UNIT 5 SQL MODEL CLAUSE
OER UNIT 5 SQL MODEL CLAUSEOER UNIT 5 SQL MODEL CLAUSE
OER UNIT 5 SQL MODEL CLAUSE
 
1 z0 047
1 z0 0471 z0 047
1 z0 047
 
Oracle: Joins
Oracle: JoinsOracle: Joins
Oracle: Joins
 
Oracle: Joins
Oracle: JoinsOracle: Joins
Oracle: Joins
 
The Database Environment Chapter 8
The Database Environment Chapter 8The Database Environment Chapter 8
The Database Environment Chapter 8
 
235689260 oracle-forms-10g-tutorial
235689260 oracle-forms-10g-tutorial235689260 oracle-forms-10g-tutorial
235689260 oracle-forms-10g-tutorial
 
5. Group Functions
5. Group Functions5. Group Functions
5. Group Functions
 
ADV PPT 8 LAB.pptx
ADV PPT 8 LAB.pptxADV PPT 8 LAB.pptx
ADV PPT 8 LAB.pptx
 
How to work with Subquery in Data Mining?
How to work with Subquery in Data Mining?How to work with Subquery in Data Mining?
How to work with Subquery in Data Mining?
 

Recently uploaded

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 

Recently uploaded (20)

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 

SQL UNION

  • 1. SQL UNION Reference :- http://www.w3resource.com/sql/sql-union.php http://dev.mysql.com/doc/refman/5.0/en/union.html
  • 2. SQL UNION Description:- The SQL UNION operator combines the results of two or more queries and makes a result set which includes fetched rows from the participating queries in the UNION. Basic rules for combining two or more queries using UNION : 1.) number of columns and order of columns of all queries must be same. 2.) the data types of the columns on involving table in each query must be same or compatible. 3.) Usually returned column names are taken from the first query. By default the UNION behalves like UNION [DISTINCT] , i.e. eliminated the duplicate rows; however, using ALL keyword with UNION returns all rows, including duplicates.
  • 3. SQL UNION Example of SQL UNION Code:- SELECT prod_code,prod_name FROM product UNION SELECT prod_code,prod_name FROM purchase;
  • 4. SQL UNION ALL Example of SQL UNION ALL Code:- SELECT prod_code,prod_name,com_name FROM product UNION ALL SELECT prod_code,prod_name,com_name FROM purchase;
  • 5. SQL UNION ALL Explanation In the above example the optional clause ALL have been added with UNION for which, all the rows from each query have been available in the result set. Here in the above output the marking rows are non-unique but it has been displayed. If ignored ALL clause, the marking rows would have come once.
  • 6. Sql UNION ALL using where Example of Sql UNION ALL using where Code:- SELECT prod_code,prod_name,com_name FROM product WHERE life>6 UNION ALL SELECT prod_code,prod_name,com_name FROM purchase WHERE pur_qty>10
  • 7. Sql UNION ALL using where Explanation:- In the above example the two queries have been set using two different criterias including WHERE clause. So all the retrieve rows (including duplicates) have displayed in the result set. Here in this example the marking rows are identical, but it has been displayed for the ALL clause along with UNION. If ignored ALL clause the marking rows would have come once.
  • 8. Sql UNION a table to itself Example of Sql UNION a table to itself Code:- SELECT prod_code,prod_name,com_name FROM purchase WHERE pur_qty>6 UNION ALL SELECT prod_code,prod_name,com_name FROM purchase WHERE pur_amount>100000
  • 9. Sql UNION a table to itself Explanation:- In the above example the two queries have been set using two different criterias for a same table. So all the retrieved rows ( including duplicates ) have displayed. Here in this example the marking rows are identical, but it has been displayed for the ALL clause along with UNION.
  • 10. Sql UNION with different column names Example of Sql UNION a table to itself Code:- SELECT prod_code,prod_name,life FROM product WHERE life>6 UNION SELECT prod_code,prod_name,pur_qty FROM purchase WHERE pur_qty<20
  • 11. Sql UNION with different column names Explanation:- n the above example the two queries have been set using two different criterias and different columns. The different columns in two statements are 'life' and 'pur_qty'. But as the data type are same for both the columns so, result have displayed. Usually returned column names are taken from the first query.
  • 12. Sql UNION with Inner Join Example of Sql UNION a table to itself SELECT product.prod_code,product.prod_name, Code:- purchase.pur_qty, purchase.pur_amount FROM product INNER JOIN purchase ON product.prod_code =purchase.prod_code UNION SELECT product.prod_code,product.prod_name, purchase.pur_qty, purchase.pur_amount FROM product INNER JOIN purchase ON product.prod_name =purchase.prod_name;
  • 13. Sql UNION with Inner Join Explanation:- n the above example the union made by two queries. The queries are two inner join statement. In the first query the join take place between two tables where the prod_code of both tables are same and in the 2nd query the join take place between two tables where the prod_name of both tables are same.