|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, October 16, 2012 8:54 AM
Points: 182,
Visits: 57
|
|
mike (4/22/2009)
Hey! Not fair. I lost a point because of a missing ORDER BY ? Like a previous poster mentioned, both 1 and 2 are correct.  Can I get my point back please?
Yeah, me too!
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, September 24, 2012 8:10 AM
Points: 2,042,
Visits: 499
|
|
From Microsoft: "The exact results of a UNION operation depend on the collation chosen during installation and the ORDER BY clause."
http://msdn.microsoft.com/en-us/library/ms191141(SQL.90).aspx
This is for SQL Server 2005. I'm assuming it applies to other versions as well. It appears that behind the scenes SQL Server does sort it as part of the duplicate elimination process. This will not be done with UNION ALL though.
For the record: An ORDER BY should be used as part of best practices.
Cheers, Brian
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Friday, March 15, 2013 2:43 PM
Points: 3,924,
Visits: 1,554
|
|
select NULL,'manoj'union select Null,'1'union select NULL,'manoj' order by 2
or
select NULL,'manoj'union select Null,'1'union select NULL,'manoj' order by 1
gives you same result.
NULL 1 NULL manoj
I tried on all sql server versions and result is same. May be I am missing something here when some people are telling both 1 & 2 answers are correct.
SQL DBA.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, November 30, 2012 1:52 AM
Points: 1,116,
Visits: 602
|
|
Jayeff (4/22/2009)
mike (4/22/2009)
Hey! Not fair. I lost a point because of a missing ORDER BY ? Like a previous poster mentioned, both 1 and 2 are correct.  Can I get my point back please? Yeah, me too! And see, you both got you point just by posting that you want your point back. ;)
Ronald Hensbergen
Help us, help yourself... Post data so we can read and use it: http://www.sqlservercentral.com/articles/Best+Practices/61537/ ------------------------------------------------------------------------- 2+2=5 for significant large values of 2
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 2:04 AM
Points: 1,342,
Visits: 1,946
|
|
SanjayAttray (4/22/2009) select NULL,'manoj'union select Null,'1'union select NULL,'manoj' order by 2
or
select NULL,'manoj'union select Null,'1'union select NULL,'manoj' order by 1
gives you same result.
NULL 1 NULL manoj
I tried on all sql server versions and result is same. May be I am missing something here when some people are telling both 1 & 2 answers are correct.The point is that, as documented, the order of results from a union without an explicit ORDER BY cannot be guaranteed. As Hugo pointed out, it is possible under some circumstances to get a different order, hence, in theory, both answers are correct.
Of course, adding 'ORDER BY 2 DESC' forces answer 2.
Derek
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Thursday, January 17, 2013 2:34 PM
Points: 565,
Visits: 360
|
|
r.hensbergen (4/22/2009)
Jayeff (4/22/2009)
mike (4/22/2009)
Hey! Not fair. I lost a point because of a missing ORDER BY ? Like a previous poster mentioned, both 1 and 2 are correct.  Can I get my point back please? Yeah, me too! And see, you both got you point just by posting that you want your point back. ;)
Good point :)...but shouldn't these Qs be screened for accuracy?
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
SanjayAttray (4/22/2009) select NULL,'manoj'union select Null,'1'union select NULL,'manoj' order by 2
or
select NULL,'manoj'union select Null,'1'union select NULL,'manoj' order by 1
gives you same result.
NULL 1 NULL manoj
I tried on all sql server versions and result is same. May be I am missing something here when some people are telling both 1 & 2 answers are correct.
Try actually specifying an "order by" that can be honored by the dbms:select NULL,'manoj' union select Null,'1' union select NULL,'manoj' order by 2 desc
This returns the same two rows in a different order: ----------- ----- NULL manoj NULL 1
(2 row(s) affected) So, the difference between the two 2-rows-returned answers comes down to whether you assume the same collation was chosen (or accepted) at installation time. I lost the point because as a mainframe programmer I've got EBCDIC stuck in my head and figured that letters would sort before numbers. But even as I thought that, I was grumbling to myself that without an "order by", it's a crap-shoot between the two.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Friday, March 15, 2013 2:43 PM
Points: 3,924,
Visits: 1,554
|
|
John,
With " order by 2 desc " are you not forcing the query to get result in the way you had desired?
select NULL,'manoj' union select Null,'1' union select NULL,'manoj' order by 2 desc
SQL DBA.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 3:01 AM
Points: 266,
Visits: 1,023
|
|
Maybe I'm being pedantic, but I don't think that any of the answers provided are correct.
If you interpret a comma to be a column delimiter, the results should be either:
NULL,manoj NULL,1
...or:
NULL,1 NULL,manoj
Note that all of the answers in the original question have single quotes around manoj.
Chris
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Tuesday, December 04, 2012 11:16 AM
Points: 477,
Visits: 389
|
|
I got it right (guessed that it would sort alphabetically to remove duplicates, but that's not guaranteed, as mentioned) However, I reckon that *none* of the answers are (or should that be 'is?) correct.
I would prefer to see NULL, '1' and NULL,'manoj' (2 rows) rather than NULL, 1 and NULL,'manoj' (2 rows) as the answer, as all the second-field values are clearly stated as character strings.
Some may call that being picky, but hey, two of the 'mutually exclusive' answers are actually the same, according to set theory.
|
|
|
|