Learnr

Learn SQL Window Functions

Rank, compare and total rows without collapsing them, using OVER and PARTITION BY.

Free to start. No credit card. Or browse all courses.

What's inside your SQL Window Functions course

A sample path Learnr builds for SQL Window Functions. Your real course adapts to your goal and level.

  1. GROUP BY vs window functions
  2. The OVER clause and PARTITION BY
  3. Ranking functions and top N per group
  4. LAG, LEAD and period-over-period changes
  5. Running totals, moving averages and frames
  6. FIRST_VALUE, LAST_VALUE, NTILE and performance

Window functions let a SQL query calculate across a set of related rows while still returning every row, which plain GROUP BY cannot do. With an OVER clause you can partition rows into groups, order them within each group and optionally define a frame of nearby rows. That unlocks rankings with ROW_NUMBER, RANK and DENSE_RANK, comparisons with the previous or next row through LAG and LEAD, running totals and moving averages with SUM and AVG, and percentiles with NTILE. They are a staple of analytics interviews and day-to-day reporting. Learnr builds a window functions course around your SQL level and database, in short query-focused lessons.

How to learn SQL Window Functions, step by step

  1. Contrast with GROUP BY on the same data. Write a query that totals sales per customer with GROUP BY, then one that adds the same total to every order row with SUM OVER PARTITION BY customer. Seeing rows kept instead of collapsed is the core idea.
  2. Solve top N per group. Number each customer orders by date with ROW_NUMBER, wrap the query in a CTE and keep rows where the number is 1 to get each first order. Then switch to RANK and DENSE_RANK and compare how ties are numbered.
  3. Calculate changes with LAG and LEAD. Compute month-over-month revenue change by subtracting LAG of revenue from the current value, ordered by month. Handle the first row, where LAG returns NULL, with a default argument or COALESCE.
  4. Control the window frame. Write a running total with SUM OVER ORDER BY date, then a seven-day moving average with ROWS BETWEEN 6 PRECEDING AND CURRENT ROW. Learn that the default frame with ORDER BY runs from the start of the partition to the current row.
  5. Learn the LAST_VALUE trap. Try LAST_VALUE with only an ORDER BY and notice it returns the current row, because of that default frame. Fix it with ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, a classic interview question.
  6. Drill interview-style problems. Practice streaks, gaps and islands, retention and percentile questions on real tables. Short daily Learnr exercises help you recall frame syntax and the order in which SQL evaluates clauses.

What you'll learn

  • Write OVER clauses with PARTITION BY and ORDER BY
  • Rank rows with ROW_NUMBER, RANK and DENSE_RANK and explain how ties differ
  • Compare rows across time with LAG and LEAD
  • Compute running totals and moving averages with explicit window frames
  • Filter on window results using subqueries, CTEs or QUALIFY where supported

How long does it take to learn SQL Window Functions?

If you already write joins and GROUP BY queries, the main window functions take about 1 to 3 weeks at a few hours a week. Solving harder problems such as gaps and islands fluently usually takes a month or two of regular practice.

What you need before you start

Solid basic SQL: SELECT, WHERE, joins, GROUP BY and CTEs. You need a database that supports window functions, which includes PostgreSQL, SQL Server, Oracle, SQLite, MySQL 8.0 and later, and cloud warehouses.

Common mistakes to avoid

  • Trying to filter on a window function in WHERE, which runs before windows are computed. Wrap the query in a CTE or subquery, or use QUALIFY where supported.
  • Using ROW_NUMBER when ties should share a rank, so equal values get arbitrary different positions that change between runs.
  • Forgetting ORDER BY inside OVER for running totals or LAG, which gives meaningless results because the row order within each partition is undefined.

How Learnr builds your course

Type your topic

Tell Learnr you want to learn SQL Window Functions, or get as specific as you like.

Get a personalized course

Learnr instantly builds a full SQL Window Functions course tailored to your goal and skill level.

Learn and level up

Move through bite-sized lessons and interactive practice, earning XP and keeping your streak.

SQL Window Functions questions, answered

What is the difference between RANK and DENSE_RANK?

Both give tied rows the same rank. RANK then skips numbers, so two rows tied for first are followed by third. DENSE_RANK does not skip, so the next row is second.

Can I use a window function in a WHERE clause?

No. Window functions are evaluated after WHERE, GROUP BY and HAVING. Compute the value in a subquery or CTE and filter outside it, or use QUALIFY in databases such as Snowflake, BigQuery and DuckDB.

Are window functions slow?

They can be on large tables because each partition must be sorted. Indexes that match the PARTITION BY and ORDER BY columns, and filtering rows early, usually keep them fast enough for reporting.

Explore more courses

More data and ai courses

See all data and ai courses

Ready to learn SQL Window Functions?

Start your free, personalized SQL Window Functions course in seconds. It will be typed in and ready when you get there.

Start learning SQL Window Functions

Free to start. No credit card.