Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2007
    Posts
    2

    Unanswered: Sorting in UNION ALL

    Hi

    I have following query


    SELECT 'ALL'
    UNION ALL
    SELECT DISTINCT Emp_Name

    FROM

    EMP
    How do I sort the result ?
    Thanks

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    by adding an ORDER BY clause

    if you don't give the column an alias name in the first subselect, you can use the ordinal column number --
    Code:
    SELECT 'ALL' 
    UNION ALL 
    SELECT DISTINCT Emp_Name 
      FROM EMP 
    ORDER
        BY 1
    alternatively, if you reverse the subselects, you can use the column name --
    Code:
    SELECT DISTINCT Emp_Name 
      FROM EMP 
    UNION ALL 
    SELECT 'ALL' 
    ORDER
        BY Emp_Name
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Nov 2007
    Posts
    2
    Thanks for the quick reply. It worked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •