MainMenu

Home Java Overview Maven Tutorials

Sunday 15 January 2017

Union and Intersection with Example

Sample Query for UNION Here two select statement result will be combined whether records are identical or not also first select statement result will appear and below it second select statement result will appear.
Syntax :

Select table1.Column_Name1,table1.Column_Name2
from Table1
UNION
select table2.Column_Name1, table2.column_name2
from Table2


Consider the following table : Salary:
Emp_idEmp_salaryEmp_name
0125000Chandan
0230000Adhiraj
0325000Akshat

Employee_record:
Emp_idEmp_cityEmp_name
201noidaSara
202gurgaonfatima

Query of Union:

select Emp_id, Emp_name
from salary
UNION
select Emp_id, Emp_name
from designation

Result :
Emp_idEmp_name
01Chandan
02Adhiraj
03Akshat
201Sara
202Fatima

--------------------------------------------------------------------------------------------------------------- Sample Query for Intersection Here two select statement result will be combined but returns rows only from the first SELECT statement that are identical to a row in the second SELECT statement also first select statement result will appear and below it second select statement result will appear.
Syntax :

Select table1.Column_Name1,table1.Column_Name2
from Table1
Intersection
select table2.Column_Name1, table2.column_name2
from Table2


Consider the following table : Salary:
Emp_idEmp_salaryEmp_name
0125000Chandan
0230000Adhiraj
0325000Akshat

Employee_record:
Emp_idEmp_cityEmp_name
01noidachandan
04gurgaonfatima

Query of Union:

select Emp_id, Emp_name
from salary
INTERSECT
select Emp_id, Emp_name
from designation

Result :
Emp_idEmp_name
01Chandan

Tag:UNION in SQl, UNION query with example, Intersection in SQL, Intersection with example, Difference between union and intersection

No comments:

Post a Comment