Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

Predict output Expand / Collapse
Author
Message
Posted Tuesday, October 14, 2008 4:33 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, April 1, 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.


Post #585330
Posted Tuesday, October 14, 2008 7:34 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, July 3, 2012 5:49 AM
Points: 418, Visits: 365
Hi Anirban,

Many thanks. I will try to dig more questions from the SQL mine.
Post #585451
Posted Tuesday, October 14, 2008 8:28 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:42 PM
Points: 10,193, Visits: 13,120
Tom Moreau (10/14/2008)
As others have stated, only an ORDER BY guarantees the sort order of the results. Therefore, there are 2 correct answers.


As Gail explained, in the current version(s) of SQL Server available there is an implicit sort being done, so the NULL will come first. I got it wrong, and realized it just as I clicked submit. Oh well, too late, no points.




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #585514
Posted Tuesday, October 14, 2008 10:29 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:49 PM
Points: 39,886, Visits: 36,233
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

Post #585621
Posted Tuesday, October 14, 2008 10:57 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
Post #585655
Posted Wednesday, October 15, 2008 9:08 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued 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 ?
Post #586310
Posted Thursday, October 30, 2008 12:49 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 6:46 PM
Points: 1,113, Visits: 704
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
Post #594623
Posted Saturday, November 22, 2008 6:27 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!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
Post #607113
Posted Monday, January 26, 2009 11:55 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:20 AM
Points: 7,696, Visits: 9,424
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
Post #643626
Posted Wednesday, December 19, 2012 4:23 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:27 AM
Points: 1,893, Visits: 2,329
Good Question :)


_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1398295
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse