{"id":13724,"date":"2023-12-29T00:19:28","date_gmt":"2023-12-29T07:19:28","guid":{"rendered":"https:\/\/www.mysqltutorial.org\/?page_id=13724"},"modified":"2023-12-29T00:19:29","modified_gmt":"2023-12-29T07:19:29","slug":"mysql-order-by","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/","title":{"rendered":"MySQL ORDER BY"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to sort the rows in a result set using the MySQL <code>ORDER BY<\/code> clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the MySQL ORDER BY clause<\/h2>\n\n\n\n<p>When you use the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">SELECT<\/a><\/code> statement to query data from a table, the order of rows in the result set is unspecified. <\/p>\n\n\n\n<p>To sort the rows in the result set, you add the <code>ORDER BY<\/code> clause to the <code>SELECT<\/code> statement. <\/p>\n\n\n\n<p>The following illustrates 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   column1 &#91;<span class=\"hljs-keyword\">ASC<\/span>|<span class=\"hljs-keyword\">DESC<\/span>], \n   column2 &#91;<span class=\"hljs-keyword\">ASC<\/span>|<span class=\"hljs-keyword\">DESC<\/span>],\n   ...;<\/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, you specify the one or more columns that you want to sort after the <code>ORDER BY<\/code> clause.<\/p>\n\n\n\n<p>The <code>ASC<\/code> stands for ascending and the&nbsp;<code>DESC<\/code> stands for descending. You use <code>ASC<\/code> to sort the result set in ascending order and <code>DESC<\/code> to sort the result set in descending order.<\/p>\n\n\n\n<p>This <code>ORDER BY<\/code> clause sorts the result set by the values in the <code>column1<\/code> in ascending order:<\/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\">ORDER BY column1 ASC;<\/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>And this <code>ORDER BY<\/code> clause sorts the result set by the values in the <code>column1<\/code> in descending 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\">ORDER BY column1 DESC;<\/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>By default, the <code>ORDER BY<\/code> clause uses <code>ASC<\/code> if you don&#8217;t explicitly specify any option. Therefore, the following <code>ORDER BY<\/code> clauses are equivalent:<\/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\">ORDER BY column1 ASC;<\/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>and<\/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\">ORDER BY column1;<\/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>If you want to sort the result set by multiple columns, you specify a comma-separated list of columns in the <code>ORDER BY<\/code> clause:<\/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\">ORDER BY\n   column1,\n   column2;<\/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>In this case, the <code>ORDER BY<\/code> clause sorts the result set by <code>column1<\/code> in ascending order first and sorts the sorted result set by <code>column2<\/code> in ascending order.<\/p>\n\n\n\n<p>It is possible to sort the result set by a column in ascending order and then by another column in descending order:<\/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\">ORDER BY\n    column1 ASC,\n    column2 DESC;<\/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>In this case, the <code>ORDER BY<\/code> clause:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, sort the result set by the values in the <code>column1<\/code> in ascending order.<\/li>\n\n\n\n<li>Then, sort the sorted result set by the values in the <code>column2<\/code>&nbsp; in descending order. Note that the order of values in the <code>column1<\/code> will not change in this step, only the order of values in the <code>column2<\/code> changes.<\/li>\n<\/ul>\n\n\n\n<p>When executing the <code>SELECT<\/code> statement with an <code>ORDER BY<\/code> clause, MySQL always evaluates the <code>ORDER BY<\/code> clause after the <code>FROM<\/code> and <code>SELECT<\/code> clauses:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/MySQL-ORDER-BY.svg\" alt=\"\" class=\"wp-image-10799\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL ORDER BY examples<\/h2>\n\n\n\n<p>We&#8217;ll use the <code>customers<\/code> table from the <a href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/mysql-sample-database\/\">sample database<\/a> for the demonstration:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/customers.svg\" alt=\"\" class=\"wp-image-10765\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using ORDER BY clause to sort the result set by one column example<\/h3>\n\n\n\n<p>The following query uses the <code>ORDER BY<\/code> clause to sort the customers by their last names in ascending order.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" 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  contactLastname, \n  contactFirstname \n<span class=\"hljs-keyword\">FROM<\/span> \n  customers \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  contactLastname;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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 class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-order-by\/#1\">Try It Out<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">+-----------------+------------------+\n| contactLastname | contactFirstname |\n+-----------------+------------------+\n| Accorti         | Paolo            |\n| Altagar,G M     | Raanan           |\n| Andersen        | Mel              |\n| Anton           | Carmen           |\n| Ashworth        | Rachel           |\n| Barajas         | Miguel           |\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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>If you want to sort customers by the last name in descending order, you use the <code>DESC<\/code>&nbsp;after the <code>contactLastname<\/code> column in the <code>ORDER BY<\/code> clause as shown in the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" 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  contactLastname, \n  contactFirstname \n<span class=\"hljs-keyword\">FROM<\/span> \n  customers \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  contactLastname <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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 class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-order-by\/#2\">Try It Out<\/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\">+-----------------+------------------+\n| contactLastname | contactFirstname |\n+-----------------+------------------+\n| Young           | Jeff             |\n| Young           | Julie            |\n| Young           | Mary             |\n| Young           | Dorothy          |\n| Yoshido         | Juri             |\n| Walker          | Brydey           |\n| Victorino       | Wendy            |\n| Urs             | Braun            |\n| Tseng           | Jerry            |\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<h3 class=\"wp-block-heading\">2) Using the ORDER BY clause to sort the result set by multiple columns example<\/h3>\n\n\n\n<p>If you want to sort the customers by the last name in descending order and then by the first name in ascending order, you specify both &nbsp;<code>DESC<\/code> and <code>ASC<\/code> in these respective columns as follows:<\/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  contactLastname, \n  contactFirstname \n<span class=\"hljs-keyword\">FROM<\/span> \n  customers \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  contactLastname <span class=\"hljs-keyword\">DESC<\/span>, \n  contactFirstname <span class=\"hljs-keyword\">ASC<\/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 class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-order-by\/#3\">Try It Out<\/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\">+-----------------+------------------+\n| contactLastname | contactFirstname |\n+-----------------+------------------+\n| Young           | Dorothy          |\n| Young           | Jeff             |\n| Young           | Julie            |\n| Young           | Mary             |\n| Yoshido         | Juri             |\n| Walker          | Brydey           |\n| Victorino       | Wendy            |\n| Urs             | Braun            |\n| Tseng           | Jerry            |\n| Tonini          | Daniel           |\n...<\/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<p>In this example, the <code>ORDER BY<\/code> &nbsp;clause sorts the result set by the last name in descending order first and then sorts the sorted result set by the first name in ascending order to make the final result set.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3) Using the ORDER BY clause to sort a result set by an expression example<\/h3>\n\n\n\n<p>See the following <code>orderdetails<\/code> table from the <a href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/mysql-sample-database\/\">sample database<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/orderdetails.svg\" alt=\"\" class=\"wp-image-10768\"\/><\/figure>\n\n\n\n<p>The following query selects the order line items from the <code>orderdetails<\/code> table. It calculates the subtotal for each line item and sorts the result set based on the subtotal.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" 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  orderNumber, \n  orderlinenumber, \n  quantityOrdered * priceEach \n<span class=\"hljs-keyword\">FROM<\/span> \n  orderdetails \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  quantityOrdered * priceEach <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><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 class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-order-by\/#4\">Try It Out<\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">+-------------+-----------------+-----------------------------+\n| orderNumber | orderlinenumber | quantityOrdered * priceEach |\n+-------------+-----------------+-----------------------------+\n|       10403 |               9 |                    11503.14 |\n|       10405 |               5 |                    11170.52 |\n|       10407 |               2 |                    10723.60 |\n|       10404 |               3 |                    10460.16 |\n|       10312 |               3 |                    10286.40 |\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><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 make the query more readable, you can assign a <a title=\"MySQL Column Alias\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-alias\/\">column alias<\/a>  to the expression in the <code>SELECT<\/code> clause and use the column alias in the <code>ORDER BY<\/code> clause as shown in the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" 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  orderNumber, \n  orderLineNumber, \n  quantityOrdered * priceEach <span class=\"hljs-keyword\">AS<\/span> subtotal \n<span class=\"hljs-keyword\">FROM<\/span> \n  orderdetails \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  subtotal <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><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 class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-order-by\/#5\">Try It Out<\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">+-------------+-----------------+----------+\n| orderNumber | orderLineNumber | subtotal |\n+-------------+-----------------+----------+\n|       10403 |               9 | 11503.14 |\n|       10405 |               5 | 11170.52 |\n|       10407 |               2 | 10723.60 |\n|       10404 |               3 | 10460.16 |\n|       10312 |               3 | 10286.40 |\n|       10424 |               6 | 10072.00 |\n|       10348 |               8 |  9974.40 |\n|       10405 |               3 |  9712.04 |\n|       10196 |               5 |  9571.08 |\n|       10206 |               6 |  9568.73 |\n ...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><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 use <code>subtotal<\/code> as the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-alias\/\">column alias<\/a> for the expression&nbsp;<code>quantityOrdered * priceEach<\/code> and sort the result set by the <code>subtotal<\/code> alias.<\/p>\n\n\n\n<p>Since MySQL evaluates the <code>SELECT<\/code> clause before the <code>ORDER BY<\/code> clause, you can use the column alias specified in the <code>SELECT<\/code> clause in the <code>ORDER BY<\/code> clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using MySQL ORDER BY clause to sort data using a custom list<\/h2>\n\n\n\n<p>The <code>FIELD()<\/code> function returns the index (position) of a value within a list of values.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>FIELD()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">FIELD(value, value1, value2, ...)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><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><code>value<\/code>: The value for which you want to find the position.<\/li>\n\n\n\n<li><code>value1, value2, ...<\/code>: A list of values against which you want to compare the specified value.<\/li>\n<\/ul>\n\n\n\n<p>The <code>FIELD()<\/code> function returns the position of the <code>value<\/code> in the list of values  value1, value2, and so on.<\/p>\n\n\n\n<p>If the <code>value<\/code> is not found in the list, the <code>FIELD()<\/code> function returns 0. <\/p>\n\n\n\n<p>For example, the following query returns 1 because the position of the string <code>'A'<\/code> is the first position on the list <code>'A'<\/code>, <code>'B'<\/code>, and <code>'C'<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" 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> <span class=\"hljs-keyword\">FIELD<\/span>(<span class=\"hljs-string\">'A'<\/span>, <span class=\"hljs-string\">'A'<\/span>, <span class=\"hljs-string\">'B'<\/span>,<span class=\"hljs-string\">'C'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><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>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">+--------------------------+\n| FIELD('A', 'A', 'B','C') |\n+--------------------------+\n|                        1 |\n+--------------------------+\n1 row in set (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><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>And the following example returns 2 because <code>'B'<\/code> has the second position in the list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" 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> <span class=\"hljs-keyword\">FIELD<\/span>(<span class=\"hljs-string\">'B'<\/span>, <span class=\"hljs-string\">'A'<\/span>,<span class=\"hljs-string\">'B'<\/span>,<span class=\"hljs-string\">'C'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><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>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">+-------------------------+\n| FIELD('B', 'A','B','C') |\n+-------------------------+\n|                       2 |\n+-------------------------+\n1 row in set (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><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>Let&#8217;s take a more practical example.<\/p>\n\n\n\n<p>See the following <code>orders<\/code> table from the sample database:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/orders.svg\" alt=\"\" class=\"wp-image-10769\"\/><\/figure>\n\n\n\n<p>Suppose that you want to sort the sales orders based on their statuses in the following order:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In Process<\/li>\n\n\n\n<li>On Hold<\/li>\n\n\n\n<li>Canceled<\/li>\n\n\n\n<li>Resolved<\/li>\n\n\n\n<li>Disputed<\/li>\n\n\n\n<li>Shipped<\/li>\n<\/ul>\n\n\n\n<p>To do this, you can use the <code>FIELD()<\/code> function to map each order status to a number and sort the result by the result of the <code>FIELD()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" 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  orderNumber, \n  <span class=\"hljs-keyword\">status<\/span> \n<span class=\"hljs-keyword\">FROM<\/span> \n  orders \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  <span class=\"hljs-keyword\">FIELD<\/span>(\n    <span class=\"hljs-keyword\">status<\/span>, \n    <span class=\"hljs-string\">'In Process'<\/span>, \n    <span class=\"hljs-string\">'On Hold'<\/span>, \n    <span class=\"hljs-string\">'Cancelled'<\/span>, \n    <span class=\"hljs-string\">'Resolved'<\/span>, \n    <span class=\"hljs-string\">'Disputed'<\/span>, \n    <span class=\"hljs-string\">'Shipped'<\/span>\n  );\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><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 class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-order-by\/#6\">Try It Out<\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">+-------------+------------+\n| orderNumber | status     |\n+-------------+------------+\n|       10425 | In Process |\n|       10421 | In Process |\n|       10422 | In Process |\n|       10420 | In Process |\n|       10424 | In Process |\n|       10423 | In Process |\n|       10414 | On Hold    |\n|       10401 | On Hold    |\n|       10334 | On Hold    |\n|       10407 | On Hold    |\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><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\">MySQL ORDER BY and NULL<\/h2>\n\n\n\n<p>In MySQL, <code>NULL<\/code> comes before non-NULL values. Therefore, when you the <code>ORDER BY<\/code> clause with the <code>ASC<\/code> option, <code>NULLs<\/code> appear first in the result set.<\/p>\n\n\n\n<p>For example, the following query uses the <code>ORDER BY<\/code> clause to sort employees by values in the <code>reportsTo<\/code> column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" 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  firstName, \n  lastName, \n  reportsTo \n<span class=\"hljs-keyword\">FROM<\/span> \n  employees \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  reportsTo;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><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>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">+-----------+-----------+-----------+\n| firstName | lastName  | reportsTo |\n+-----------+-----------+-----------+\n| Diane     | Murphy    |      NULL |\n| Mary      | Patterson |      1002 |\n| Jeff      | Firrelli  |      1002 |\n| William   | Patterson |      1056 |\n| Gerard    | Bondur    |      1056 |\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><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>However, if you use the <code>ORDER BY<\/code> with the <code>DESC<\/code> option, <code>NULLs<\/code> will appear last in the result set. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-27\" 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  firstName, \n  lastName, \n  reportsTo \n<span class=\"hljs-keyword\">FROM<\/span> \n  employees \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  reportsTo <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><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>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">+-----------+-----------+-----------+\n| firstName | lastName  | reportsTo |\n+-----------+-----------+-----------+\n| Yoshimi   | Kato      |      1621 |\n| Leslie    | Jennings  |      1143 |\n| Leslie    | Thompson  |      1143 |\n| Julie     | Firrelli  |      1143 |\n| ....\n| Mami      | Nishi     |      1056 |\n| Mary      | Patterson |      1002 |\n| Jeff      | Firrelli  |      1002 |\n| Diane     | Murphy    |      NULL |\n+-----------+-----------+-----------+\n23 rows in set (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><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\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>ORDER BY<\/code> clause to sort the result set by one or more columns.<\/li>\n\n\n\n<li>Use the <code>ASC<\/code> option to sort the result set in ascending order and the <code>DESC<\/code> option to sort the result set in descending order.<\/li>\n\n\n\n<li>The <code>ORDER BY<\/code> clause is evaluated after the <code>FROM<\/code> and <code>SELECT<\/code> clauses.<\/li>\n\n\n\n<li>In MySQL, <code>NULL<\/code> is lower than non-NULL values<\/li>\n<\/ul>\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=\"13724\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/\"\n\t\t\t\tdata-post-title=\"MySQL 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=\"13724\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/\"\n\t\t\t\tdata-post-title=\"MySQL 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>Show you how to use the MySQL ORDER BY clause to sort rows in the result set by one or more columns in ascending or descending order.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-13724","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>MySQL ORDER BY<\/title>\n<meta name=\"description\" content=\"Show you how to use the MySQL ORDER BY clause to sort rows in the result set by one or more columns 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.mysqltutorial.org\/mysql-basics\/mysql-order-by\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL ORDER BY\" \/>\n<meta property=\"og:description\" content=\"Show you how to use the MySQL ORDER BY clause to sort rows in the result set by one or more columns in ascending or descending order.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-29T07:19:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/MySQL-ORDER-BY.svg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-order-by\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-order-by\\\/\",\"name\":\"MySQL ORDER BY\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-order-by\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-order-by\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/MySQL-ORDER-BY.svg\",\"datePublished\":\"2023-12-29T07:19:28+00:00\",\"dateModified\":\"2023-12-29T07:19:29+00:00\",\"description\":\"Show you how to use the MySQL ORDER BY clause to sort rows in the result set by one or more columns in ascending or descending order.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-order-by\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-order-by\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-order-by\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/MySQL-ORDER-BY.svg\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/MySQL-ORDER-BY.svg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-order-by\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Basics\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL ORDER BY\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.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":"MySQL ORDER BY","description":"Show you how to use the MySQL ORDER BY clause to sort rows in the result set by one or more columns 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.mysqltutorial.org\/mysql-basics\/mysql-order-by\/","og_locale":"en_US","og_type":"article","og_title":"MySQL ORDER BY","og_description":"Show you how to use the MySQL ORDER BY clause to sort rows in the result set by one or more columns in ascending or descending order.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-12-29T07:19:29+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/MySQL-ORDER-BY.svg","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/","url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/","name":"MySQL ORDER BY","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/MySQL-ORDER-BY.svg","datePublished":"2023-12-29T07:19:28+00:00","dateModified":"2023-12-29T07:19:29+00:00","description":"Show you how to use the MySQL ORDER BY clause to sort rows in the result set by one or more columns in ascending or descending order.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/MySQL-ORDER-BY.svg","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/MySQL-ORDER-BY.svg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Basics","item":"https:\/\/www.mysqltutorial.org\/mysql-basics\/"},{"@type":"ListItem","position":3,"name":"MySQL ORDER BY"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/13724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=13724"}],"version-history":[{"count":2,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/13724\/revisions"}],"predecessor-version":[{"id":13726,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/13724\/revisions\/13726"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/174"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=13724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}