13

Click here to load reader

SQL UNION

Embed Size (px)

DESCRIPTION

The SQL UNION operator combines two or more SELECT statements.

Citation preview

Page 1: SQL UNION

SQL UNION

Reference :-

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

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

Page 2: SQL UNION

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.

Page 3: SQL UNION

SQL UNION

Example of SQL UNION

Code:-

SELECT prod_code,prod_name FROM product UNION SELECT prod_code,prod_name FROM purchase;

Page 4: SQL UNION

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;

Page 5: SQL UNION

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.

Page 6: SQL UNION

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

Page 7: SQL UNION

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.

Page 8: SQL UNION

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

Page 9: SQL UNION

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.

Page 10: SQL 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

Page 11: SQL UNION

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.

Page 12: SQL UNION

Sql UNION with Inner Join

Example of Sql UNION a table to itself

Code:-SELECT product.prod_code,product.prod_name, 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;

Page 13: SQL UNION

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.