October 13, 2008 at 9:04 pm
Comments posted to this topic are about the item Predict output
October 13, 2008 at 11:19 pm
Hi Dudes
The question is really amazing.Nice ideas.
Waiting for amazing questions.
October 14, 2008 at 12:59 am
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.
October 14, 2008 at 1:25 am
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
October 14, 2008 at 1:26 am
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.
October 14, 2008 at 1:59 am
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
October 14, 2008 at 2:14 am
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!
October 14, 2008 at 2:26 am
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.
October 14, 2008 at 2:31 am
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
October 14, 2008 at 3:34 am
Nice and wonderful questions ...:)
Arup great effort
October 14, 2008 at 4:33 am
As others have stated, only an ORDER BY guarantees the sort order of the results. Therefore, there are 2 correct answers.
October 14, 2008 at 7:34 am
Hi Anirban,
Many thanks. I will try to dig more questions from the SQL mine.
October 14, 2008 at 8:28 am
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
Consultant - Straight Path Solutions
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
October 14, 2008 at 10:29 am
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
October 14, 2008 at 10:57 am
In this quiry using Union - The union don't allow the duplicate.
The Null have low weight
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply