Skip to content

SQL Window Functions Cheatsheet

Master SQL Window Functions Cheatsheet with 223 free flashcards. Study using spaced repetition and focus mode for effective learning in Databases.

🎓 223 cards ⏱️ ~112 min Advanced
Study Full Deck →
Share: 𝕏 Twitter LinkedIn WhatsApp

🎯 What You'll Learn

Preview Questions

12 shown

Running total of <i>amount</i> by <i>user_id</i> ordered by <i>ts</i>?

Show ▼

SUM(amount) OVER (PARTITION BY user_id ORDER BY ts ROWS UNBOUNDED PRECEDING)

Difference between ROW_NUMBER, RANK, DENSE_RANK?

Show ▼

ROW_NUMBER: unique sequential.
RANK: same value → same rank, leaves gaps.
DENSE_RANK: same value → same rank, no gaps.

Top N rows per group?

Show ▼

SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY group_id ORDER BY score DESC) rn
FROM t
) x WHERE rn <= 3;

Previous row's value in same partition?

Show ▼

LAG(price) OVER (PARTITION BY symbol ORDER BY ts)

Next row's value?

Show ▼

LEAD(price) OVER (PARTITION BY symbol ORDER BY ts)

Percentage change from previous row?

Show ▼

(price - LAG(price) OVER (ORDER BY ts)) * 100.0 / LAG(price) OVER (ORDER BY ts)

Cumulative average?

Show ▼

AVG(x) OVER (ORDER BY ts ROWS UNBOUNDED PRECEDING)

Moving 7-day average?

Show ▼

AVG(x) OVER (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)

Rank within partition by score?

Show ▼

RANK() OVER (PARTITION BY category ORDER BY score DESC)

First and last value per partition?

Show ▼

FIRST_VALUE(x) OVER (PARTITION BY g ORDER BY ts) and LAST_VALUE(x) OVER (PARTITION BY g ORDER BY ts ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)

Why does plain LAST_VALUE seem wrong?

Show ▼

Default frame is ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW — LAST_VALUE returns the current row! Override the frame.

Percentile rank?

Show ▼

PERCENT_RANK() OVER (ORDER BY score) — value 0..1, percentile within partition.

🎓 Start studying SQL Window Functions Cheatsheet

🎮 Study Modes Available

🔄

Flashcards

Flip to reveal

🧠

Focus Mode

Spaced repetition

Multiple Choice

Test your knowledge

⌨️

Type Answer

Active recall

📚

Learn Mode

Multi-round mastery

🎯

Match Game

Memory challenge

Related Topics in Databases

📖 Learning Resources