{"id":3919,"date":"2025-06-06T20:17:57","date_gmt":"2025-06-06T20:17:57","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3919"},"modified":"2026-09-26T13:13:41","modified_gmt":"2026-09-26T07:43:41","slug":"sql-order-by","status":"publish","type":"post","link":"https:\/\/w3buddy.com\/blog\/sql-order-by\/","title":{"rendered":"SQL ORDER BY"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>ORDER BY<\/code> clause lets you <strong>sort the rows<\/strong> returned by a query \u2014 either in ascending or descending order.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Basic Usage<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Sort by one column (default is ASC)\nSELECT name, salary FROM employees\nORDER BY salary;\n\n-- Sort in descending order\nSELECT name, salary FROM employees\nORDER BY salary DESC;\n\n-- Sort by multiple columns\nSELECT name, department, salary FROM employees\nORDER BY department ASC, salary DESC;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Use with Aliases &amp; Expressions<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Use column alias in ORDER BY\nSELECT name, salary * 12 AS annual_salary\nFROM employees\nORDER BY annual_salary DESC;\n\n-- Order by column number (not recommended, but valid)\nSELECT name, salary FROM employees\nORDER BY 2 DESC;  -- refers to 'salary'<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Quick Recap<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Syntax<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>ORDER BY column<\/code><\/td><td>Sort by column (ascending by default)<\/td><\/tr><tr><td><code>ORDER BY column DESC<\/code><\/td><td>Sort in descending order<\/td><\/tr><tr><td><code>ORDER BY col1, col2 DESC<\/code><\/td><td>Sort by multiple columns<\/td><\/tr><tr><td><code>ORDER BY alias \/ expression<\/code><\/td><td>Sort by calculated or renamed field<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <code>ORDER BY<\/code> helps organize results meaningfully<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The ORDER BY clause lets you sort the rows returned by a query \u2014 either in ascending or descending order. \ud83d\udd39 Basic Usage \ud83d\udd39 Use with Aliases &amp; Expressions \ud83e\udde0 Quick Recap Syntax Meaning ORDER BY column Sort by column (ascending by default) ORDER BY column DESC Sort in descending order ORDER BY col1, col2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowu461DA:productID":"","footnotes":""},"categories":[1225,979],"tags":[],"class_list":["post-3919","post","type-post","status-publish","format-standard","hentry","category-database","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/3919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/comments?post=3919"}],"version-history":[{"count":2,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/3919\/revisions"}],"predecessor-version":[{"id":4031,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/3919\/revisions\/4031"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/tags?post=3919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}