EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial SQL Window Functions
 

SQL Window Functions

Ravi Khatri
Article byRavi Khatri
EDUCBA
Reviewed byRavi Rathore

Updated March 13, 2023

SQL Window Functions

 

 

Introduction to SQL Window Functions

The following article provides an outline for SQL Window Functions. A window function gets out an estimation over a lot of table rows that are by one way or another identified with the present line. This is somehow related to aggregate functions which we are used to but unlike aggregate functions, window functions do not group the result into single row but each row have its separate identity. So,more than just the current row of the query result is accessed by the window function. This provide extremely powerful and useful features, but since they are different from the standard SQL, they are difficult to grasp and have strange syntax and are very often avoided.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

There are different classes of window functions:

  • Aggregate functions: COUNT, AVG, SUM, MAX, MIN, etc.
  • Ranking functions: RANK, ROW_NUMBER, DENSE_RANK etc.
  • Analytic functions: FIRST_VALUE, LAST_VALUE, LEAD, LAG etc.

The partitioning and order of rows is defined by OVER clause in a window and so they are called window function and following arguments are used in this clause:

  • ORDER BY: It defines the logical order of the rows.
  • PARTITION BY: It divides the result into separate partitions and the window function is applied individually to every partition.
  • ROWS or RANGE clause: It limits the rows within the partition by specifying start and end points within the partition.

If ROWS or RANGE clause is not specified then the default value is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

Examples of SQL Window Functions

Given below are the examples mentioned:

Example #1

Simple example with AVG() function.

Code:

SELECT emp_name, emp_gender, emp_salary,
AVG(emp_salary) OVER (ORDER BY emp_salary) AS CalAverage
from Employee;

Output:

SQL Window Functions 1

Explanation:

  • This is the most basic example in which we select name, gender and salary from Employee table and used AVG function to get the salary of employee. As you can see average of Mark is 1000 and that of John is 1500. Since we have not used ROWS and RANGE clause all the rows before the current row are considered in the average.
  • In other words, the average is taken between unbounded preceding and the current row. Similarly, all other values are calculated. Notice the last row has the true average because it calculates all the rows above it.

Example #2

Using ROWS or RANGE clause.

Code:

SELECT emp_name, emp_gender, emp_salary,
AVG(emp_salary)OVER (ORDER BY emp_salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS CalAverage,
SUM(emp_salary)OVER (ORDER BY emp_salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS CalSum,
COUNT(emp_salary)OVER (ORDER BY emp_salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS CalCount
from Employee;

Output:

SQL Window Functions 2

Explanation:

  • In this example we select name, gender and salary from Employee table and used AVG, SUM, COUNT function to do some calculation and as you can see the aggregate functions are working on the entire result set which gives us same values and there are two reason for this.
  • First is that we are using ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING which gives us values of entire result and second is we are not using PARTITION so the entire result is considered one partition and so we are getting same result.

Example #3

Using PARTITION clause.

Code:

SELECT emp_name, emp_gender, emp_salary,
AVG (emp_salary) OVER (PARTITION BY emp_gender ORDER BY emp_salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS CalAverage,
SUM (emp_salary) OVER (PARTITION BY emp_gender ORDER BY emp_salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS CalSum,
COUNT (emp_salary) OVER (PARTITION BY emp_gender ORDER BY emp_salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS CalCount
from Employee;

Output:

SQL Window Functions 3

Explanation:

  • This is same as above example, the only difference is that in this we have used PARTITION BY clause to partition the data by gender, so the windows functions are applied within the partition and the average, sum and count is calculated accordingly.

Example #4

One preceding row and one following row.

Code:

SELECT emp_name, emp_gender, emp_salary,
AVG (emp_salary) OVER (ORDER BY emp_salary ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS CalAverage,
SUM (emp_salary) OVER (ORDER BY emp_salary ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS CalSum,
COUNT (emp_salary) OVER (ORDER BY emp_salary ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS CalCount from Employee;

Output:

One preceding row and one following row

Explanation:

  • In this query example we have calculated the salary of current row, one row preceding and one row following. So, the average of Mark is 1500 as it has no preceding row but only following row.
  • Similarly, John average is same 2000 because it has one preceding row and one following row.

Example #5

Simple example with LEAD() function.

Code:

SELECT emp_name, emp_gender, emp_salary,
LEAD (emp_salary) OVER (ORDER BY emp_salary)as Lead
FROM Employee;

Output:

LEAD()

The following shows the common syntax of a LEAD() in SQL Server:

LEAD(return_value ,offset [,default])
OVER (
[PARTITION BY partition_expression, ... ]
ORDER BY sort_expression [ASC or DESC], ...
)

Explanation:

  • This is the most basic example in which we select name, gender and salary from Employee table and used LEAD function to get the salary of next person.
  • As you can see Ron get NULL because there is no next salary and also, we have not specified any default value and offset is 1 so we get the immediate previous value.

Example #6

A bit complex example with LEAD() function.

Code:

SELECT emp_name, emp_gender, emp_salary,
LEAD (emp_salary, 2,-1) OVER (PARTITION BY emp_gender ORDER BY emp_salary) AS Lead
FROM Employee;

Output:

bit complex example with LEAD()

Explanation:

  • In this example we select name, gender and salary from Employee table and used LEAD function to get the salary of next person in Lead column as done in previous example but also, we partition the data using gender column. So, the LEAD function works within the confines of the partition.
  • What we have done is we have set the offset to 2 and default value to -1. As we can see in the example for Mary and Jodi we cannot fetch the values of next rows which is leads 2 rows so we get the default value -1 and similarly in the Male partition we get the result.

Conclusion

Now you know what window functions are in SQL server and how it is used to calculate results from multiple rows without grouping.

Recommended Articles

We hope that this EDUCBA information on “SQL Window Functions” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Array in SQL
  2. SQL Bulk Insert
  3. Azure SQL Database
  4. SQL While Loop

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW