SlideShare ist ein Scribd-Unternehmen logo
1 von 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.

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Where conditions and Operators in SQL
Where conditions and Operators in SQLWhere conditions and Operators in SQL
Where conditions and Operators in SQL
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
joins in database
 joins in database joins in database
joins in database
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
Using the set operators
Using the set operatorsUsing the set operators
Using the set operators
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
Group By, Order By, and Aliases in SQL
Group By, Order By, and Aliases in SQLGroup By, Order By, and Aliases in SQL
Group By, Order By, and Aliases in SQL
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Mysql joins
Mysql joinsMysql joins
Mysql joins
 
Types of keys in database | SQL
Types of keys in database | SQLTypes of keys in database | SQL
Types of keys in database | SQL
 
Triggers
TriggersTriggers
Triggers
 
Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
SQL Joins and Query Optimization
SQL Joins and Query OptimizationSQL Joins and Query Optimization
SQL Joins and Query Optimization
 
SQL Join Basic
SQL Join BasicSQL Join Basic
SQL Join Basic
 

Ähnlich wie SQL UNION

Ähnlich wie 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
 
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?
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
 

Kürzlich hochgeladen

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 

Kürzlich hochgeladen (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

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.