Home > DeveloperSection > Interviews > How To Use ORDER BY with UNION Operators in MS SQL Server?

Posted on    November-09-2010 12:00 AM

 DB2 DB2 
Ratings:
 1 Answer(s)
  1624  View(s)
Rate this:

Amit Singh

Total Post:565

Points:2825
Posted on    November-09-2010 1:00 AM

If you need to sort the output from two queries grouped together with a UNION operator, you need to apply the ORDER BY clause at the group level, not at the subquery level.

for example:
(SELECT * FROM sample WHERE tag = 'MCA' ORDER BY created)
UNION
(SELECT * FROM sample WHERE tag = 'BCA' ORDER BY created)

Don't want to miss updates? Please click the below button!

Follow MindStick