{"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":"2025-06-07T09:56:18","modified_gmt":"2025-06-07T09:56:18","slug":"sql-order-by","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/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},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3919","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"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"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}