Window Functions
Perform calculations across sets of rows related to the current row without collapsing them - essential for ranking, running totals, and time-series analysis.
What Are Window Functions?
Window functions compute values across a set of rows (a "window") while keeping every individual row in the result. Unlike GROUP BY, they do not collapse rows.
function_name(args) OVER ( PARTITION BY column1, column2 -- optional: divide into groups ORDER BY column3 -- optional: define row order ROWS BETWEEN ... -- optional: frame specification )
Ranking Functions
SELECT name, category, price, ROW_NUMBER() OVER (ORDER BY price DESC) AS row_num, RANK() OVER (ORDER BY price DESC) AS rank, DENSE_RANK() OVER (ORDER BY price DESC) AS dense_rank FROM products;
| Function | Ties | Gaps | Example: prices 100, 90, 90, 80 |
|---|---|---|---|
ROW_NUMBER() | Breaks ties arbitrarily | No | 1, 2, 3, 4 |
RANK() | Same rank for ties | Yes | 1, 2, 2, 4 |
DENSE_RANK() | Same rank for ties | No | 1, 2, 2, 3 |
PARTITION BY
Rank within groups instead of the entire table:
-- Top 3 products per category by price SELECT * FROM ( SELECT name, category, price, ROW_NUMBER() OVER ( PARTITION BY category ORDER BY price DESC ) AS rn FROM products ) ranked WHERE rn <= 3;
LAG and LEAD
Access values from previous or next rows - perfect for time-series and period-over-period analysis:
SELECT order_date, revenue, LAG(revenue, 1) OVER (ORDER BY order_date) AS prev_day_revenue, revenue - LAG(revenue, 1) OVER (ORDER BY order_date) AS daily_change, LEAD(revenue, 1) OVER (ORDER BY order_date) AS next_day_revenue FROM daily_sales;
Running Totals and Moving Averages
SELECT order_date, revenue, -- Running total SUM(revenue) OVER ( ORDER BY order_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS running_total, -- 7-day moving average AVG(revenue) OVER ( ORDER BY order_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW ) AS moving_avg_7d FROM daily_sales;
NTILE
Divide rows into N equal buckets - useful for creating percentiles and quantiles:
SELECT name, revenue, NTILE(4) OVER (ORDER BY revenue DESC) AS quartile FROM customers;
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX