INTERSECT in SQL SERVER



INTERSECT

       Returns any distinct values that are returned by both the query on the left and right sides of the INTERSECT operand.

Simply its returns common distinct values from right and left side of  INTERSECT operator.

--CREATE TEMP TABLE
create table #tblsample (ProductId tinyint)
create table #tblsample2 (ProductId tinyint)

--insert values to tbl 1
insert into #tblsample values (1)
insert into #tblsample values (2)
insert into #tblsample values (3)
insert into #tblsample values (4)

--insert values to tbl 2
insert into #tblsample2 values (1)

--SELECT
select * from #tblsample
select * from #tblsample2

--USE INTERSECT
select * from #tblsample
INTERSECT
select * from #tblsample2 

--insert duplicate values to tbl2
insert into #tblsample2 values (2)

insert into #tblsample2 values (2)


--USE INTERSECT
select * from #tblsample
INTERSECT
select * from #tblsample2 

Above query returns common distinct values.