Window Functions Flashcards

SQL window functions (48 cards)

1
Q

Visualize a rolling average 3 days

A

06/01/2022: 2 = 2.0
06/02/2022: 2 + 1 = 3 / 2 days = 1.5
06/03/2022: 2 + 1 + 3 = 6 / 3 days = 2.0
06/04/2022: 2 + 1 + 3 + 4 = 10 / 4 days = 2.5
06/05/2022: 2 + 1 + 3 + 4 + 5 = 15 / 5 days = 3.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does ROW_NUMBER() do?

A

Assigns a unique sequential number to each row within a partition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does RANK() do?

A

Assigns same rank to ties but skips numbers after ties

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does DENSE_RANK() do?

A

Assigns same rank to ties with no gaps/skips

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does LAG() do?

A

Returns previous row value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does LEAD() do?

A

Returns next row value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does SUM() OVER() do?

A

Calculates running total without collasping the rows like GROUP BY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does AVG() OVER() do?

A

Calculates moving average across rows

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does PARTITION BY do?

A

Splits data into groups without collapsing rows

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does ORDER BY do in window functions?

A

Defines row order for calculations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does ROWS BETWEEN mean?

A

Defines which rows are included in the window

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does UNBOUNDED PRECEDING mean?

A

Start from first row of partition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does CURRENT ROW mean?

A

Includes the current row as boundary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does UNBOUNDED FOLLOWING mean?

A

Includes all rows after current row

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does ROWS BETWEEN 2 PRECEDING AND CURRENT ROW mean?

A

Includes current row + 2 previous rows

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING mean?

A

Includes previous current and next row

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does ROWS mean?

A

Counts physical rows

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does RANGE mean?

A

Uses value-based range not row count

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How to get latest row per user?

A

Use ROW_NUMBER with PARTITION and filter rn=1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How to calculate MoM change?

A

Use LAG to compare current vs previous row

21
Q

How to get top N including ties?

A

Use DENSE_RANK and filter rank <= N

22
Q

How to compute running total?

A

Use SUM OVER with ORDER BY

23
Q

How to compute moving average?

A

Use AVG OVER with ROWS BETWEEN

24
Q

Visualize ROW_NUMBER() ?

A

ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY event_date DESC)

25
Visualize RANK() ?
RANK() OVER (ORDER BY revenue DESC)
26
Visualize DENSE_RANK() ?
DENSE_RANK() OVER (ORDER BY revenue DESC)
27
Visualize LAG() ?
LAG(revenue) OVER (ORDER BY month)
28
Visualize LEAD() ?
LEAD(payment_date) OVER (PARTITION BY user_id ORDER BY payment_date)
29
Visualize SUM() OVER() ?
SUM(amount) OVER (ORDER BY payment_date)
30
Visualize AVG() OVER() ?
AVG(mrr) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
31
Visualize PARTITION BY ?
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY event_date DESC)
32
Visualize ORDER BY in window functions?
LAG(revenue) OVER (ORDER BY month)
33
Visualize ROWS BETWEEN mean?
AVG(mrr) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
34
Visualize UNBOUNDED PRECEDING mean?
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
35
Visualize CURRENT ROW mean?
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
36
Visualize UNBOUNDED FOLLOWING mean?
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
37
Visualize ROWS BETWEEN 2 PRECEDING AND CURRENT ROW mean?
AVG(mrr) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
38
Visualize ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING mean?
AVG(value) OVER (ORDER BY date ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
39
Visualize ROWS mean?
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
40
Visualize RANGE mean?
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
41
Visualize How to get latest row per user?
SELECT * FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY event_date DESC) rn FROM events) t WHERE rn=1
42
Visualize How to calculate MoM change?
SELECT month, revenue, revenue - LAG(revenue) OVER (ORDER BY month) AS mom_change FROM revenue_table
43
Visualize How to get top N including ties?
SELECT * FROM (SELECT user_id, revenue, DENSE_RANK() OVER (ORDER BY revenue DESC) rnk FROM users) t WHERE rnk <= 3
44
Visualize How to compute running total?
SELECT date, SUM(revenue) OVER (ORDER BY date) AS cumulative_revenue FROM sales
45
Visualize How to compute moving average?
SELECT date, AVG(revenue) OVER (ORDER BY date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_avg FROM sales
46
Formula for determining a window frame ROWS BETWEEN
N-row window frame = ROWS BETWEEN N-1 PRECEDING AND CURRENT ROW 3-row → 2 PRECEDING 5-row → 4 PRECEDING 10-row → 9 PRECEDING
47
Functions that do not use window frames and only care about PARTITION BY and ORDER BY
ROW_NUMBER() RANK() DENSE_RANK() LAG() LEAD()
48
Functions that do you use WINDOW FRAMES
SUM() OVER AVG() OVER MIN() OVER MAX() OVER