{"id":6932,"date":"2018-08-21T07:33:34","date_gmt":"2018-08-21T14:33:34","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=6932"},"modified":"2024-01-04T07:12:14","modified_gmt":"2024-01-04T14:12:14","slug":"mysql-row_number-function","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/","title":{"rendered":"MySQL ROW_NUMBER Function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about the MySQL <code>ROW_NUMBER()<\/code> function and how to use it to generate a sequential number for each row in the result set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL ROW_NUMBER() function<\/h2>\n\n\n\n<p>MySQL introduced the <code>ROW_NUMBER()<\/code> function since version 8.0. The <code>ROW_NUMBER()<\/code> is a <a href=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/\">window function<\/a> or analytic function that assigns a sequential number to each row in the result set. The first number begins with one.<\/p>\n\n\n\n<p class=\"note\">Notice that if you use MySQL with a version less than 8.0, you can&nbsp;<a href=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-emulation\/\">emulate some functionality of the&nbsp;<code>ROW_NUMBER()<\/code>&nbsp;function<\/a>&nbsp;using various techniques.<\/p>\n\n\n\n<p>The following shows the syntax of the <code>ROW_NUMBER()<\/code>function:<\/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\">ROW_NUMBER() OVER (&lt;partition_definition&gt; &lt;order_definition&gt;)<\/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<h3 class=\"wp-block-heading\">partition_definition<\/h3>\n\n\n\n<p>The <code>partition_definition<\/code> has the following syntax:<\/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\">PARTITION BY &lt;expression&gt;,&#91;{,&lt;expression&gt;}...]<\/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>The <code>PARTITION BY<\/code> clause breaks the rows into smaller sets. The <code>expression<\/code> can be any valid expression that would be used in the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-group-by\/\">GROUP BY<\/a><\/code> clause. It&#8217;s possible to use multiple expressions separated by a comma (<code>,<\/code>).<\/p>\n\n\n\n<p>The <code>PARTITION BY<\/code> clause is optional. If you omit it, the entire result set is considered a partition. However, when you use the <code>PARTITION BY<\/code> clause, each partition can be also considered as a window.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">order_definition<\/h3>\n\n\n\n<p>The <code>order_definition<\/code> syntax looks like the following:<\/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 &lt;expression&gt; &#91;ASC|DESC],&#91;{,&lt;expression&gt;}...]<\/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>The purpose of the <code>ORDER BY<\/code> clause is to set the orders of rows. Note that this <code>ORDER BY<\/code> clause is independent of the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/\">ORDER BY<\/a><\/code> clause of the query.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL ROW_NUMBER() function examples<\/h2>\n\n\n\n<p>Let&#8217;s use the <code>products<\/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\"><img loading=\"lazy\" decoding=\"async\" width=\"181\" height=\"231\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/products_table.png\" alt=\"products table\" class=\"wp-image-2493\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1) Assigning sequential numbers to rows<\/h3>\n\n\n\n<p>The following statement uses the <code>ROW_NUMBER()<\/code> function to assign a sequential number to each row from the <code>products<\/code> table:<\/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  ROW_NUMBER() <span class=\"hljs-keyword\">OVER<\/span> (\n    <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> productName\n  ) row_num, \n  productName, \n  msrp \n<span class=\"hljs-keyword\">FROM<\/span> \n  products \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  productName;<\/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>Here is the output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"325\" height=\"236\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-ROW_NUMBER-function-assign-sequential-numbers.png\" alt=\"MySQL ROW_NUMBER function - assign sequential numbers\" class=\"wp-image-6957\" title=\"MySQL ROW_NUMBER function - assign sequential numbers\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-ROW_NUMBER-function-assign-sequential-numbers.png 325w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-ROW_NUMBER-function-assign-sequential-numbers-200x145.png 200w\" sizes=\"auto, (max-width: 325px) 100vw, 325px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2) Finding the top N rows of every group<\/h3>\n\n\n\n<p>You can use the <code>ROW_NUMBER()<\/code> function to find the&nbsp;top N rows for every group, for example, the top three sales employees by sales channels or the top five high-performance products by product categories.<\/p>\n\n\n\n<p>The following statement uses the <code>ROW_NUMBER()<\/code> to find the top three products by product line that have the highest inventory:<\/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\">WITH<\/span> inventory <span class=\"hljs-keyword\">AS<\/span> (\n  <span class=\"hljs-keyword\">SELECT<\/span> \n    productLine, \n    productName, \n    quantityInStock, \n    ROW_NUMBER() <span class=\"hljs-keyword\">OVER<\/span> (\n      <span class=\"hljs-keyword\">PARTITION<\/span> <span class=\"hljs-keyword\">BY<\/span> productLine \n      <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n        quantityInStock <span class=\"hljs-keyword\">DESC<\/span>\n    ) row_num \n  <span class=\"hljs-keyword\">FROM<\/span> \n    products\n) \n<span class=\"hljs-keyword\">SELECT<\/span> \n  productLine, \n  productName, \n  quantityInStock \n<span class=\"hljs-keyword\">FROM<\/span> \n  inventory \n<span class=\"hljs-keyword\">WHERE<\/span> \n  row_num &lt;= <span class=\"hljs-number\">3<\/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>In this example,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, we used the <code>ROW_NUMER()<\/code> function to rank the inventory of all products in each product line by partitioning all products by product line and ordering them by quantity in stock in descending order.&nbsp; As a result, each product is assigned a rank based on its quantity in stock. and the rank is reset for each product line.<\/li>\n\n\n\n<li>Then, we selected only products whose rank is less than or equal to three.<\/li>\n<\/ul>\n\n\n\n<p>The following shows the output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"406\" height=\"264\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-ROW_NUMBER-function-Top-N-rows-from-group.png\" alt=\"MySQL ROW_NUMBER function - Top N rows from group\" class=\"wp-image-6955\" title=\"MySQL ROW_NUMBER function - Top N rows from group\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-ROW_NUMBER-function-Top-N-rows-from-group.png 406w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-ROW_NUMBER-function-Top-N-rows-from-group-200x130.png 200w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3) Removing duplicate rows<\/h3>\n\n\n\n<p>You can use the <code>ROW_NUMBER()<\/code> to turn non-unique rows into unique rows and then <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-delete-duplicate-rows\/\">delete the duplicate rows<\/a>. Consider the following example.<\/p>\n\n\n\n<p>First, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create a table<\/a> with some duplicate values:<\/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\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> t (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    <span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">10<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>\n);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> t(<span class=\"hljs-keyword\">name<\/span>) \n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'A'<\/span>),\n      (<span class=\"hljs-string\">'B'<\/span>),\n      (<span class=\"hljs-string\">'B'<\/span>),\n      (<span class=\"hljs-string\">'C'<\/span>),\n      (<span class=\"hljs-string\">'C'<\/span>),\n      (<span class=\"hljs-string\">'C'<\/span>),\n      (<span class=\"hljs-string\">'D'<\/span>);\n<span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> t;<\/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>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----+------+\r\n| id | name |\r\n+----+------+\r\n|  <span class=\"hljs-number\">1<\/span> | A    |\r\n|  <span class=\"hljs-number\">2<\/span> | B    |\r\n|  <span class=\"hljs-number\">3<\/span> | B    |\r\n|  <span class=\"hljs-number\">4<\/span> | C    |\r\n|  <span class=\"hljs-number\">5<\/span> | C    |\r\n|  <span class=\"hljs-number\">6<\/span> | C    |\r\n|  <span class=\"hljs-number\">7<\/span> | D    |\r\n+----+------+\r\n<span class=\"hljs-number\">7<\/span> rows <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, use the <code>ROW_NUMBER()<\/code> function\u00a0to divide the rows into partitions by all columns. The row number will restart for each unique set of rows.<\/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  <span class=\"hljs-keyword\">id<\/span>, \n  <span class=\"hljs-keyword\">name<\/span>, \n  ROW_NUMBER() <span class=\"hljs-keyword\">OVER<\/span> (\n    <span class=\"hljs-keyword\">PARTITION<\/span> <span class=\"hljs-keyword\">BY<\/span> <span class=\"hljs-keyword\">name<\/span>\n    <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> <span class=\"hljs-keyword\">id<\/span>\n  ) <span class=\"hljs-keyword\">AS<\/span> row_num \n<span class=\"hljs-keyword\">FROM<\/span> \n  t;<\/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>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----+------+---------+\r\n| id | name | row_num |\r\n+----+------+---------+\r\n|  <span class=\"hljs-number\">1<\/span> | A    |       <span class=\"hljs-number\">1<\/span> |\r\n|  <span class=\"hljs-number\">2<\/span> | B    |       <span class=\"hljs-number\">1<\/span> |\r\n|  <span class=\"hljs-number\">3<\/span> | B    |       <span class=\"hljs-number\">2<\/span> |\r\n|  <span class=\"hljs-number\">4<\/span> | C    |       <span class=\"hljs-number\">1<\/span> |\r\n|  <span class=\"hljs-number\">5<\/span> | C    |       <span class=\"hljs-number\">2<\/span> |\r\n|  <span class=\"hljs-number\">6<\/span> | C    |       <span class=\"hljs-number\">3<\/span> |\r\n|  <span class=\"hljs-number\">7<\/span> | D    |       <span class=\"hljs-number\">1<\/span> |\r\n+----+------+---------+\r\n<span class=\"hljs-number\">7<\/span> rows <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output indicates that the unique rows are the ones with the row number 1.<\/p>\n\n\n\n<p>Third, you can use the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-cte\/\">common table expression<\/a> (CTE) to return the duplicate rows and the <code>DELETE<\/code> statement to remove them:<\/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\">WITH<\/span> dups <span class=\"hljs-keyword\">AS<\/span> (\n  <span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">id<\/span>, \n    <span class=\"hljs-keyword\">name<\/span>, \n    ROW_NUMBER() <span class=\"hljs-keyword\">OVER<\/span>(\n      <span class=\"hljs-keyword\">PARTITION<\/span> <span class=\"hljs-keyword\">BY<\/span> <span class=\"hljs-keyword\">name<\/span> \n      <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> <span class=\"hljs-keyword\">id<\/span>\n    ) <span class=\"hljs-keyword\">AS<\/span> row_num \n  <span class=\"hljs-keyword\">FROM<\/span> \n    t\n) \n<span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">FROM<\/span> \n  t <span class=\"hljs-keyword\">USING<\/span> t \n  <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> dups <span class=\"hljs-keyword\">ON<\/span> t.id = dups.id \n<span class=\"hljs-keyword\">WHERE<\/span> \n  dups.row_num &gt; <span class=\"hljs-number\">1<\/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>Notice that MySQL does not support CTE-based delete. Therefore, you have to <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-delete-join\/\">join<\/a> the original table with the CTE as a workaround.<\/p>\n\n\n\n<p>Finally, select data from the t table to check for duplicates:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"> select * <span class=\"hljs-keyword\">from<\/span> t;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/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-12\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----+------+\r\n| id | name |\r\n+----+------+\r\n|  <span class=\"hljs-number\">1<\/span> | A    |\r\n|  <span class=\"hljs-number\">2<\/span> | B    |\r\n|  <span class=\"hljs-number\">4<\/span> | C    |\r\n|  <span class=\"hljs-number\">7<\/span> | D    |\r\n+----+------+\r\n<span class=\"hljs-number\">4<\/span> rows <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">4) Pagination using\u00a0the ROW_NUMBER() function<\/h3>\n\n\n\n<p>Because the <code>ROW_NUMBER()<\/code> assigns each row in the result set a unique number, you can use it for pagination.<\/p>\n\n\n\n<p>Suppose, you need to display a list of products with 10 products per page. To get the products for the second page, you use the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" 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  * \n<span class=\"hljs-keyword\">FROM<\/span> \n  (\n    <span class=\"hljs-keyword\">SELECT<\/span> \n      productName, \n      msrp, \n      row_number() <span class=\"hljs-keyword\">OVER<\/span> (\n        <span class=\"hljs-keyword\">order<\/span> <span class=\"hljs-keyword\">by<\/span> \n          msrp\n      ) <span class=\"hljs-keyword\">AS<\/span> row_num \n    <span class=\"hljs-keyword\">FROM<\/span> \n      products\n  ) t \n<span class=\"hljs-keyword\">WHERE<\/span> \n  row_num <span class=\"hljs-keyword\">BETWEEN<\/span> <span class=\"hljs-number\">11<\/span> <span class=\"hljs-keyword\">AND<\/span> <span class=\"hljs-number\">20<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><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>Here&#8217;s the output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">+------------------------------------------+-------+---------+\n| productName                              | msrp  | row_num |\n+------------------------------------------+-------+---------+\n| 1936 Mercedes-Benz 500K Special Roadster | 53.91 |      11 |\n| 1954 Greyhound Scenicruiser              | 54.11 |      12 |\n| Pont Yacht                               | 54.60 |      13 |\n| 1970 Dodge Coronet                       | 57.80 |      14 |\n| 1962 City of Detroit Streetcar           | 58.58 |      15 |\n| 1911 Ford Town Car                       | 60.54 |      16 |\n| 1936 Harley Davidson El Knucklehead      | 60.57 |      17 |\n| 1926 Ford Fire Engine                    | 60.77 |      18 |\n| 1971 Alpine Renault 1600s                | 61.23 |      19 |\n| 1950's Chicago Surface Lines Streetcar   | 62.14 |      20 |\n+------------------------------------------+-------+---------+\n10 rows in set (0.01 sec)<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the MySQL <code>ROW_NUMBER()<\/code> function to generate a sequential number for each row in a result set.<\/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=\"6932\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/\"\n\t\t\t\tdata-post-title=\"MySQL ROW_NUMBER Function\"\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=\"6932\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/\"\n\t\t\t\tdata-post-title=\"MySQL ROW_NUMBER Function\"\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>In this tutorial, you will learn about the MySQL ROW_NUMBER() function and how to use it to generate a unique number for each row in the result set.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":6941,"menu_order":10,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6932","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 ROW_NUMBER and Its Useful Applications<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn about the MySQL ROW_NUMBER() function and how to use it to generate a unique number for each row in the result set.\" \/>\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-window-functions\/mysql-row_number-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL ROW_NUMBER and Its Useful Applications\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn about the MySQL ROW_NUMBER() function and how to use it to generate a unique number for each row in the result set.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-04T14:12:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/products_table.png\" \/>\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.mysqltutorial.org\\\/mysql-window-functions\\\/mysql-row_number-function\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/mysql-row_number-function\\\/\",\"name\":\"MySQL ROW_NUMBER and Its Useful Applications\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/mysql-row_number-function\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/mysql-row_number-function\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/05\\\/products_table.png\",\"datePublished\":\"2018-08-21T14:33:34+00:00\",\"dateModified\":\"2024-01-04T14:12:14+00:00\",\"description\":\"In this tutorial, you will learn about the MySQL ROW_NUMBER() function and how to use it to generate a unique number for each row in the result set.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/mysql-row_number-function\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/mysql-row_number-function\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/mysql-row_number-function\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/05\\\/products_table.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/05\\\/products_table.png\",\"width\":181,\"height\":231,\"caption\":\"products table\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/mysql-row_number-function\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Window Functions\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL ROW_NUMBER Function\"}]},{\"@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 ROW_NUMBER and Its Useful Applications","description":"In this tutorial, you will learn about the MySQL ROW_NUMBER() function and how to use it to generate a unique number for each row in the result set.","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-window-functions\/mysql-row_number-function\/","og_locale":"en_US","og_type":"article","og_title":"MySQL ROW_NUMBER and Its Useful Applications","og_description":"In this tutorial, you will learn about the MySQL ROW_NUMBER() function and how to use it to generate a unique number for each row in the result set.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/","og_site_name":"MySQL Tutorial","article_modified_time":"2024-01-04T14:12:14+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/products_table.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/","url":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/","name":"MySQL ROW_NUMBER and Its Useful Applications","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/products_table.png","datePublished":"2018-08-21T14:33:34+00:00","dateModified":"2024-01-04T14:12:14+00:00","description":"In this tutorial, you will learn about the MySQL ROW_NUMBER() function and how to use it to generate a unique number for each row in the result set.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/products_table.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/products_table.png","width":181,"height":231,"caption":"products table"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Window Functions","item":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/"},{"@type":"ListItem","position":3,"name":"MySQL ROW_NUMBER Function"}]},{"@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\/6932","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=6932"}],"version-history":[{"count":4,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/6932\/revisions"}],"predecessor-version":[{"id":14198,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/6932\/revisions\/14198"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/6941"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=6932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}