Log in  ::  Register  ::  Not logged in

## INTERSECT 1

 Author Message EL Jerry Hall of Fame Group: General Forum Members Points: 3914 Visits: 1337 Really nice question. Thanks, Ron.Hugo, thank you for the additional explanation, too. I would have not been able to understand it just by reading BOL."El" Jerry. "El" Jerry."A watt of Ottawa" - Gerardo GalvanTo better understand your help request, please follow these best practices. Ken Wymore SSCarpal Tunnel Group: General Forum Members Points: 4420 Visits: 2342 Great question and dialogue. Thanks! TomThomson SSChampion Group: General Forum Members Points: 10704 Visits: 12001 Nice tidy question, that brings up two importan fundamentals:(1) intersection returns distinct values not duplicates(2) equality tests do implicit conversion where needed, from lower precedence type to higher. Tom zymos Mr or Mrs. 500 Group: General Forum Members Points: 540 Visits: 259 Hugo,Thanks for the further explanation that supports the correct answer. zymos Mr or Mrs. 500 Group: General Forum Members Points: 540 Visits: 259 Nice question on the topic.Thanks. chriscoates Old Hand Group: General Forum Members Points: 308 Visits: 110 Nice question, thanks. Prior to answering I would have done something like`SELECT DISTINCT x AS 'Intersect Chars with BIGINT'FROM #AINNER JOIN #B on #b.M = #a.x`A quick execution plan shows that the INTERSECT is ever so marginally more efficient too (in this case anyway), primarily because the Nested Loop step is a Left Semi Join, where the INNER JOIN has a slightly higher nested loop step cost.Every day's a school day! Hugo Kornelis SSCrazy Eights Group: General Forum Members Points: 8321 Visits: 11556 chriscoates (6/26/2012)Nice question, thanks. Prior to answering I would have done something like`SELECT DISTINCT x AS 'Intersect Chars with BIGINT'FROM #AINNER JOIN #B on #b.M = #a.x`A quick execution plan shows that the INTERSECT is ever so marginally more efficient too (in this case anyway), primarily because the Nested Loop step is a Left Semi Join, where the INNER JOIN has a slightly higher nested loop step cost.Every day's a school day!Note that they are only equivalent when the columns #a.x and #b.M are unique. If there is a primary key, unique constraint, or unique index involved, you can be sure that they are unique; otherwise you can't, so you can't just replace one with the other. Hugo Kornelis, SQL Server MVPVisit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis chriscoates Old Hand Group: General Forum Members Points: 308 Visits: 110 Perhaps we are at cross purposes Hugo, I meant tht the result set is the same, regardless of uniqueness. Of course if the values are not unique then there's a man-to-many relationship that I'm sure would make my query cost soar to determine the DISTINCT results, but the output would surely be the same? Am I missing something else obvious here? Hugo Kornelis SSCrazy Eights Group: General Forum Members Points: 8321 Visits: 11556 chriscoates (6/27/2012)Perhaps we are at cross purposes Hugo, I meant tht the result set is the same, regardless of uniqueness. Of course if the values are not unique then there's a man-to-many relationship that I'm sure would make my query cost soar to determine the DISTINCT results, but the output would surely be the same? Am I missing something else obvious here?When comparing two queries for performance, the first thing you must consider is whether they are equivalent. Not just for one set of data, but for each possible set of data.In the case of this question, the queries happen to produce the same result for the given data, but will give different results with other data, so they are not equivalent, and you cannot replace one with the other in a production system. If you add constraints to ensure that they are equivalent, the optimizer might (I didn't test this, and have no time for it either) well produce identical plans for the two queries. Without those constraints, it can't produce identical plans, since (depending on the data) it might have to return different results. Hugo Kornelis, SQL Server MVPVisit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis chriscoates Old Hand Group: General Forum Members Points: 308 Visits: 110 To quote this question, and the article it refers to:INERSECT returns "those distinct values that are common between tables"I have never seen a situation where:SELECT DISTINCT a.xFROM a INNER JOIN b on a.x=b.mwould not return "those distinct values that are common between tables" either, regardless of the uniqueness of a.x or b.m in either query.I agree that the execution may be completely different in either case.Chris