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


Predict output


Predict output

Author
Message
arup chakraborty
arup chakraborty
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1474 Visits: 365
Comments posted to this topic are about the item Predict output
premindthomas
premindthomas
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 6
Hi Dudes

The question is really amazing.Nice ideas.
Waiting for amazing questions.
Carlo Romagnano
Carlo Romagnano
SSCrazy Eights
SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)

Group: General Forum Members
Points: 9361 Visits: 3441
Without the ORDER BY clause, you can not predict the order of rows.
NULL, 1 and 1, NULL are both possible.
In the future release of sqlserver or in another DBMS, the order may be different.
Robert-378556
Robert-378556
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2929 Visits: 1015
UNION ALL simply concats the results, UNION removes the duplicates, the best way to do it is to sort the result, so NULLs come first.
Try this:
SELECT 3 UNION SELECT '2' UNION SELECT NULL
arup chakraborty
arup chakraborty
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1474 Visits: 365
Hi Carlo,

I have tested it several times in SQL Server 2000 and 2005. Every time, it gives NULL first. I guess that it shows the NULL first because NULL has no values. Then it shows the values.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)

Group: General Forum Members
Points: 370884 Visits: 46957
Null comes first because SQL has to do a sort behind the scenes to ensure uniqueness (required by union). Hence there's an implicit Order by 1 ASC on the statement. When you do an order by, null comes before values.

There's no guarantee that the behaviour will be the same in future versions though.

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


Hugo Kornelis
Hugo Kornelis
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25125 Visits: 12585
arup_kc (10/14/2008)
Hi Carlo,

I have tested it several times in SQL Server 2000 and 2005. Every time, it gives NULL first. I guess that it shows the NULL first because NULL has no values. Then it shows the values.


Hi Arup_kc,

It's great that testing shows consistent results, but that doesn't make this the "only" correct answer. A query without ORDER BY returns a relational set, which (by the very definitions of the relational model) has no inherent order. Limitations of the clients force some order upon that set, but unless an ORDER BY clause is specified, that order is indetermined and prone to change at SQL Server's whim.

Undocumented behaviour, even if confirmed by repeated testing, should never be relied on. I thought that after the "GROUP BY no longer implies sorting" change back in 6.5 and the "TOP 100 PERCENT ... ORDER BY in views no longer orders the view" change in 2005, that should be clear by now!


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Carlo Romagnano
Carlo Romagnano
SSCrazy Eights
SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)

Group: General Forum Members
Points: 9361 Visits: 3441
GilaMonster (10/14/2008)
Hence there's an implicit Order by 1 ASC on the statement.

Also the DISTINCT clause and the GROUP BY perform an implicit ORDER BY for all cols, but "IMPLICIT" is not the rule nor is written in the BOL.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)

Group: General Forum Members
Points: 370884 Visits: 46957
Carlo Romagnano (10/14/2008)

Also the DISTINCT clause and the GROUP BY perform an implicit ORDER BY for all cols, but "IMPLICIT" is not the rule nor is written in the BOL.


I'm fully aware of that. I was trying to explain why, in this case, on the current versions of SQL, the null always comes first.

Oh, and if you look at the exec plan for this query, SQL's actually not doing a sort at all.

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


Anipaul
Anipaul
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: 11889 Visits: 1407
Nice and wonderful questions ...Smile
Arup great effort



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