{"id":6941,"date":"2018-08-23T07:55:40","date_gmt":"2018-08-23T14:55:40","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=6941"},"modified":"2023-10-19T23:24:11","modified_gmt":"2023-10-20T06:24:11","slug":"mysql-window-functions","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/","title":{"rendered":"MySQL Window Functions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about MySQL window functions and their practical applications for solving analytical query challenges.<\/p>\n\n\n\n<p>MySQL has supported window functions since version 8.0, allowing you to solve query problems more easily and with better performance.<\/p>\n\n\n\n<p>Suppose that we have the <code>sales<\/code> table which stores the sales by employees and fiscal years:<\/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\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> sales(\n    sales_employee <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">50<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    fiscal_year <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    sale <span class=\"hljs-built_in\">DECIMAL<\/span>(<span class=\"hljs-number\">14<\/span>,<span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    PRIMARY <span class=\"hljs-keyword\">KEY<\/span>(sales_employee,fiscal_year)\n);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> sales(sales_employee,fiscal_year,sale)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'Bob'<\/span>,<span class=\"hljs-number\">2016<\/span>,<span class=\"hljs-number\">100<\/span>),\n      (<span class=\"hljs-string\">'Bob'<\/span>,<span class=\"hljs-number\">2017<\/span>,<span class=\"hljs-number\">150<\/span>),\n      (<span class=\"hljs-string\">'Bob'<\/span>,<span class=\"hljs-number\">2018<\/span>,<span class=\"hljs-number\">200<\/span>),\n      (<span class=\"hljs-string\">'Alice'<\/span>,<span class=\"hljs-number\">2016<\/span>,<span class=\"hljs-number\">150<\/span>),\n      (<span class=\"hljs-string\">'Alice'<\/span>,<span class=\"hljs-number\">2017<\/span>,<span class=\"hljs-number\">100<\/span>),\n      (<span class=\"hljs-string\">'Alice'<\/span>,<span class=\"hljs-number\">2018<\/span>,<span class=\"hljs-number\">200<\/span>),\n       (<span class=\"hljs-string\">'John'<\/span>,<span class=\"hljs-number\">2016<\/span>,<span class=\"hljs-number\">200<\/span>),\n      (<span class=\"hljs-string\">'John'<\/span>,<span class=\"hljs-number\">2017<\/span>,<span class=\"hljs-number\">150<\/span>),\n      (<span class=\"hljs-string\">'John'<\/span>,<span class=\"hljs-number\">2018<\/span>,<span class=\"hljs-number\">250<\/span>);\n\n<span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> sales;\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<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"229\" height=\"194\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-Window-Function-Sample-Table.png\" alt=\"\" class=\"wp-image-6943\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-Window-Function-Sample-Table.png 229w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-Window-Function-Sample-Table-200x169.png 200w\" sizes=\"auto, (max-width: 229px) 100vw, 229px\" \/><\/figure>\n\n\n\n<p>It&#8217;s probably easier to understand window functions is to start with <a href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/\">aggregate functions<\/a>.<\/p>\n\n\n\n<p>Aggregate functions summarize data from multiple rows into a single result row. For example, the following <a href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/\"><code>SUM()<\/code><\/a>function returns the total sales of all employees in the recorded years:<\/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    <span class=\"hljs-keyword\">SUM<\/span>(sale)\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales;<\/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<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"88\" height=\"37\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-Window-Function-vs-Aggregate-SUM.png\" alt=\"MySQL Window Function - vs Aggregate SUM\" class=\"wp-image-6944\"\/><\/figure>\n\n\n\n<p>The <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-group-by\/\">GROUP BY<\/a><\/code> clause allows you to apply aggregate functions to a subset of rows. For example, you may want to calculate the total sales by fiscal years:<\/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    fiscal_year, \n    <span class=\"hljs-keyword\">SUM<\/span>(sale)\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> \n    fiscal_year;<\/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<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"152\" height=\"79\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-Window-Function-vs-SUM-with-GROUP-BY.png\" alt=\"MySQL Window Function - vs SUM with GROUP BY\" class=\"wp-image-6945\" title=\"MySQL Window Function - vs SUM with GROUP BY\"\/><\/figure>\n\n\n\n<p>In both examples, the aggregate functions reduce the number of rows returned by the query.<\/p>\n\n\n\n<p>Like the aggregate functions with the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-group-by\/\">GROUP BY<\/a><\/code> clause, window functions also operate on a subset of rows but they do not reduce the number of rows returned by the query.<\/p>\n\n\n\n<p>For example, the following query returns the sales for each employee, along with the total sales of the employees by fiscal year:<\/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    fiscal_year, \n    sales_employee,\n    sale,\n    <span class=\"hljs-keyword\">SUM<\/span>(sale) <span class=\"hljs-keyword\">OVER<\/span> (<span class=\"hljs-keyword\">PARTITION<\/span> <span class=\"hljs-keyword\">BY<\/span> fiscal_year) total_sales\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales;<\/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<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"299\" height=\"200\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-Window-Function-SUM-window-function.png\" alt=\"MySQL Window Function - SUM window function\" class=\"wp-image-6946\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-Window-Function-SUM-window-function.png 299w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/08\/MySQL-Window-Function-SUM-window-function-200x134.png 200w\" sizes=\"auto, (max-width: 299px) 100vw, 299px\" \/><\/figure>\n\n\n\n<p>In this example, the <code>SUM()<\/code> function works as a window function that operates on a set of rows defined by the contents of the <code>OVER<\/code> clause. A set of rows to which the <code>SUM()<\/code> function applies is referred to as a window.<\/p>\n\n\n\n<p>The <code>SUM()<\/code> window function reports not only the total sales by fiscal year as it does in the query with the <code>GROUP BY<\/code> clause, but also the result in each row, rather than the total number of rows returned.<\/p>\n\n\n\n<p>Note that window functions are performed on the result set after all <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-join\/\">JOIN<\/a><\/code>, <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-where\/\">WHERE<\/a><\/code>, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-group-by\/\"><code>GROUP BY<\/code><\/a>, and <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-having\/\">HAVING<\/a><\/code> clauses and before the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/\">ORDER BY<\/a><\/code>, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-limit\/\"><code>LIMIT<\/code><\/a> and <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-distinct\/\">SELECT DISTINCT<\/a><\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Window function syntax<\/h2>\n\n\n\n<p>The general syntax of calling a window function is as follows:<\/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\">window_function_name(expression) OVER ( \n   &#91;partition_defintion]\n   &#91;order_definition]\n   &#91;frame_definition]\n)<\/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 syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the window function name followed by an expression.<\/li>\n\n\n\n<li>Second, specify the <code>OVER<\/code> clause which has three possible elements: partition definition, order definition,&nbsp;and frame definition.<\/li>\n<\/ul>\n\n\n\n<p>The opening and closing parentheses, which appear after the <code>OVER<\/code> clause, is mandatory, even with no expression, for example:<\/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\">window_function_name(expression) OVER()<\/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<h3 class=\"wp-block-heading\">partition_clause\u00a0syntax<\/h3>\n\n\n\n<p>The <code>partition_clause<\/code> breaks up the rows into chunks or partitions. Two partitions are separated by a partition boundary.<\/p>\n\n\n\n<p>The window function is performed within partitions and re-initialized when crossing the partition boundary.<\/p>\n\n\n\n<p>The <code>partition_clause<\/code> syntax looks like the following:<\/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\">PARTITION BY &lt;expression&gt;&#91;{,&lt;expression&gt;...}]<\/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>You can specify one or more expressions in the <code>PARTITION BY<\/code> clause. Multiple expressions are separated by commas.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">order_by_clause syntax<\/h3>\n\n\n\n<p>The <code>order_by_clause<\/code> has the following syntax:<\/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\">ORDER BY &lt;expression&gt; &#91;ASC|DESC], &#91;{,&lt;expression&gt;...}]<\/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>The <code>ORDER BY<\/code> clause specifies how the rows are ordered within a partition. It is possible to order data within a partition on multiple keys, each key is specified by an expression. Multiple expressions are also separated by commas.<\/p>\n\n\n\n<p>Similar to the <code>PARTITION BY<\/code> clause, the <code>ORDER BY<\/code> clause is also supported by all the window functions. However, it only makes sense to use the <code>ORDER BY<\/code> clause for order-sensitive window functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">frame_clause syntax<\/h3>\n\n\n\n<p>A frame is a subset of the current partition. To define the subset, you use the frame clause as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">frame_unit {&lt;frame_start&gt;|&lt;frame_between&gt;}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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 frame is defined with respect to the current row, which allows a frame to move within a partition depending on the position of the current row within its partition.<\/p>\n\n\n\n<p>The frame unit specifies the type of relationship between the current row and frame rows. It can be <code>ROWS<\/code> or <code>RANGE<\/code>. The offsets of the current row and frame rows are the row numbers if the frame unit is <code>ROWS<\/code> and row values the frame unit is <code>RANGE<\/code>.<\/p>\n\n\n\n<p>The <code>frame_start<\/code> and <code>frame_between<\/code> define the frame boundary.<\/p>\n\n\n\n<p>The <code>frame_start<\/code> contains one of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>UNBOUNDED PRECEDING<\/code>: frame starts at the first row of the partition.<\/li>\n\n\n\n<li><code>N PRECEDING<\/code>: a physical N of rows before the first current row. N can be a literal number or an expression that evaluates to a number.<\/li>\n\n\n\n<li><code>CURRENT ROW<\/code>: the row of the current calculation<\/li>\n<\/ul>\n\n\n\n<p>The <code>frame_between<\/code> is as follows:<\/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\">BETWEEN frame_boundary_1 AND frame_boundary_2   <\/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>The <code>frame_boundary_1<\/code> and <code>frame_boundary_2<\/code> can each contain one of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>frame_start<\/code>: as mentioned previously.<\/li>\n\n\n\n<li><code>UNBOUNDED FOLLOWING<\/code>: the frame ends at the final row in the partition.<\/li>\n\n\n\n<li><code>N FOLLOWING<\/code>: a physical N of rows after the current row.<\/li>\n<\/ul>\n\n\n\n<p>If you don&#8217;t specify the <code>frame_definition<\/code> in the <code>OVER<\/code> clause, then MySQL uses the following frame by default:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><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<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"613\" height=\"439\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/09\/mysql-window-functions-frame-clause-bound.png\" alt=\"mysql window functions - frame clause bound\" class=\"wp-image-7410\" title=\"mysql window functions - frame clause bound\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/09\/mysql-window-functions-frame-clause-bound.png 613w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/09\/mysql-window-functions-frame-clause-bound-200x143.png 200w\" sizes=\"auto, (max-width: 613px) 100vw, 613px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL Window Function list<\/h2>\n\n\n\n<p>The following table shows the window functions in MySQL:<br>\n<input id=\"search\" class=\"light-table-filter\" type=\"text\" placeholder=\"Type a function name to search...\" data-table=\"functions\"><\/p>\n\n\n\n<figure class=\"wp-block-table responsive functions\"><table><thead><tr><th>Name<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><a href=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-cume_dist-function\/\">CUME_DIST<\/a><\/td><td>Calculates the cumulative distribution of a value in a set of values.<\/td><\/tr><tr><td><a href=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-dense_rank-function\/\">DENSE_RANK<\/a><\/td><td>Assigns a rank to every row within its partition based on the <code>ORDER BY<\/code> clause. It assigns the same rank to the rows with equal values. If two or more rows have the same rank, then there will be no gaps in the sequence of ranked values.<\/td><\/tr><tr><td><a href=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-first_value-function\/\">FIRST_VALUE<\/a><\/td><td>Returns the value of the specified expression with respect to the first row in the window frame.<\/td><\/tr><tr><td><a href=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-lag-function\/\">LAG<\/a><\/td><td>Returns the value of the Nth row before the current row in a partition.&nbsp;It returns NULL if no preceding row exists.<\/td><\/tr><tr><td><a href=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-last_value-function\/\">LAST_VALUE<\/a><\/td><td>Returns the value of the specified expression with respect to the last row in the window frame.<\/td><\/tr><tr><td><a href=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-lead-function\/\">LEAD<\/a><\/td><td>Returns the value of the Nth row after the current row in a partition.&nbsp;It returns NULL if no subsequent row exists.<\/td><\/tr><tr><td><a href=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-nth_value-function\/\">NTH_VALUE<\/a><\/td><td>Returns value of argument from Nth row of the window frame<\/td><\/tr><tr><td><a href=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-ntile-function\/\">NTILE<\/a><\/td><td>Distributes the rows for each window partition into a specified number of ranked groups.<\/td><\/tr><tr><td><a href=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-percent_rank-function\/\">PERCENT_RANK<\/a><\/td><td>Calculates the percentile rank of a row in a partition or result set<\/td><\/tr><tr><td><a href=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-rank-function\/\">RANK<\/a><\/td><td>Similar to the <code>DENSE_RANK()<\/code> function except that there are gaps in the sequence of ranked values when two or more rows have the same rank.<\/td><\/tr><tr><td><a href=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/mysql-row_number-function\/\">ROW_NUMBER<\/a><\/td><td>Assigns a sequential integer to every row within its partition<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>In this tutorial, you have learned about the MySQL window functions and their syntax. In the next tutorials, you will learn more about each window function and its applications in more detail.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial provides you with some practical and motivational examples of the window functions.<\/p>\n","protected":false},"author":2,"featured_media":10069,"parent":0,"menu_order":20,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6941","page","type-page","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL Window Functions<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn about MySQL window functions and their practical applications for solving analytical query challenges.\" \/>\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\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL Window Functions\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn about MySQL window functions and their practical applications for solving analytical query challenges.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-20T06:24:11+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 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\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/\",\"name\":\"MySQL Window Functions\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/mysql-window-functions.svg\",\"datePublished\":\"2018-08-23T14:55:40+00:00\",\"dateModified\":\"2023-10-20T06:24:11+00:00\",\"description\":\"In this tutorial, you will learn about MySQL window functions and their practical applications for solving analytical query challenges.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/mysql-window-functions.svg\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/mysql-window-functions.svg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-window-functions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Window Functions\"}]},{\"@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 Window Functions","description":"In this tutorial, you will learn about MySQL window functions and their practical applications for solving analytical query challenges.","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\/","og_locale":"en_US","og_type":"article","og_title":"MySQL Window Functions","og_description":"In this tutorial, you will learn about MySQL window functions and their practical applications for solving analytical query challenges.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-10-20T06:24:11+00:00","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/","url":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/","name":"MySQL Window Functions","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2021\/04\/mysql-window-functions.svg","datePublished":"2018-08-23T14:55:40+00:00","dateModified":"2023-10-20T06:24:11+00:00","description":"In this tutorial, you will learn about MySQL window functions and their practical applications for solving analytical query challenges.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-window-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2021\/04\/mysql-window-functions.svg","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2021\/04\/mysql-window-functions.svg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-window-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Window Functions"}]},{"@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\/6941","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=6941"}],"version-history":[{"count":3,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/6941\/revisions"}],"predecessor-version":[{"id":11472,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/6941\/revisions\/11472"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media\/10069"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=6941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}