SQL UNIONReference :-       http://www.w3resource.com/sql/sql-union.php       http://dev.mysql.com/doc/refman/5.0/en/union...
SQL UNIONDescription:-The SQL UNION operator combines the results of two or more queries and makes aresult set which inclu...
SQL UNIONExample of SQL UNIONCode:-         SELECT prod_code,prod_name         FROM product         UNION         SELECT p...
SQL UNION ALLExample of SQL UNION ALLCode:-         SELECT prod_code,prod_name,com_name         FROM product         UNION...
SQL UNION ALLExplanation In the above example the optional clause ALL have been added with UNION for which, all the rows f...
Sql UNION ALL using where   Example of Sql UNION ALL using whereCode:-           SELECT prod_code,prod_name,com_name      ...
Sql UNION ALL using whereExplanation:- In the above example the two queries have been set using two different criterias in...
Sql UNION a table to itself    Example of Sql UNION a table to itselfCode:-           SELECT prod_code,prod_name,com_name ...
Sql UNION a table to itselfExplanation:- In the above example the two queries have been set using two different criterias ...
Sql UNION with different column names     Example of Sql UNION a table to itself Code:-          SELECT prod_code,prod_nam...
Sql UNION with different column namesExplanation:- n the above example the two queries have been set using two different c...
Sql UNION with Inner Join         Example of Sql UNION a table to itself          SELECT product.prod_code,product.prod_na...
Sql UNION with Inner JoinExplanation:- n the above example the union made by two queries. The queries are two inner join s...
Upcoming SlideShare
Loading in...5
×

SQL UNION

241

Published on

The SQL UNION operator combines two or more SELECT statements.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
241
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "SQL UNION"

  1. 1. SQL UNIONReference :- http://www.w3resource.com/sql/sql-union.php http://dev.mysql.com/doc/refman/5.0/en/union.html
  2. 2. SQL UNIONDescription:-The SQL UNION operator combines the results of two or more queries and makes aresult 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 orcompatible.3.) Usually returned column names are taken from the first query.By default the UNION behalves like UNION [DISTINCT] , i.e. eliminated the duplicaterows; however, using ALL keyword with UNION returns all rows, including duplicates.
  3. 3. SQL UNIONExample of SQL UNIONCode:- SELECT prod_code,prod_name FROM product UNION SELECT prod_code,prod_name FROM purchase;
  4. 4. SQL UNION ALLExample of SQL UNION ALLCode:- SELECT prod_code,prod_name,com_name FROM product UNION ALL SELECT prod_code,prod_name,com_name FROM purchase;
  5. 5. SQL UNION ALLExplanation 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. 6. Sql UNION ALL using where Example of Sql UNION ALL using whereCode:- 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. 7. Sql UNION ALL using whereExplanation:- 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. 8. Sql UNION a table to itself Example of Sql UNION a table to itselfCode:- 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. 9. Sql UNION a table to itselfExplanation:- 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. 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. 11. Sql UNION with different column namesExplanation:- 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. 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. 13. Sql UNION with Inner JoinExplanation:- 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.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×