EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials SQL Tutorial SQL Window Functions

SQL Window Functions

By Ravi KhatriRavi Khatri

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.

There are different classes of window functions:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • 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
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle1500+ Hour of HD Videos | 80 Learning Paths | 360+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more