{"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. Below example shows how to use union with order by clause in sql server: (3 row(s) affected)<\/p>\n with out order by claues: (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],"class_list":["post-180","post","type-post","status-publish","format-standard","hentry","category-sql-server-basics","category-t-sql-code-examples","tag-order-by-clause-in-union-query","tag-sql-server-2000-union-order-by-clause","tag-sql-server-2005-union-order-by-clause","tag-sql-server-2008-r2-union-order-by-clause","tag-sql-server-2008-union-order-by-clause","tag-sql-server-2012-union-order-by-clause","tag-sql-server-union-all-order-by-clause-example","tag-sql-server-union-all-order-by-clause-sql","tag-union-order-by-clause","tag-union-order-by-clause-sql"],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"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}]}}
\nIf you are using alias names use the alias column names used in the first select statement<\/p>\n
\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
\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