🗄️ Database Design / Normalization
Intermediate
What are window functions in SQL?
Answer
Window functions perform calculations across a set of rows related to the current row (the "window"), without collapsing them into groups like GROUP BY. Syntax: function() OVER (PARTITION BY col ORDER BY col ROWS/RANGE ...). Common functions: ROW_NUMBER() — sequential row number in partition; RANK() / DENSE_RANK() — rank with/without gaps for ties; LAG(col, n) / LEAD(col, n) — access previous/next row values; SUM() OVER () — running total; NTILE(n) — divide rows into n buckets. Window functions are invaluable for analytics: calculating running totals, percentile rankings, and comparing rows to their peers.
Previous
What is a common table expression (CTE)?
Next
What is the difference between horizontal and vertical scaling of databases?
More Database Design / Normalization Questions
View all →- Intermediate What are database anomalies and how does normalization prevent them?
- Intermediate What is denormalization and when is it used?
- Intermediate What are isolation levels in database transactions?
- Intermediate What are dirty reads, non-repeatable reads, and phantom reads?
- Intermediate What is query optimization and what is a query execution plan?