🐘 PostgreSQL
Intermediate
What are window functions in PostgreSQL?
Answer
Window functions perform calculations across a set of rows related to the current row (a "window") without collapsing rows like GROUP BY. Syntax: function() OVER (PARTITION BY col ORDER BY col ROWS/RANGE frame). Common functions: ROW_NUMBER() (unique row number per partition), RANK() (rank with gaps for ties), DENSE_RANK() (rank without gaps), NTILE(n) (divide into n buckets), LAG(col, n)/LEAD(col, n) (access previous/next rows), FIRST_VALUE(col)/LAST_VALUE(col), SUM/AVG/COUNT() OVER (...) (running totals). Example: SELECT name, salary, RANK() OVER (PARTITION BY dept ORDER BY salary DESC) as rank FROM employees;
Previous
What are CTEs (Common Table Expressions) in PostgreSQL?
Next
What are partial indexes in PostgreSQL?