SlideShare a Scribd company logo
2
Most read
3
Most read
11
Most read
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

PPTX
Sql clauses by Manan Pasricha
MananPasricha
 
PPTX
SQL - DML and DDL Commands
Shrija Madhu
 
PPT
Introduction to structured query language (sql)
Sabana Maharjan
 
PPT
Advanced sql
Dhani Ahmad
 
PPTX
Inner join and outer join
Nargis Ehsan
 
PPTX
Sql joins inner join self join outer joins
Deepthi Rachumallu
 
PPTX
Sql queries presentation
NITISH KUMAR
 
PPT
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Oum Saokosal
 
Sql clauses by Manan Pasricha
MananPasricha
 
SQL - DML and DDL Commands
Shrija Madhu
 
Introduction to structured query language (sql)
Sabana Maharjan
 
Advanced sql
Dhani Ahmad
 
Inner join and outer join
Nargis Ehsan
 
Sql joins inner join self join outer joins
Deepthi Rachumallu
 
Sql queries presentation
NITISH KUMAR
 
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Oum Saokosal
 

What's hot (20)

PPT
Aggregate functions
sinhacp
 
PPT
1 - Introduction to PL/SQL
rehaniltifat
 
PPTX
SQL commands
GirdharRatne
 
PDF
Sql tutorial
Rumman Ansari
 
PPTX
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
PPTX
SQL Join Basic
Naimul Arif
 
PPT
Mysql
TSUBHASHRI
 
PPTX
4. plsql
Amrit Kaur
 
PPT
SQL.ppt
Ranjit273515
 
PPTX
Integrity Constraints
Megha yadav
 
PPTX
SQL Basics
Hammad Rasheed
 
PPTX
Oraclesql
Priya Goyal
 
PPTX
Database constraints
Khadija Parween
 
PPT
Types Of Join In Sql Server - Join With Example In Sql Server
programmings guru
 
PPTX
Basic sql Commands
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PDF
SQL JOINS
Swapnali Pawar
 
PPTX
SQL
Vineeta Garg
 
PPTX
SQL Commands
Sachidananda M H
 
PPT
Retrieving data using the sql select statement
Syed Zaid Irshad
 
Aggregate functions
sinhacp
 
1 - Introduction to PL/SQL
rehaniltifat
 
SQL commands
GirdharRatne
 
Sql tutorial
Rumman Ansari
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
SQL Join Basic
Naimul Arif
 
Mysql
TSUBHASHRI
 
4. plsql
Amrit Kaur
 
SQL.ppt
Ranjit273515
 
Integrity Constraints
Megha yadav
 
SQL Basics
Hammad Rasheed
 
Oraclesql
Priya Goyal
 
Database constraints
Khadija Parween
 
Types Of Join In Sql Server - Join With Example In Sql Server
programmings guru
 
Basic sql Commands
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
SQL JOINS
Swapnali Pawar
 
SQL Commands
Sachidananda M H
 
Retrieving data using the sql select statement
Syed Zaid Irshad
 
Ad

Viewers also liked (7)

PPT
Sql Patterns
phanleson
 
PPT
Oracle sql joins
redro
 
PPTX
Flash Presentation
Samuel Adams, MBA
 
PPTX
Oracle: Joins
oracle content
 
PPTX
Sql joins
Gaurav Dhanwant
 
PPTX
SQL JOIN
Ritwik Das
 
PPT
SQL Injection
Adhoura Academy
 
Sql Patterns
phanleson
 
Oracle sql joins
redro
 
Flash Presentation
Samuel Adams, MBA
 
Oracle: Joins
oracle content
 
Sql joins
Gaurav Dhanwant
 
SQL JOIN
Ritwik Das
 
SQL Injection
Adhoura Academy
 
Ad

Similar to SQL UNION (20)

PDF
Sql joins
Vivek Singh
 
PPTX
MYSQL using set operators
Ahmed Farag
 
PPTX
ADV PPT 8 LAB.pptx
ArjayBalberan1
 
PPTX
MySQL JOIN & UNION
Jamshid Hashimi
 
PDF
Sql wksht-6
Mukesh Tekwani
 
PPT
Intro to tsql unit 4
Syed Asrarali
 
PPT
Intro To TSQL - Unit 4
iccma
 
PPTX
Oracle: Joins
DataminingTools Inc
 
PPTX
Understanding Structured Query Language fundamentals
Ankit Rao
 
PDF
Sql ch 5
Mukesh Tekwani
 
PDF
DBMS Nested & Sub Queries Set operations
Sreedhar Chowdam
 
PPT
Ms sql server ii
Iblesoft
 
PPTX
sql joinsubdjbrjdbjrjnfkjcnkrnfknrkfkrfkrfkrk
kailasmanoj
 
PPTX
sqlyyybdbyehduheufhuehfuheuwehfiewifhewihfiehfiwf
kailasmanoj
 
PDF
Chapter9 more on database and sql
KV(AFS) Utarlai, Barmer (Rajasthan)
 
PPTX
Unit_9.pptx
BhagyasriPatel1
 
PPTX
set operators.pptx
Anusha sivakumar
 
PDF
Sql (Introduction to Structured Query language)
Mohd Tousif
 
PPTX
Database Overview
Livares Technologies Pvt Ltd
 
Sql joins
Vivek Singh
 
MYSQL using set operators
Ahmed Farag
 
ADV PPT 8 LAB.pptx
ArjayBalberan1
 
MySQL JOIN & UNION
Jamshid Hashimi
 
Sql wksht-6
Mukesh Tekwani
 
Intro to tsql unit 4
Syed Asrarali
 
Intro To TSQL - Unit 4
iccma
 
Oracle: Joins
DataminingTools Inc
 
Understanding Structured Query Language fundamentals
Ankit Rao
 
Sql ch 5
Mukesh Tekwani
 
DBMS Nested & Sub Queries Set operations
Sreedhar Chowdam
 
Ms sql server ii
Iblesoft
 
sql joinsubdjbrjdbjrjnfkjcnkrnfknrkfkrfkrfkrk
kailasmanoj
 
sqlyyybdbyehduheufhuehfuheuwehfiewifhewihfiehfiwf
kailasmanoj
 
Chapter9 more on database and sql
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Unit_9.pptx
BhagyasriPatel1
 
set operators.pptx
Anusha sivakumar
 
Sql (Introduction to Structured Query language)
Mohd Tousif
 
Database Overview
Livares Technologies Pvt Ltd
 

Recently uploaded (20)

PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Doc9.....................................
SofiaCollazos
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 

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.