DEV Community

Cover image for Window Functions in SQL
John Kyalo
John Kyalo

Posted on

Window Functions in SQL

Just another time nerds, to break down Window functions to you in the simplest form ever.
Simply window functions allow you to perform calculations across a set of rows(a window) while still retaining access to individual rows.
You can use these functions to perform running calculations.
They include:
RANK(), ROW_NUMBER(), DENSE_RANK(), LEAD(), LAG() and even other more aggregate functions.

For the ranking functions:
Row_number(): numbers all rows sequentially
Rank(): uses the same numeric values for rows which are a tie
then skips the next value
Dense_ rank(): uses the same numeric values for rows which are a
but does not allow any gap to the values.

Lead () allows access to rows after the current row while lag() allows access to rows before the current row.

Syntax:
RANK() OVER(PARTITION BY... ORDER BY... )

Partition by divides the result set into partitions
The ranking gets applied to each partition separately

This will help you get started with window functions
Anything and Everything Data

Top comments (1)

Collapse
 
martinbaun profile image
Martin Baun

Really nicely done, John.