{"id":227,"date":"2018-12-13T06:24:49","date_gmt":"2018-12-13T06:24:49","guid":{"rendered":"https:\/\/barkhane.com\/sql\/2018\/12\/13\/sql-order-by-clause\/"},"modified":"2024-03-03T17:56:21","modified_gmt":"2024-03-03T17:56:21","slug":"sql-order-by-clause","status":"publish","type":"post","link":"https:\/\/barkhane.com\/sql\/sql-order-by-clause\/","title":{"rendered":"SQL ORDER BY Clause"},"content":{"rendered":"<p>The SQL ORDER BY clause is used for sorting data in ascending and descending order based on one or more columns of table of <a href=\"https:\/\/barkhane.com\/sql\/what-is-database\/\">database<\/a><\/p>\n<p>Some databases sort query results in ascending order by default.<\/p>\n<p>SQL ORDER BY Clause<\/p>\n<p>SQL ORDER BY syntax:<br \/>\n<code><\/code><\/p>\n<pre>SELECT expressions FROM tables WHERE conditions ORDER BY expression [ASC | DESC];<\/pre>\n<h4>SQL ORDER BY ASC<\/h4>\n<p>This statement is used to sort data in ascending order. If you miss the ASC attribute, SQL ORDER BY query takes ascending order by default.<br \/>\n<code><\/code><\/p>\n<pre>SELECT * FROM CUSTOMERS\nORDER BY NAME, SALARY;<\/pre>\n<h4>SQL ORDER BY DESC<\/h4>\n<p>This statement is used to sort data in descending order. You should use the DESC attribute in your ORDER BY clause as follows.<br \/>\n<code><\/code><\/p>\n<pre>SELECT * FROM CUSTOMERS\nORDER BY NAME DESC;<\/pre>\n<h4>SQL ORDER BY RANDOM<\/h4>\n<p>If you want to return a random row with MY SQL, Use the following code:<br \/>\n<code><\/code><\/p>\n<pre>SELECT column FROM table\nORDER BY RAND ()\nLIMIT 1<\/pre>\n<h4>SQL ORDER BY Multiple Columns<\/h4>\n<p>the following SQL statement selects all customers from the table named \\&#8221;customer\\&#8221;, stored by the \\&#8221;country\\&#8221; and \\&#8221;Customer-Name\\&#8221; columns:<br \/>\n<code><\/code><\/p>\n<pre>SELECT * FROM customers\nORDER BY country, Customer-Name;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The SQL ORDER BY clause is used for sorting data in ascending and descending order based on one or more [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[2],"tags":[],"class_list":["post-227","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/comments?post=227"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/227\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/media?parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/categories?post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/tags?post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}