Mid-level Track Roadmap

SQL: window functions for BI

This course covers Postgres window functions through one running example: a learner percentile that this app once computed with a GROUP BY and HAVING query per learner. It explains how the OVER clause computes across rows without collapsing them, and how RANK, DENSE_RANK and ROW_NUMBER number ties. It shows how PERCENT_RANK gives every learner's percentile in one query. It also covers the default frame behind running totals, PARTITION BY for per-course rankings, and LAG for the time between events.

What you'll learn

  • ✓How OVER differs from GROUP BY and keeps every input row
  • ✓How RANK, DENSE_RANK and ROW_NUMBER handle tied rows
  • ✓Computing every learner's percentile with PERCENT_RANK over a CTE of totals
  • ✓How the default RANGE frame affects running totals, and when to write ROWS with a tiebreaker
  • ✓Per-group rankings with PARTITION BY and gaps between events with LAG
Free preview · Lesson 1 4 min read

One query per user, just to rank them

Enrollment::UserGamificationQuery computes a number this app shows each learner: leaderboard_percentile, how their XP stacks up against everyone else's. Until a fix shipped on the day this course was written, the code that computed it read:

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...