SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Intersect, Except, Union, All and Any


Intersect, Except, Union, All and Any

Author
Message
jmcnemar
jmcnemar
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 126
Hello, the article appears very interesting and useful, but I am concerned that I am missing important parts of it because the image links are broken.

Will you fix these links so we can see the entire article as you intended?

Thanks!
Tim Friesen
Tim Friesen
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 78
Images would be extremely helpful.
Adam Haines
Adam Haines
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3228 Visits: 3135
Jeff Moden (7/19/2009)
Why would anyone think that INTERSECT is easier than INNER JOIN?


Jeff,

In some cases intersect is easier to use than inner join, if you want to compare resultsets. For example, an inner join requires you to add EVERY column to the inner join list or to the predicate to achieve what you could accomplish by use the intersect operator. I am not saying this is the best method, but it can sometimes reduce coding and save you time. Look at the example below and imagine if you wanted to compare tables with 15+ columns. Essentially with intersect, you can copy and paste the same query on both sides of the operator.

Note: To those reading this, you should not use select * in your select list. I did so below to demonstrate a point.

E.g.

DECLARE @t1 TABLE(
id INT,
col1 CHAR(1),
col2 SMALLINT,
col3 CHAR(1),
col4 CHAR(1),
col5 CHAR(1)
)

INSERT INTO @t1 VALUES (1,'a',10,'z','z','z');
INSERT INTO @t1 VALUES (2,'b',20,'z','z','z');
INSERT INTO @t1 VALUES (3,'c',30,'z','z','z');
INSERT INTO @t1 VALUES (4,'d',40,'z','z','z');

DECLARE @t2 TABLE(
id INT,
col CHAR(1),
col2 SMALLINT,
col3 CHAR(1),
col4 CHAR(1),
col5 CHAR(1)
)

INSERT INTO @t2 VALUES (1,'a',10,'z','z','z');
INSERT INTO @t2 VALUES (2,'b',20,'z','z','z');
INSERT INTO @t2 VALUES (4,'d',30,'z','z','z');
INSERT INTO @t2 VALUES (5,'e',40,'z','z','z');

--intersect
SELECT *
FROM @t1

INTERSECT

SELECT *
FROM @t2

--equivilant
SELECT
t1.*
FROM @t1 t1
INNER JOIN @t2 t2
ON t1.id = t2.id
AND t1.col1 = t2.col
AND t1.col2 = t2.col2
AND t1.col3 = t2.col3
AND t1.col4 = t2.col4
AND t1.col5 = t2.col5



As for except, I find this operator quite useful when trying to diagnose RI violaters and resultsets that dont match between source and destination. Except has the same benefits as Intersect, which is namely reduced typing.

I personally dont allow or use these operators in production code, but I sometimes use these operators to quickly retrieve information.

I recently blogged about intersect and except. You can have a look if you like:

http://jahaines.blogspot.com/2009/07/should-i-intersect-or-except.html



My blog: http://jahaines.blogspot.com
Lee Hilton
Lee Hilton
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 17
I am not near my workstation to test, but I am curious if the tests were affected by caching? Any time I have two statements that are different but produce the same timing I am suspicious. I know that these statements may actually run the same in the engine, using the same query plan, but then again the difference might be in the details there.

Perhaps there is a difference in how the query performs when there is no data already in cache. I know that this has me interested enough that I want to test this against a massive dataset, then retest with the old query after cycling SQL.

Great article though, this may prove very interesting.
ta.bu.shi.da.yu
ta.bu.shi.da.yu
SSC-Addicted
SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)

Group: General Forum Members
Points: 415 Visits: 494
Lee Hilton (7/20/2009)
I am not near my workstation to test, but I am curious if the tests were affected by caching? Any time I have two statements that are different but produce the same timing I am suspicious. I know that these statements may actually run the same in the engine, using the same query plan, but then again the difference might be in the details there.

Perhaps there is a difference in how the query performs when there is no data already in cache. I know that this has me interested enough that I want to test this against a massive dataset, then retest with the old query after cycling SQL.

Great article though, this may prove very interesting.


I know that the author is using the AdventureWorks database, but I'm curious just how much data he was testing on.

Random Technical Stuff
Steve Jones
Steve Jones
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: Administrators
Points: 63346 Visits: 19115
Images have been loaded.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Dan Cook-293387
Dan Cook-293387
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 8
I learnt about INTERSECT when I had a query which I couldn't write any other way.

http://sci.esa.int/science-e/www/object/index.cfm?fobjectid=30910

you see there are missions and topics, and an option of Match Any (or) of those selected and Match All (and).

Unfortunately Missions and Topics all come from the same attribute id column, so I needed to compare missions and topics seperately, before 'AND'ing them together Smile.

A logical nightmare.... and I was considering looping queries until .. INTERSECT

the code...






INTERSECT
(SELECT objectid FROM OC4_OBJECTATTRIBUTEMAP where attributeid = #i#)


INTERSECT (SELECT objectid FROM OC4_OBJECTATTRIBUTEMAP where attributeid IN (#m#))







INTERSECT
(SELECT objectid FROM OC4_OBJECTATTRIBUTEMAP where attributeid = #i#)


INTERSECT (SELECT objectid FROM OC4_OBJECTATTRIBUTEMAP where attributeid IN (#t#))




code is a bit broken, but you get the idea!
Carleton
Carleton
SSC Veteran
SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)

Group: General Forum Members
Points: 254 Visits: 3892
Very nice article. One clarrification, INTERSECT and EXCEPT are available in SQL 2005 (article refers only to 2008). I use these quite a bit in my 2005 env, especially for adhoc table comparisons.
Slick84
Slick84
Right there with Babe
Right there with Babe (776 reputation)Right there with Babe (776 reputation)Right there with Babe (776 reputation)Right there with Babe (776 reputation)Right there with Babe (776 reputation)Right there with Babe (776 reputation)Right there with Babe (776 reputation)Right there with Babe (776 reputation)

Group: General Forum Members
Points: 776 Visits: 1163
Even though I still use my left and inner joins...

I think EXCEPT & Intersect are very nice. It's so much easier to explain data to some one who is not so much knowledgeable about joins which can be very initimidating at first.

Just my 0.2c!

--
Hehe
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39762 Visits: 38563
Jeff Moden (7/19/2009)
Why would anyone think that INTERSECT is easier than INNER JOIN?


How about finding all records matching records between two tables with identical structure? Would you rather write a query joining two tables on 63 columns (for example) with the possibility of NULL values in some of the columns, or would you rather write a simple INTERSECT query? Personally, I'll go with the simplier INTERSECT.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search