ROW_NUMBER(): SELECT ROW_NUMBER() OVER(ORDER BY score DESC) AS rnk FROM exams; : Assign unique sequential number to each row in window order

RANK(): SELECT RANK() OVER(PARTITION BY dept ORDER BY salary DESC) r FROM emp; : Assign rank with gaps for ties within partition

DENSE_RANK(): SELECT DENSE_RANK() OVER(PARTITION BY dept ORDER BY salary DESC) dr FROM emp; : Assign consecutive ranks without gaps

NTILE(): SELECT NTILE(4) OVER(ORDER BY salary) quartile FROM emp; : Distribute rows into equal buckets

LAG(): SELECT year,sales,LAG(sales) OVER(ORDER BY year) prev FROM rev; : Access value from preceding row

LEAD(): SELECT year,sales,LEAD(sales) OVER(ORDER BY year) next FROM rev; : Access value from following row

FIRST_VALUE(): SELECT FIRST_VALUE(price) OVER(ORDER BY dt) AS first_price FROM btc; : Return first value in window frame

LAST_VALUE(): SELECT LAST_VALUE(price) OVER(ORDER BY dt RANGE BETWEEN UNBOUNDED FOLLOWING AND UNBOUNDED FOLLOWING) last_price FROM btc; : Return last value in frame

NTH_VALUE(): SELECT NTH_VALUE(sales,3) OVER(ORDER BY year) AS third_year FROM rev; Return nth
value in frame

CUME_DIST(): SELECT CUME_DIST() OVER(ORDER BY score) cd FROM exams; : Calculate cumulative distribution

PERCENT_RANK(): SELECT PERCENT_RANK() OVER(ORDER BY score) pr FROM exams; : Compute relative rank of row

ROWS BETWEEN: SUM(sales) OVER(PARTITION BY prod ORDER BY dt ROWS BETWEEN 3 PRECEDING AND CURRENT ROW); : Define moving window frame for aggregation

RANGE BETWEEN: AVG(price) OVER(ORDER BY dt RANGE BETWEEN INTERVAL 7 DAY PRECEDING AND CURRENT ROW); : Use value based bounds for window

EXCLUDE CURRENT ROW: AVG(score) OVER(ORDER BY score DESC EXCLUDE CURRENT ROW); : Remove current row from frame calculation

WINDOW NAMED clause: SELECT SUM(v) OVER w FROM t WINDOW w AS (PARTITION BY c); : Reuse window specification across functions
Previous Next