Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Predict output


Predict output

Author
Message
Tom Moreau
Tom Moreau
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1973 Visits: 48
As others have stated, only an ORDER BY guarantees the sort order of the results. Therefore, there are 2 correct answers.



arup chakraborty
arup chakraborty
SSC-Addicted
SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)

Group: General Forum Members
Points: 458 Visits: 365
Hi Anirban,

Many thanks. I will try to dig more questions from the SQL mine.
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11020 Visits: 14858
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47259 Visits: 44389
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, MVP, M.Sc (Comp Sci)
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


prem_ignou_2003
prem_ignou_2003
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: 0
In this quiry using Union - The union don't allow the duplicate.
The Null have low weight
Frédéric van der Plancke
Frédéric van der Plancke
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 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 ?
Adam Machanic
Adam Machanic
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1141 Visits: 714
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
Piotr.Rodak
Piotr.Rodak
SSC Eights!
SSC Eights! (802 reputation)SSC Eights! (802 reputation)SSC Eights! (802 reputation)SSC Eights! (802 reputation)SSC Eights! (802 reputation)SSC Eights! (802 reputation)SSC Eights! (802 reputation)SSC Eights! (802 reputation)

Group: General Forum Members
Points: 802 Visits: 1761
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
TomThomson
TomThomson
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10709 Visits: 12008
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

kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2417 Visits: 2763
Good Question Smile

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
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