{"id":63,"date":"2016-03-22T04:59:51","date_gmt":"2016-03-22T04:59:51","guid":{"rendered":"https:\/\/sqltutorial.org\/?page_id=63"},"modified":"2025-09-10T07:47:26","modified_gmt":"2025-09-10T14:47:26","slug":"sql-order-by","status":"publish","type":"page","link":"https:\/\/www.sqltutorial.org\/sql-order-by\/","title":{"rendered":"SQL ORDER BY"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: In this tutorial, you&#8217;ll learn how to use the SQL <code>ORDER BY<\/code> clause to sort the result set based on values of one or more rows in ascending or descending orders.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-sql-order-by-clause'>Introduction to SQL ORDER BY clause <a href=\"#introduction-to-sql-order-by-clause\" class=\"anchor\" id=\"introduction-to-sql-order-by-clause\" title=\"Anchor for Introduction to SQL ORDER BY clause\">#<\/a><\/h2>\n\n\n\n<p>The <code>ORDER BY<\/code> is an optional clause of the <code><a href=\"https:\/\/www.sqltutorial.org\/sql-select\/\">SELECT<\/a><\/code> statement. The <code>ORDER BY<\/code> clause allows you to sort the result set by one or more sort expressions in ascending and\/or descending order.<\/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=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  select_list\n<span class=\"hljs-keyword\">FROM<\/span>\n  table_name\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\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/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 a sort expression (<code>sort_expression<\/code>) in the <code>ORDER BY<\/code> clause based on which you want to sort the result set. The <code>sort_expression<\/code> can be a table column or an expression that involves a table column.<\/li>\n\n\n\n<li>Second, use <code>ASC<\/code> to sort the result set in ascending order and <code>DESC<\/code> to sort the result set in descending order.<\/li>\n<\/ul>\n\n\n\n<p class=\"note\"><code>ASC<\/code> and <code>DESC<\/code> stand for ascending and descending respectively.<\/p>\n\n\n\n<p>The <code>ORDER BY<\/code> clause uses the <code>ASC<\/code> option by default. It means that the <code>ORDER BY<\/code> clause sorts the rows in the result set by the <code>sort_expression<\/code> in ascending order if you don&#8217;t specify either <code>ASC<\/code> or <code>DESC<\/code>.<\/p>\n\n\n\n<p>Note that if you don&#8217;t specify the <code>ORDER BY<\/code> clause, the <code>SELECT<\/code> statement will not sort the result set. It means the rows in the result set don&#8217;t have a specific order.<\/p>\n\n\n\n<p>The <code>ORDER BY<\/code> clause allows you to sort the rows in the result set by multiple expressions. In this case, you need to use a comma-separated list of sort expressions in the <code>ORDER BY<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  select_list\n<span class=\"hljs-keyword\">FROM<\/span>\n  table_name\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  sort_expression_1 &#91;<span class=\"hljs-keyword\">ASC<\/span> | <span class=\"hljs-keyword\">DESC<\/span>],\n  sort_expression_2 &#91;<span class=\"hljs-keyword\">ASC<\/span> | <span class=\"hljs-keyword\">DESC<\/span>];<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, the <code>ORDER BY<\/code> clause sorts the result set by the <code>sort_expression_1<\/code> first and then sorts the sorted result set by the <code>sort_expression_2<\/code>.<\/p>\n\n\n\n<p>The database system evaluates the <code>SELECT<\/code> statement with the <code>ORDER BY<\/code> clause in the following order:<\/p>\n\n\n\n<ul 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<\/ul>\n\n\n\n<p>Since the database system evaluates the <code>ORDER BY<\/code> clause after the <code>SELECT<\/code> clause, you can use column aliases in the <code>ORDER BY<\/code> clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='sql-order-by-clause-examples'>SQL ORDER BY clause examples <a href=\"#sql-order-by-clause-examples\" class=\"anchor\" id=\"sql-order-by-clause-examples\" title=\"Anchor for SQL ORDER BY clause examples\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll use the <code>employees<\/code> table in the <a href=\"https:\/\/www.sqltutorial.org\/sql-sample-database\/\">sample database<\/a>&nbsp;for the demonstration.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"169\" height=\"273\" src=\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\" alt=\"employees_table\" class=\"wp-image-237\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='sorting-rows-by-one-column-example'>Sorting rows by one column example <a href=\"#sorting-rows-by-one-column-example\" class=\"anchor\" id=\"sorting-rows-by-one-column-example\" title=\"Anchor for Sorting rows by one column example\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>ORDER BY<\/code> clause to sort employees by first names in alphabetical order:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  employee_id,\n  first_name,\n  last_name\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  first_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p><p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGVtcGxveWVlX2lkLCBmaXJzdF9uYW1lLCBsYXN0X25hbWUgRlJPTSBlbXBsb3llZXMgT1JERVIgQlkgZmlyc3RfbmFtZTs%3D\">Try it<\/a><\/p>\n<p>Output:<\/p><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> employee_id | first_name  |  last_name\n-------------+-------------+-------------\n         121 | Adam        | Fripp\n         103 | Alexander   | Hunold\n         115 | Alexander   | Khoo\n         193 | Britney     | Everett\n         104 | Bruce       | Ernst\n...<\/code><\/span><\/pre>\n\n\n<p>In this example, the <code>ORDER BY<\/code> clause sorts the rows of the result set by the values in the <code>first_name<\/code> column.<\/p>\n\n\n\n<h3 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><\/h3>\n\n\n\n<p>The following example uses the <code>ORDER BY<\/code> clause to sort the employees by the first name in ascending order and the last name in descending order:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  employee_id,\n  first_name,\n  last_name\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  first_name,\n  last_name <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGVtcGxveWVlX2lkLCBmaXJzdF9uYW1lLCBsYXN0X25hbWUgRlJPTSBlbXBsb3llZXMgT1JERVIgQlkgZmlyc3RfbmFtZSwgbGFzdF9uYW1lIERFU0M7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> employee_id | first_name  |  last_name\n-------------+-------------+-------------\n         121 | Adam        | Fripp\n         115 | Alexander   | Khoo\n         103 | Alexander   | Hunold\n         193 | Britney     | Everett\n         104 | Bruce       | Ernst\n         179 | Charles     | Johnson\n...<\/code><\/span><\/pre>\n\n\n<p>In this example, the <code>ORDER BY<\/code> clause sorts rows by the first name in ascending order, then sorts the sorted result set by the last name in descending order.<\/p>\n\n\n\n<p>Notice the change in the position of two employees: <code>Alexander Khoo<\/code> and <code>Alexander Hunold<\/code> in the result set.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='sorting-rows-by-a-numeric-column-example'>Sorting rows by a numeric column example <a href=\"#sorting-rows-by-a-numeric-column-example\" class=\"anchor\" id=\"sorting-rows-by-a-numeric-column-example\" title=\"Anchor for Sorting rows by a numeric column example\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>ORDER BY<\/code> clause to sort employees by salary from high to low:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  employee_id,\n  first_name,\n  last_name,\n  salary\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  salary <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\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGVtcGxveWVlX2lkLCBmaXJzdF9uYW1lLCBsYXN0X25hbWUgRlJPTSBlbXBsb3llZXMgT1JERVIgQlkgZmlyc3RfbmFtZSwgbGFzdF9uYW1lIERFU0M7\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> employee_id | first_name  |  last_name  |  salary\n-------------+-------------+-------------+----------\n         100 | Steven      | King        | 24000.00\n         101 | Neena       | Kochhar     | 17000.00\n         102 | Lex         | De Haan     | 17000.00\n         145 | John        | Russell     | 14000.00\n         146 | Karen       | Partners    | 13500.00\n         201 | Michael     | Hartstein   | 13000.00<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='sorting-rows-by-dates-example'>Sorting rows by dates example <a href=\"#sorting-rows-by-dates-example\" class=\"anchor\" id=\"sorting-rows-by-dates-example\" title=\"Anchor for Sorting rows by dates example\">#<\/a><\/h3>\n\n\n\n<p>Besides the character and numeric data, you can use the <code>ORDER BY<\/code> clause to sort rows by dates.<\/p>\n\n\n\n<p>For example, the following statement uses the <code>ORDER BY<\/code> clause to sort the employees by hire dates from earliest to latest:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  first_name,\n  last_name,\n  hire_date\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  hire_date;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGZpcnN0X25hbWUsIGxhc3RfbmFtZSwgaGlyZV9kYXRlIEZST00gZW1wbG95ZWVzIE9SREVSIEJZIGhpcmVfZGF0ZTs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> first_name  |  last_name  | hire_date\n-------------+-------------+------------\n Steven      | King        | 1987-06-17\n Jennifer    | Whalen      | 1987-09-17\n Neena       | Kochhar     | 1989-09-21\n Alexander   | Hunold      | 1990-01-03\n Bruce       | Ernst       | 1991-05-21\n Lex         | De Haan     | 1993-01-13<\/code><\/span><\/pre>\n\n\n<p>To sort the employees by the hire dates in descending order, you can use the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  first_name,\n  last_name,\n  hire_date\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  hire_date <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\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGZpcnN0X25hbWUsIGxhc3RfbmFtZSwgaGlyZV9kYXRlIEZST00gZW1wbG95ZWVzIE9SREVSIEJZIGhpcmVfZGF0ZSBERVNDOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> first_name  |  last_name  | hire_date\n-------------+-------------+------------\n Charles     | Johnson     | 2000-01-04\n Luis        | Popp        | 1999-12-07\n Karen       | Colmenares  | 1999-08-10\n Kimberely   | Grant       | 1999-05-24\n Diana       | Lorentz     | 1999-02-07\n...<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='sorting-nulls'>Sorting NULLs <a href=\"#sorting-nulls\" class=\"anchor\" id=\"sorting-nulls\" title=\"Anchor for Sorting NULLs\">#<\/a><\/h2>\n\n\n\n<p>In SQL,&nbsp;<code>NULL<\/code> is a marker that indicates missing data or unknown value. <code>NULL<\/code> is special because you cannot compare it with any value.<\/p>\n\n\n\n<p>If you want to sort rows by a column that has <code>NULL<\/code>, you can have an option to place <code>NULL<\/code>s before or after other regular values.<\/p>\n\n\n\n<p>To place <code>NULL<\/code>s before other values, you use the <code>NULLS FIRST<\/code> option in the <code>ORDER BY<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" 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><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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>If you want to place <code>NULL<\/code> after other regular values, you use the <code>NULLS LAST<\/code> option:<\/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\">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-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>The following query uses the <code>ORDER BY<\/code> clause with the <code>NULLS FIRST<\/code> option to place the <code>NULL<\/code>s before other values:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" 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  first_name,\n  phone_number\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  phone_number <span class=\"hljs-keyword\">NULLS FIRST<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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.sqltutorial.org\/playground\/?q=U0VMRUNUIGZpcnN0X25hbWUsIHBob25lX251bWJlciBGUk9NIGVtcGxveWVlcyBPUkRFUiBCWSBwaG9uZV9udW1iZXIgTlVMTFMgRklSU1Q7\" 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-11\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> first_name  | phone_number\n-------------+--------------\n John        | NULL\n Charles     | NULL\n Kimberely   | NULL\n Jack        | NULL\n Jonathon    | NULL\n Karen       | NULL\n Jennifer    | 515.123.4444\n Steven      | 515.123.4567\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><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>The following query uses the <code>ORDER BY<\/code> clause with the <code>NULLS LAST<\/code> option to place the <code>NULL<\/code>s after other values:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  first_name,\n  phone_number\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  phone_number <span class=\"hljs-keyword\">NULLS<\/span> <span class=\"hljs-keyword\">LAST<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGZpcnN0X25hbWUsIHBob25lX251bWJlciBGUk9NIGVtcGxveWVlcyBPUkRFUiBCWSBwaG9uZV9udW1iZXIgTlVMTFMgTEFTVDs%3D\" 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-13\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> first_name  | phone_number\n-------------+--------------\n ...\n Irene       | 650.124.1224\n Sarah       | 650.501.1876\n Britney     | 650.501.2876\n Kimberely   | NULL\n Jack        | NULL\n Jonathon    | NULL\n Karen       | NULL\n John        | NULL\n Charles     | NULL<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><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 to sort rows in a result set.<\/li>\n\n\n\n<li>Use the <code>ASC<\/code> option to sort rows in ascending order and <code>DESC<\/code> option to sort rows in descending order.<\/li>\n\n\n\n<li>Use <code>NULLS FIRST<\/code> to place <code>NULL<\/code>s before and <code>NULLS LAST<\/code> to place <code>NULL<\/code>s after other non-NULL values.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='databases'>Databases <a href=\"#databases\" class=\"anchor\" id=\"databases\" title=\"Anchor for Databases\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL ORDER BY clause<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.oracletutorial.com\/oracle-basics\/oracle-order-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">Oracle ORDER BY clause<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-order-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQL Server ORDER BY clause<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">MySQL ORDER BY clause<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQLite ORDER BY clause<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-order-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">Db2 ORDER BY clause<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mariadbtutorial.com\/mariadb-basics\/mariadb-order-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">MariaDB ORDER BY clause<\/a><\/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<p><iframe loading=\"lazy\" class=\"iframe\" src=\"\/quiz\/?quiz=order-by\" name=\"quiz\" width=\"600\" height=\"700\"><\/iframe><\/p>\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=\"63\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-order-by\/\"\n\t\t\t\tdata-post-title=\"SQL 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=\"63\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-order-by\/\"\n\t\t\t\tdata-post-title=\"SQL 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>This tutorial shows you how to use the SQL ORDER BY clause to sort rows returned by the SELECT clause in ascending or descending order.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-63","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL ORDER BY<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use the SQL ORDER BY clause to sort rows returned by the SELECT clause in ascending or descending order.\" \/>\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.sqltutorial.org\/sql-order-by\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL ORDER BY\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use the SQL ORDER BY clause to sort rows returned by the SELECT clause in ascending or descending order.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqltutorial.org\/sql-order-by\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-10T14:47:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\" \/>\n\t<meta property=\"og:image:width\" content=\"169\" \/>\n\t<meta property=\"og:image:height\" content=\"273\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\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=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-order-by\/\",\"url\":\"https:\/\/www.sqltutorial.org\/sql-order-by\/\",\"name\":\"SQL ORDER BY\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqltutorial.org\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-order-by\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-order-by\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\",\"datePublished\":\"2016-03-22T04:59:51+00:00\",\"dateModified\":\"2025-09-10T14:47:26+00:00\",\"description\":\"This tutorial shows you how to use the SQL ORDER BY clause to sort rows returned by the SELECT clause in ascending or descending order.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-order-by\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqltutorial.org\/sql-order-by\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-order-by\/#primaryimage\",\"url\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\",\"contentUrl\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\",\"width\":169,\"height\":273,\"caption\":\"SQL Correlated Subquery - employees table\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-order-by\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqltutorial.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL ORDER BY\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqltutorial.org\/#website\",\"url\":\"https:\/\/www.sqltutorial.org\/\",\"name\":\"SQL Tutorial\",\"description\":\"An Interactive SQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqltutorial.org\/?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":"SQL ORDER BY","description":"This tutorial shows you how to use the SQL ORDER BY clause to sort rows returned by the SELECT clause in ascending or descending order.","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.sqltutorial.org\/sql-order-by\/","og_locale":"en_US","og_type":"article","og_title":"SQL ORDER BY","og_description":"This tutorial shows you how to use the SQL ORDER BY clause to sort rows returned by the SELECT clause in ascending or descending order.","og_url":"https:\/\/www.sqltutorial.org\/sql-order-by\/","og_site_name":"SQL Tutorial","article_modified_time":"2025-09-10T14:47:26+00:00","og_image":[{"width":169,"height":273,"url":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqltutorial.org\/sql-order-by\/","url":"https:\/\/www.sqltutorial.org\/sql-order-by\/","name":"SQL ORDER BY","isPartOf":{"@id":"https:\/\/www.sqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqltutorial.org\/sql-order-by\/#primaryimage"},"image":{"@id":"https:\/\/www.sqltutorial.org\/sql-order-by\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png","datePublished":"2016-03-22T04:59:51+00:00","dateModified":"2025-09-10T14:47:26+00:00","description":"This tutorial shows you how to use the SQL ORDER BY clause to sort rows returned by the SELECT clause in ascending or descending order.","breadcrumb":{"@id":"https:\/\/www.sqltutorial.org\/sql-order-by\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqltutorial.org\/sql-order-by\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqltutorial.org\/sql-order-by\/#primaryimage","url":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png","contentUrl":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png","width":169,"height":273,"caption":"SQL Correlated Subquery - employees table"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqltutorial.org\/sql-order-by\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"SQL ORDER BY"}]},{"@type":"WebSite","@id":"https:\/\/www.sqltutorial.org\/#website","url":"https:\/\/www.sqltutorial.org\/","name":"SQL Tutorial","description":"An Interactive SQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":0,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/63\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/media?parent=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}