{"id":445,"date":"2024-11-28T09:04:57","date_gmt":"2024-11-28T02:04:57","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=445"},"modified":"2025-01-02T08:54:28","modified_gmt":"2025-01-02T01:54:28","slug":"postgresql-order-by","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/","title":{"rendered":"PostgreSQL ORDER BY"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PostgreSQL <code>ORDER BY<\/code> clause in the <code>SELECT<\/code> statement to sort rows in ascending or descending orders.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-postgresql-order-by-clause'>Introduction to PostgreSQL ORDER BY clause <a href=\"#introduction-to-postgresql-order-by-clause\" class=\"anchor\" id=\"introduction-to-postgresql-order-by-clause\" title=\"Anchor for Introduction to PostgreSQL ORDER BY clause\">#<\/a><\/h2>\n\n\n\n<p>The <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-select\/\">SELECT<\/a><\/code> statement returns a result set with rows in an unspecified order by default. To sort rows returned by the <code>SELECT<\/code> statement, you use the <code>ORDER BY<\/code> clause.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>ORDER BY<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  column1,\n  column2,\n  ...\n<span class=\"hljs-keyword\">FROM<\/span>\n  <span class=\"hljs-built_in\">table_name<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  sort_expression &#91;<span class=\"hljs-keyword\">ASC<\/span> | <span class=\"hljs-keyword\">DESC<\/span>]; <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the table name from which you want to retrieve data in the <code>FROM<\/code> clause.<\/li>\n\n\n\n<li>Second, provide the columns to include in the result set in the <code>SELECT<\/code> clause.<\/li>\n\n\n\n<li>Third, specify the sort expression in the <code>ORDER BY<\/code> clause.<\/li>\n<\/ul>\n\n\n\n<p>The <code>sort_expression<\/code> can be a column or an expression for sorting.<\/p>\n\n\n\n<p>Use <code>ASC<\/code> to sort rows in ascending order and <code>DESC<\/code> to sort rows in descending order.<\/p>\n\n\n\n<p><code>ASC<\/code> or <code>DESC<\/code> option is optional. The <code>ORDER BY<\/code> clause uses the <code>ASC<\/code> by default if you don&#8217;t explicitly specify it.<\/p>\n\n\n\n<p>PostgreSQL evaluates the clauses in the <code>SELECT<\/code> statement in the following orders:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>FROM<\/code><\/li>\n\n\n\n<li><code>SELECT<\/code><\/li>\n\n\n\n<li><code>ORDER BY<\/code><\/li>\n<\/ol>\n\n\n\n<p>If you have column aliases in the <code>SELECT<\/code> clause, you can use them in the <code>ORDER BY<\/code> clause. The reason is that PostgreSQL evaluates the <code>SELECT<\/code> clause before the <code>ORDER BY<\/code> clause.  The column aliases that are available in the <code>SELECT<\/code> clause are accessible by the <code>ORDER BY<\/code> clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='setting-up-a-sample-table'>Setting up a sample table <a href=\"#setting-up-a-sample-table\" class=\"anchor\" id=\"setting-up-a-sample-table\" title=\"Anchor for Setting up a sample table\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-table\/\">create a new table<\/a>, <code>inventories<\/code>, to practice with the <code>ORDER BY<\/code>clause:<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>SQL Script to create and insert some rows into the inventories table:<\/summary><pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> inventories (\n    <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n    price <span class=\"hljs-type\">DEC<\/span>(<span class=\"hljs-number\">11<\/span>,<span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n    quantity <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n    color <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">50<\/span>),\n    updated_date <span class=\"hljs-type\">DATE<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>    \n);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> inventories (<span class=\"hljs-type\">name<\/span>, price, quantity, color, updated_date) \n\n<span class=\"hljs-keyword\">VALUES<\/span>\n(<span class=\"hljs-string\">'iPhone 15 Black'<\/span>, <span class=\"hljs-number\">999.99<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-string\">'Black'<\/span>, <span class=\"hljs-string\">'2024-12-01'<\/span>),\n(<span class=\"hljs-string\">'Galaxy S23'<\/span>, <span class=\"hljs-number\">899.99<\/span>, <span class=\"hljs-number\">12<\/span>, <span class=\"hljs-keyword\">NULL<\/span>, <span class=\"hljs-string\">'2024-12-02'<\/span>),\n(<span class=\"hljs-string\">'Pixel 8'<\/span>, <span class=\"hljs-number\">799.99<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-keyword\">NULL<\/span>, <span class=\"hljs-string\">'2024-12-02'<\/span>),\n(<span class=\"hljs-string\">'iPhone 15 Pro'<\/span>, <span class=\"hljs-number\">1099.99<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-string\">'Silver'<\/span>, <span class=\"hljs-string\">'2024-12-03'<\/span>),\n(<span class=\"hljs-string\">'Galaxy S23 Ultra'<\/span>, <span class=\"hljs-number\">1199.99<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-string\">'Black'<\/span>, <span class=\"hljs-string\">'2024-12-03'<\/span>),\n(<span class=\"hljs-string\">'Pixel 8 Pro'<\/span>, <span class=\"hljs-number\">999.99<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-string\">'Red'<\/span>, <span class=\"hljs-string\">'2024-12-03'<\/span>),\n(<span class=\"hljs-string\">'iPhone 15 Pro Max'<\/span>, <span class=\"hljs-number\">1299.99<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-string\">'Gold'<\/span>, <span class=\"hljs-string\">'2024-12-04'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id='sorting-rows-by-one-column'>Sorting rows by one column <a href=\"#sorting-rows-by-one-column\" class=\"anchor\" id=\"sorting-rows-by-one-column\" title=\"Anchor for Sorting rows by one column\">#<\/a><\/h2>\n\n\n\n<p>The following statement uses the <code>ORDER BY<\/code> clause to sort the product in  the inventory by price from lowest to highest:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">name<\/span>,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  price;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=orderby&amp;q=U0VMRUNUIG5hbWUsIHByaWNlIEZST00gaW52ZW50b3JpZXMgT1JERVIgQlkgcHJpY2U7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">       name        |  price\n-------------------+---------\n Pixel 8           |  799.99\n Galaxy S23        |  899.99\n Pixel 8 Pro       |  999.99\n iPhone 15 Black   |  999.99\n iPhone 15 Pro     | 1099.99\n Galaxy S23 Ultra  | 1199.99\n iPhone 15 Pro Max | 1299.99<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To sort the products by price in descending order, you use the <code>DESC<\/code> option:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">name<\/span>,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  price <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=orderby&amp;q=U0VMRUNUIG5hbWUsIHByaWNlIEZST00gaW52ZW50b3JpZXMgT1JERVIgQlkgcHJpY2UgREVTQzs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">       name        |  price\n-------------------+---------\n iPhone 15 Pro Max | 1299.99\n Galaxy S23 Ultra  | 1199.99\n iPhone 15 Pro     | 1099.99\n iPhone 15 Black   |  999.99\n Pixel 8 Pro       |  999.99\n Galaxy S23        |  899.99\n Pixel 8           |  799.99<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='sorting-rows-by-multiple-columns'>Sorting rows by multiple columns <a href=\"#sorting-rows-by-multiple-columns\" class=\"anchor\" id=\"sorting-rows-by-multiple-columns\" title=\"Anchor for Sorting rows by multiple columns\">#<\/a><\/h2>\n\n\n\n<p>Some products control the same price; you can sort the products by price first and then sort the sorted product list by names.<\/p>\n\n\n\n<p>The following query uses the <code>ORDER BY<\/code> clause to sort products by values in the <code>price<\/code> and <code>name<\/code> columns:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">name<\/span>,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  price <span class=\"hljs-keyword\">DESC<\/span>,\n  <span class=\"hljs-type\">name<\/span> <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=orderby&amp;q=U0VMRUNUIG5hbWUsIHByaWNlIEZST00gaW52ZW50b3JpZXMgT1JERVIgQlkgcHJpY2UgREVTQywgbmFtZSBERVNDOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">       name        |  price\n-------------------+---------\n iPhone 15 Pro Max | 1299.99\n Galaxy S23 Ultra  | 1199.99\n iPhone 15 Pro     | 1099.99\n Pixel 8 Pro       |  999.99\n iPhone 15 Black   |  999.99\n Galaxy S23        |  899.99\n Pixel 8           |  799.99<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='sorting-rows-by-expressions'>Sorting rows by expressions <a href=\"#sorting-rows-by-expressions\" class=\"anchor\" id=\"sorting-rows-by-expressions\" title=\"Anchor for Sorting rows by expressions\">#<\/a><\/h2>\n\n\n\n<p>The following statement calculates the inventory amount of each product and sorts the products by inventory amount from high to low:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">name<\/span>,\n  price * quantity\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  price * quantity <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=orderby&amp;q=U0VMRUNUIG5hbWUsIHByaWNlICogcXVhbnRpdHkgRlJPTSBpbnZlbnRvcmllcyBPUkRFUiBCWSBwcmljZSAqIHF1YW50aXR5IERFU0M7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">       name        | ?column?\n-------------------+----------\n Galaxy S23        | 10799.88\n iPhone 15 Black   |  9999.90\n Pixel 8 Pro       |  8999.91\n iPhone 15 Pro     |  6599.94\n Pixel 8           |  5599.93\n Galaxy S23 Ultra  |  4799.96\n iPhone 15 Pro Max |  3899.97<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since PostgreSQL evaluates the <code>SELECT<\/code> statement before the <code>ORDER BY<\/code> statement, you can use a column alias in the <code>SELECT<\/code> and <code>ORDER BY<\/code> clauses:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">name<\/span>,\n  price * quantity <span class=\"hljs-keyword\">AS<\/span> amount\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  amount <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=orderby&amp;q=U0VMRUNUIG5hbWUsIHByaWNlICogcXVhbnRpdHkgQVMgYW1vdW50IEZST00gaW52ZW50b3JpZXMgT1JERVIgQlkgYW1vdW50IERFU0M7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">       name        |  amount\n-------------------+----------\n Galaxy S23        | 10799.88\n iPhone 15 Black   |  9999.90\n Pixel 8 Pro       |  8999.91\n iPhone 15 Pro     |  6599.94\n Pixel 8           |  5599.93\n Galaxy S23 Ultra  |  4799.96\n iPhone 15 Pro Max |  3899.97<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we assign the <code>amount<\/code> as the column alias for the expression <code>price * quantity<\/code> and then use it in the <code>ORDER BY<\/code> clause for sorting.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='sorting-rows-by-dates'>Sorting rows by dates <a href=\"#sorting-rows-by-dates\" class=\"anchor\" id=\"sorting-rows-by-dates\" title=\"Anchor for Sorting rows by dates\">#<\/a><\/h2>\n\n\n\n<p>The following statements use the <code>ORDER BY<\/code>clause to sort rows in the inventories table by updated dates from the earliest to the latest:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">name<\/span>,\n  updated_date\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  updated_date;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=orderby&amp;q=U0VMRUNUIG5hbWUsIHVwZGF0ZWRfZGF0ZSBGUk9NIGludmVudG9yaWVzIE9SREVSIEJZIHVwZGF0ZWRfZGF0ZTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">       name        | updated_date\n-------------------+--------------\n iPhone 15 Black   | 2024-12-01\n Galaxy S23        | 2024-12-02\n Pixel 8           | 2024-12-02\n iPhone 15 Pro     | 2024-12-03\n Galaxy S23 Ultra  | 2024-12-03\n Pixel 8 Pro       | 2024-12-03\n iPhone 15 Pro Max | 2024-12-04<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can sort products in the inventory by updated date from latest to earliest by using the <code>DESC<\/code> option as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">name<\/span>,\n  updated_date\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  updated_date <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=orderby&amp;q=U0VMRUNUIG5hbWUsIHVwZGF0ZWRfZGF0ZSBGUk9NIGludmVudG9yaWVzIE9SREVSIEJZIHVwZGF0ZWRfZGF0ZSBERVNDOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">       name        | updated_date\n-------------------+--------------\n iPhone 15 Pro Max | 2024-12-04\n iPhone 15 Pro     | 2024-12-03\n Galaxy S23 Ultra  | 2024-12-03\n Pixel 8 Pro       | 2024-12-03\n Galaxy S23        | 2024-12-02\n Pixel 8           | 2024-12-02\n iPhone 15 Black   | 2024-12-01<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-order-by-dealing-with-nulls-in-sorting'>PostgreSQL ORDER BY: Dealing with NULLs in sorting <a href=\"#postgresql-order-by-dealing-with-nulls-in-sorting\" class=\"anchor\" id=\"postgresql-order-by-dealing-with-nulls-in-sorting\" title=\"Anchor for PostgreSQL ORDER BY: Dealing with NULLs in sorting\">#<\/a><\/h2>\n\n\n\n<p>In PostgreSQL, <code>NULL<\/code> means unknown or missing data. Since <code>NULL<\/code> is unknown, you cannot compare it with any other values.<\/p>\n\n\n\n<p>However, PostgreSQL needs to know which values are before or after other values to perform sorting. When it comes to <code>NULL<\/code>, PostgreSQL provides two options in the <code>ORDER BY<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> sort_expression <span class=\"hljs-keyword\">NULLS FIRST<\/span>;\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> sort_expression <span class=\"hljs-keyword\">NULLS LAST<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>NULLS FIRST<\/code> places <code>NULLs<\/code> before other non-NULL values.<\/li>\n\n\n\n<li><code>NULLS LAST<\/code> places <code>NULLs<\/code> after other non-NULL values.<\/li>\n<\/ul>\n\n\n\n<p class=\"note\">Note that between the sort expression and <code>NULLS FIRST<\/code> or <code>NULLS LAST<\/code>, you can use the <code>ASC<\/code> or <code>DESC<\/code> option.<\/p>\n\n\n\n<p>The following statement uses the <code>ORDER BY<\/code> clause to sort the products by color alphabetically, placing <code>NULLs<\/code> first:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">name<\/span>,\n  color\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  color <span class=\"hljs-keyword\">NULLS FIRST<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=orderby&amp;q=U0VMRUNUIG5hbWUsIGNvbG9yIEZST00gaW52ZW50b3JpZXMgT1JERVIgQlkgY29sb3IgTlVMTFMgRklSU1Q7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">       name        | color\n-------------------+--------\n Galaxy S23        | NULL\n Pixel 8           | NULL\n iPhone 15 Black   | Black\n Galaxy S23 Ultra  | Black\n iPhone 15 Pro Max | Gold\n Pixel 8 Pro       | Red\n iPhone 15 Pro     | Silver<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To place <code>NULLs<\/code> after other non-NULL values, you can use the <code>NULLS LAST<\/code> option:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">name<\/span>,\n  color\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  color <span class=\"hljs-keyword\">NULLS LAST<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=orderby&amp;q=U0VMRUNUIG5hbWUsIGNvbG9yIEZST00gaW52ZW50b3JpZXMgT1JERVIgQlkgY29sb3IgTlVMTFMgTEFTVDs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">       name        | color\n-------------------+--------\n Galaxy S23 Ultra  | Black\n iPhone 15 Black   | Black\n iPhone 15 Pro Max | Gold\n Pixel 8 Pro       | Red\n iPhone 15 Pro     | Silver\n Pixel 8           | NULL\n Galaxy S23        | NULL<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>ORDER BY<\/code> clause in the <code>SELECT<\/code> statement to sort rows by one or more columns.<\/li>\n\n\n\n<li>PostgreSQL evaluates the <code>ORDER BY<\/code> clause after the <code>SELECT<\/code> clause.<\/li>\n\n\n\n<li>The <code>ASC<\/code> option orders rows from low to high, while the <code>DESC<\/code> option orders from high to low.<\/li>\n\n\n\n<li>The <code>NULLS FIRST<\/code> places <code>NULLs<\/code> before, and <code>NULLS LAST<\/code> puts <code>NULLs<\/code> after other non-NULL values.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=order-by\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"445\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL ORDER BY\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"445\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL ORDER BY\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to use the PostgreSQL ORDER BY clause in the SELECT statement to sort rows in ascending or descending orders. Introduction to PostgreSQL ORDER BY clause # The SELECT statement returns a result set with rows in an unspecified order by default. To sort rows returned by the SELECT [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":18,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-445","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL ORDER BY: Sorting Rows in a Query<\/title>\n<meta name=\"description\" content=\"You&#039;ll learn how to use the PostgreSQL ORDER BY clause in the SELECT statement to sort rows in ascending or descending orders.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL ORDER BY: Sorting Rows in a Query\" \/>\n<meta property=\"og:description\" content=\"You&#039;ll learn how to use the PostgreSQL ORDER BY clause in the SELECT statement to sort rows in ascending or descending orders.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-02T01:54:28+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-order-by\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-order-by\\\/\",\"name\":\"PostgreSQL ORDER BY: Sorting Rows in a Query\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2024-11-28T02:04:57+00:00\",\"dateModified\":\"2025-01-02T01:54:28+00:00\",\"description\":\"You'll learn how to use the PostgreSQL ORDER BY clause in the SELECT statement to sort rows in ascending or descending orders.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-order-by\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-order-by\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-order-by\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL ORDER BY\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL ORDER BY: Sorting Rows in a Query","description":"You'll learn how to use the PostgreSQL ORDER BY clause in the SELECT statement to sort rows in ascending or descending orders.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL ORDER BY: Sorting Rows in a Query","og_description":"You'll learn how to use the PostgreSQL ORDER BY clause in the SELECT statement to sort rows in ascending or descending orders.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-02T01:54:28+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/","name":"PostgreSQL ORDER BY: Sorting Rows in a Query","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2024-11-28T02:04:57+00:00","dateModified":"2025-01-02T01:54:28+00:00","description":"You'll learn how to use the PostgreSQL ORDER BY clause in the SELECT statement to sort rows in ascending or descending orders.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL ORDER BY"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=445"}],"version-history":[{"count":5,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/445\/revisions"}],"predecessor-version":[{"id":1336,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/445\/revisions\/1336"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}