HOW TO USE ORDER BY WITH UNION OPERATORS IN MS SQL SERVER?

Amit Singh

Total Post:565

Points:2825
Posted by  Amit Singh
DB2 
DB2
 1825  View(s)
Ratings:
Rate this:
  1. Amit Singh

    Post:565

    Points:2825
    How To Use ORDER BY with UNION Operators in MS SQL Server?

    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)

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!