|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Monday, April 01, 2013 4:01 AM
Points: 1,971,
Visits: 48
|
|
As others have stated, only an ORDER BY guarantees the sort order of the results. Therefore, there are 2 correct answers.
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Tuesday, July 03, 2012 5:49 AM
Points: 418,
Visits: 365
|
|
Hi Anirban,
Many thanks. I will try to dig more questions from the SQL mine.
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: 2 days ago @ 12:22 PM
Points: 10,571,
Visits: 11,871
|
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:15 PM
Points: 37,651,
Visits: 29,903
|
|
The question is do you want the theoretical result (where NULL, 1 and 1, NULL are both right) or the actual result that the current versions of SQL give for that specific query.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, October 14, 2008 10:55 AM
Points: 2,
Visits: 0
|
|
In this quiry using Union - The union don't allow the duplicate. The Null have low weight
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, May 20, 2010 10:04 AM
Points: 54,
Visits: 38
|
|
I do NOT think that "union (distinct)" always entails an "order by", neither theoretically (as several others have pointed out), neither practically.
While I've done no testing, there may be cases where SQLServer resorts to hash comparisons insead of ordering to eliminate duplicates. You're probably 99% safe assuming it will "order by" a single int field union. In more complex situations, I wouldn't bet (yet).
Is there any written guarantee from MS that any version of MSSQL always returns ordered results out of "union (disjoint)" queries ? Or are you people just wildguessing ?
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 3:20 PM
Points: 1,137,
Visits: 667
|
|
As others have mentioned, a sort is not the only option, and the NULL may or may not come first... No one actually showed this yet, though, so here you go:
SELECT 1 UNION SELECT NULL UNION SELECT '1' OPTION (HASH UNION)
-- Adam Machanic SQL Server MVP SQLblog.com: THE SQL Server Blog Spot on the Web
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Thursday, May 10, 2012 9:49 AM
Points: 800,
Visits: 1,759
|
|
Nice question. The execution plan for slightly modified query
SELECT 2 UNION SELECT 1 union SELECT NULL UNION SELECT '1' union SELECT '2'
shows that Merge Join operators are used to merge inputs from particular SELECT queries. BOL says that Merge Join operator may introduce sort as it requires inputs to be sorted. Since the Selects return only single values, they are 'sorted' and we don't see explicit sort in execution plan.
Interesting thing happens when I modify the query to following code:
SELECT 2 UNION select a from ( SELECT '2' a UNION ALL SELECT 1 ) q UNION SELECT NULL UNION SELECT '1' The Merge Joins are replaced by Concatenation operator and explicit Distinct Sort is used to remove duplicates. If I add option (merge union) to above query, the sort operation is added on the input of the Merge Join implemening union SELECT 2 UNION select a from ....
I wonder if it is possible to get output 1, NULL, 2 or 2, NULL, 1. The Hash Match operator that is used when option (hash union) is used apparently sorts data anyway, just NULLs are added to the end of the output.
Piotr
...and your only reply is slàinte mhath
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 8:32 PM
Points: 7,080,
Visits: 7,125
|
|
Piotr Rodak (11/22/2008)
The Hash Match operator that is used when option (hash union) is used apparently sorts data anyway, just NULLs are added to the end of the output. Piotr
The hash union sorts the hash values (and sometimes sorts the values within a set sharing a hash value); presumably the hash value currently used for NULL sorts high. There's no guarantee that it will sort high in the next version. There's no guarantee that the optimi[s/z]er won't chose some other duplicate elimination algorithm which hasn't been mentioned yet when it thinks its appropriate for the dataset, which may end up with a different order.
Tom
Tom Que conclure à la fin de tous mes longs propos? C'est que les préjugés sont la raison des sots. (Voltaire, 1756)
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 11:16 PM
Points: 1,060,
Visits: 1,151
|
|
|
|
|