After learning SELECT, WHERE, and GROUP BY clauses, Window Functions would be the next major thing to learn. Window functions allow ranking of rows, creation of running aggregations, and comparison of a current row with its previous row without losing any information at all.
Interviews often assess such skills, so taking the Data Scientist Course in Delhi includes those topics immediately after learning SQL basics. Here’s what you should know.
What Is a Window Function?
A window function does calculations on rows in a certain group of rows known as a window, but then returns each individual row. This makes a difference from GROUP BY because GROUP BY reduces many rows to only one.
The basic pattern looks like this:
function_name() OVER (PARTITION BY column ORDER BY column)
- PARTITION BY splits the rows into groups. It is optional.
- ORDER BY sets the order of rows inside each group.
The first examples use an employees table with the columns name, department, and salary.
1. ROW_NUMBER, RANK, and DENSE_RANK
These three functions rank rows. They differ only in how they treat ties.
SELECT name, department, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS row_num,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rnk,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS dense_rnk
FROM employees;
Suppose two people in one department share the top salary:
ROW_NUMBER gives 1, 2, 3 and never repeats a number.
RANK gives 1, 1, 3 and skips a number after a tie.
DENSE_RANK gives 1, 1, 2 and skips nothing.
Top 3 Earners per Department
You cannot use a window function directly in WHERE, so wrap the query first:
SELECT *
FROM (
SELECT name, department, salary,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rnk
FROM employees
) t
WHERE rnk <= 3;
2. Aggregate Functions as Window Functions
SUM(), AVG(), and COUNT() also work with OVER. This lets you compare each row with its group.
SELECT name, department, salary,
AVG(salary) OVER (PARTITION BY department) AS dept_avg
FROM employees;
Each employee will be shown alongside his or her department’s average salary. Using GROUP BY, we’d require an additional join here.
Running Totals
Here is a monthly_sales table with month and revenue:
SELECT month, revenue,
SUM(revenue) OVER (ORDER BY month) AS running_total
FROM monthly_sales;
Every new row has its amount added to all previous rows' amounts. Calculating cumulative totals occurs frequently when preparing sales or financial statements.
3. LAG and LEAD
LAG reads a value from the previous row. LEAD reads from the next row.
SELECT month, revenue,
LAG(revenue) OVER (ORDER BY month) AS prev_month,
revenue - LAG(revenue) OVER (ORDER BY month) AS change
FROM monthly_sales;
It shows the change in revenue between months. For example, since there was no prior month in the first row, LAG will output NULL in this case.
Window Functions vs GROUP BY
- GROUP BY returns one row per group and hides the details.
- Window functions keep every row and add the calculation beside it.
When generating summary reports, GROUP BY comes in handy. However, in situations where the details and summaries have to be presented together, window functions are preferred.
Tips for Beginners
- Always add ORDER BY inside OVER when ranking or using LAG and LEAD.
- Use a subquery or a CTE when you need to filter on a window result.
- Test each query on a small table first, so you can check the output by eye.
- Practice with real datasets, such as sales, orders, or student scores.
Today, all popular database management systems include support for window functions, such as PostgreSQL, SQL Server, and MySQL 8 or higher. Earlier versions of MySQL lack this capability.
Conclusion
Window Functions transform complex reports into simple and concise queries. The process begins with understanding ROW_NUMBER, RANK, and DENSE_RANK. You can then progress to Running Totals and LAG/LEAD. With consistent practice, you'll soon master these concepts!
In case you wish to learn from mentors through actual project-based experience, you may consider joining a Data Science Training Course in Jaipur, where you will be able to develop your SQL skills along with others such as Python programming, Statistics, and Machine Learning.