MySQL Window Functions with Examples

MySQL Window functions are a powerful tool for data analysis and querying data from MySQL databases. They allow you to perform complex calculations on a set of data and return a result set with additional computed columns that are derived from the original data.

To explain window functions, let’s take a sample dataset of a table “orders” with columns “order_id”, “customer_id”, “order_date”, “order_amount”.

order_idcustomer_idorder_dateorder_amount
11012021-01-01100
21022021-01-02200
31032021-01-03300
41012021-01-04150
51022021-01-05250
61032021-01-06350
71012021-01-07200
81022021-01-08300
91032021-01-09400

Now let’s see some examples of MySQL window functions using this table:

  • RANK() – This function assigns a rank to each row based on a specific column’s value. In the below example, we are ranking the orders based on the order_amount.
SELECT order_id, customer_id, order_amount, RANK() OVER (ORDER BY order_amount DESC) as order_rank 
FROM orders;
order_idcustomer_idorder_amountorder_rank
91034001
61033502
51022503
81023003
31033003
71012006
21022006
41011508
11011009
  • ROW_NUMBER() – This function assigns a unique sequential number to each row within a result set.
SELECT order_id, customer_id, order_amount, ROW_NUMBER() OVER (ORDER BY order_date) as row_number 
FROM orders;
order_idcustomer_idorder_amountrow_number
11011001
21022002
31033003
41011504
51022505
61033506
71012007
81023008
  • DENSE_RANK() – This function assigns a rank to each row based on a specific column’s value but skips rank numbers for duplicate values.
SELECT order_id, customer_id, order_amount, DENSE_RANK() OVER (ORDER BY order_amount DESC) as order_rank 
FROM orders;
order_idcustomer_idorder_amountorder_rank
91034001
61033502
51022503
81023004
31033004
71012005
21022005
41011506
11011007
  • SUM() – This function calculates the sum of a column’s values within a window.
SELECT order_id, customer_id, order_amount, SUM(order_amount) OVER (ORDER BY order_date) as total_amount 
FROM orders;
order_idcustomer_idorder_amounttotal_amount
1101100100
2102200300
3103300600
4101150750
51022501000
61033501350
71012001550
81023001850
91034002250
  • LAG() – This function retrieves the value of a specific column from the previous row within a result set. In the below example, we are retrieving the order_amount from the previous row for each order.
SELECT order_id, customer_id, order_amount, LAG(order_amount, 1, 0) OVER (ORDER BY order_date) as previous_order_amount 
FROM orders;
order_idcustomer_idorder_amountprevious_order_amount
11011000
2102200100
3103300200
4101150300
5102250150
6103350250
7101200350
8102300200
9103400300
  • NTILE() – This function divides a result set into a specified number of groups and assigns a group number to each row. In the below example, we are dividing the orders into 3 groups based on the order_amount.
SELECT order_id, customer_id, order_amount, NTILE(3) OVER (ORDER BY order_amount) as order_group 
FROM orders;
order_idcustomer_idorder_amountorder_group
11011001
41011501
21022001
71012002
51022502
31033002
81023003
61033503