Sitemap
Suraj Gusain

Suraj Gusain

Articles about Data Science and Machine Learning Linkedin: https://www.linkedin.com/in/suraj020/

WINDOW FUNCTION IN SQL

3 min readJul 11, 2023
Press enter or click to view image in full size

In SQL, a window function is a type of function that performs a calculation across a set of rows that are related to the current row. It allows you to perform calculations over a specific window or subset of rows without grouping or aggregating the data to a single value. Window functions are typically used in conjunction with the OVER clause.

Here’s the basic syntax of a window function:

Get Suraj Gusain’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s break down the components of the syntax:

  • <window function>: This represents the specific window function you want to use, such as SUM, AVG, ROW_NUMBER, RANK, LEAD, LAG, etc. Each function has its own purpose and behavior.
  • OVER: This keyword indicates that you are using a window function.
  • PARTITION BY: This clause is optional. It allows you to divide the rows into groups or partitions based on one or more expressions. The window function will be applied independently to each partition.
  • ORDER BY: This clause specifies the order in which the rows should be processed within each partition. It determines the window frame.
  • <expression>: It represents an expression or column on which you want to perform the calculation.
  • ROWS <window frame>: This specifies the range or frame of rows used for the window function calculation. It determines the subset of rows within the partition

Here are a few examples of common window functions:

  1. ROW_NUMBER(): Assigns a unique sequential number to each row within the partition.
Press enter or click to view image in full size

RANK(): Assigns a rank to each row within the partition, with gaps in ranking for tied rows.

Press enter or click to view image in full size

SUM(): Calculates the sum of a column over the window frame

Press enter or click to view image in full size

LEAD() and LAG(): Accesses the value of a column from a subsequent or previous row within the partition

Press enter or click to view image in full size

The DENSE_RANK() function is another window function in SQL that assigns a rank to each row within the partition, similar to the RANK() function. However, unlike RANK(), DENSE_RANK() does not leave gaps in the ranking for tied rows. It provides a dense sequence of ranks without any gaps.

Press enter or click to view image in full size

Thank you for taking the time to read the article. I appreciate your attention and feedback.”

If you found this article helpful, please consider sharing it with others who might benefit from it. Your support in spreading the word is greatly appreciated.”

Connect me on Linkedin

Connect me on GitHub

Connect me on Kaggle

--

--

Suraj Gusain
Suraj Gusain

Written by Suraj Gusain

Articles about Data Science and Machine Learning Linkedin: https://www.linkedin.com/in/suraj020/