Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Intersect in Sql

Author  Topic 

cruxmagi
Starting Member

38 Posts

Posted - 2008-04-25 : 01:37:58
does any one know about sql intersect

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-25 : 02:02:00
Yup. it brings back the common records from both sides of query. Why dont you have a look at explanation given in books online?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-25 : 02:22:24
Simple example to illustrate INTERSECT

DECLARE @temp table
(ID int identity(1,1),
Name varchar(20)
)
DECLARE @temp1 table
(ID int identity(1,1),
Name varchar(20)
)

INSERT INTO @temp (Name)
SELECT 'AAA'
UNION ALL
SELECT 'BBB'
UNION ALL
SELECT 'CCC'
UNION ALL
SELECT 'FFF'

INSERT INTO @temp1 (Name)
SELECT 'AAA'
UNION ALL
SELECT 'EEE'
UNION ALL
SELECT 'GGG'
UNION ALL
SELECT 'FFF'


SELECT * FROM @temp
SELECT * FROM @temp1

SELECT * FROM @temp
INTERSECT
SELECT * FROM @temp1


output
----------------------------------
ID Name
----------- --------------------
1 AAA
2 BBB
3 CCC
4 FFF

ID Name
----------- --------------------
1 AAA
2 EEE
3 GGG
4 FFF

ID Name
----------- --------------------
1 AAA
4 FFF
Go to Top of Page
   

- Advertisement -