Monday, May 10, 2010

The UNION operator aplied with where clause.

Using Union Operator with where clause.

--Create table

create table Emp_tab1
[Employee Name] varchar(50),
[Employee Dept] varchar(20),
[Employee ID] int,

--insert value in table

insert into Emp_tab1 values('johan','Devloper',1)
insert into Emp_tab1 values('James bond', 'Testing',2)
insert into Emp_tab1 values('Harry','HR',3)
insert into Emp_tab1 values('Michel','clerck',4)
insert into Emp_tab1 values('Thomes','Manager',5)
insert into Emp_tab1 values('jacky','Project manager',6)

create table Emp_tab2

[Employee Name] varchar(50),
[Employee Dept] varchar(30),
[Employee ID] varchar(30),


--insert value in table

insert into Emp_tab2 values('Jone', 'Manager',1)
insert into Emp_tab2 values('Ram', 'Testing',2)
insert into Emp_tab2 values('Sumit', 'Teacher',3)
insert into Emp_tab2 values('Niraj', 'Balder',4)
insert into Emp_tab2 values('Ravi', 'Side incharge',5)
insert into Emp_tab2 values('Razz', 'Doctor',6)

--Using Union with where clause

SELECT * FROM Employee1.dbo.Emp_tab1 where [Employee id] between 1 and 6
SELECT * FROM Employee1.dbo.Emp_tab2 where [Employee id] > 1

NOTE:- Two table c Emp_tab1, Emp_tab2  contain the Employee Name, Employee Dept, Employee id andEmptab2 contain the Employee Name, Employee dept, Employee id.

The result is displayed below.

How does the Union get Applied
 Two table sets shows the value in Emp_tab1 and Emp_tab2.

Now the filter  is applied

                                              Once the Union Get applied another super set is created with three  
                                               subset below and records are returned as  all the three column set (subset) wrapped up in the super set

No comments:

Recent Posts