• 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.

    NULL1

    NULLmanoj

    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