What is the difference between JOIN and UNION?

 Posted by vishalneeraj-24503 on 12/8/2013 | Category: Sql Server Interview questions | Views: 1289 | Points: 40

SQL JOIN allows us to fetch records on other table based on the given conditions between two tables.

For example,if we have the Employee ID of each employee, then we can use this Employee ID of the employee table to join with the Employee ID of Project master table to fetch employe details.

UNION operation allows us to add 2 similar data sets to create resulting data set that contains all the data from the source data sets.Union does not require any condition for joining.

For example, if you have 2 employee tables with same structure, you can UNION them to create one result set that will contain all the records from both of the tables.

Join example:-

Suppose we have 2 tables i.e. Employee_Master and Project_Master.

Join Example:-

Select emp.employee_name,emp.address,emp.ph_no from employee_master emp

project_master prj
on emp.employee_id = prj.emp_id and prj.status = 'AA'
where emp.status = 'AA';

Union Example:-

Select * from table_name1

Select * from table_name2

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response