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.