|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Tuesday, July 03, 2012 5:49 AM
Points: 418,
Visits: 365
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, February 25, 2009 11:01 AM
Points: 2,
Visits: 6
|
|
Hi Dudes
The question is really amazing.Nice ideas. Waiting for amazing questions.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 12:56 AM
Points: 1,972,
Visits: 1,822
|
|
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.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 8:40 AM
Points: 1,078,
Visits: 848
|
|
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
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Tuesday, July 03, 2012 5:49 AM
Points: 418,
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.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:11 PM
Points: 37,741,
Visits: 30,020
|
|
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 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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:03 PM
Points: 5,244,
Visits: 7,061
|
|
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
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 12:56 AM
Points: 1,972,
Visits: 1,822
|
|
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.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:11 PM
Points: 37,741,
Visits: 30,020
|
|
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 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
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: 2 days ago @ 1:35 AM
Points: 4,789,
Visits: 1,336
|
|
Nice and wonderful questions ...:) Arup great effort
|
|
|
|