SQL provides three set manipulation constructs that extend the basic query
SQL supports three operators under the names UNION, INTERSECTION,MINUS (or) EXCEPT.
UNION: The UNION statement combines rows from two or more queries without including duplicate rows.
The syntax of UNION statement is:
Query
UNION
Query
In other words UNION statement combines the output of two SELECT queries.(SELECT statement must be union compatible)
UNION ALL statement can be used to produce a relation that retains the duplicate rows.
INTERSECT: The INTERSECT statement can be used to combine rows from two queries, returning only the rows that appear in both sets.
The syntax for the INTERSSECT statement is:
Query
INTERSECT
Query
MINUS (or) EXCEPT: The MINUS statement in SQL combines rows from two queries and returns only the rows that appear in the first set but not in the second.
The syntax for the minus statement is :
Query
MINUS
Query
Example-SQL set operators:
Consider the following two tables
PRODUCT1 PRODUCT2
| P_code | P_descript | price |
| 123455 | Flashlight | 10.00 |
| 123456 | Lamp | 25.00 |
| 123457 | Fan | 100.00 |
| 123458 | Bulb | 20.00 |
| 123459 | Grinder | 500.00 |
| P_code | P_descript | price |
| 345678 | Oven | 1000.00 |
| 345679 | Mixer | 400.00 |
| 123459 | Grinder | 500.00 |
1. Combine the data in both the tables using UNION ALL operator
2. Avoid duplicate rows for the above question
3. Find the product descript from the two tables whose price is 500
4. List the common rows in two tables(use INTERECT)
5. List the rows that appear in the product1 table but not in product2 table
6. List the rows that appear in the product2 table but not in product1 table
1. Combine the data in both the tables using UNION ALL operator
SQL> select *
2 from product1
3 UNION ALL
4 select *
5 from product2;
P_CODE P_DESCRIPT PRICE
-------------- --------------------- ------------
123455 flashlight 10
123456 lamp 25
123457 fan 100
123458 bulb 20
123459 grinder 500
345678 oven 1000
345679 mixer 400
123459 grinder 500
8 rows selected.
2. Avoid duplicate rows for the above question
SQL> select *
2 from product1
3 UNION
4 select *
5 from product2;
P_CODE P_DESCRIPT PRICE
-------------- --------------------- ------------
123455 flashlight 10
123456 lamp 25
123457 fan 100
123458 bulb 20
123459 grinder 500
345678 oven 1000
345679 mixer 400
7 rows selected.
3. Find the product descript from the two tables whose price is 500
SQL> select p_descript
2 from product1
3 where price=500
4 UNION
5 select p_descript
6 from product2
7 where price=500;
P_DESCRIPT
----------------
grinder
4. List the common rows in two tables(use INTERECT)
SQL> select *
2 from product1
3 INTERSECT
4 select *
5 from product2;
P_CODE P_DESCRIPT PRICE
----------- ---------------- ----------
123459 grinder 500
5. List the rows that appear in the product1 table but not in product2 table
SQL> select *
2 from product1
3 MINUS
4 select *
5 from product2;
P_CODE P_DESCRIPT PRICE
---------- -------------------- -----------
123455 flashlight 10
123456 lamp 25
123457 fan 100
123458 bulb 20
6. List the rows that appear in the product2 table but not in product1 table
SQL> select *
2 from product2
3 MINUS
4 select *
5 from product1;
P_CODE P_DESCRIPT PRICE
------------ ------------------ ------------
345678 oven 1000
345679 mixer 400