{"id":180,"date":"2012-04-27T09:17:12","date_gmt":"2012-04-27T09:17:12","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2012\/04\/27\/sql-server-union-order-by-clause\/"},"modified":"2012-04-27T09:17:12","modified_gmt":"2012-04-27T09:17:12","slug":"sql-server-union-order-by-clause","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2012\/04\/27\/sql-server-union-order-by-clause\/","title":{"rendered":"sql server union order by clause"},"content":{"rendered":"

using union\/union all with order by clause in sql server.<\/strong><\/p>\n

In order to order the results of a union statement add order by clause with the column names corresponding to first select statement.
\nIf you are using alias names use the alias column names used in the first select statement<\/p>\n

Below example shows how to use union with order by clause in sql server:
\n[sql]
\nselect 1 AS Column1,’c’ AS Column2
\nunion
\nselect 2,’b’
\nunion
\nselect 3,’a’
\nORDER BY Column2
\n[\/sql]
\nresult:
\nColumn1 Column2
\n———– ——-
\n3 a
\n2 b
\n1 c<\/p>\n

(3 row(s) affected)<\/p>\n

with out order by claues:
\n[sql]
\nselect 1 AS Column1,’c’ AS Column2
\nunion
\nselect 2,’b’
\nunion
\nselect 3,’a’
\n[\/sql]
\nresult:
\nColumn1 Column2
\n———– ——-
\n1 c
\n2 b
\n3 a<\/p>\n

(3 row(s) affected)<\/p>\n

Applicable to: SQL Server 2000, SQL Server 2005, SQL Server 2008, SQL Server 2008 r2, SQL Server 2012<\/p>\n","protected":false},"excerpt":{"rendered":"

using union\/union all with order by clause in sql server. In order to order the results of a union statement add order by clause with the column names corresponding to first select statement. If you are using alias names use the alias column names used in the first select statement Below example shows how to…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41,1789],"tags":[1825,1826,1827,1828,1829,1830,1831,1832,1833,1834],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/180"}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=180"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/180\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}