Lesson 2 of 14 · 24 min

PARTITION BY: a separate window for each group

By the end of this lesson you can use PARTITION BY to compute group-level values on every row, combine several partitions in one query, and explain the difference between PARTITION BY and ORDER BY inside OVER.

OVER () uses every row as one big window. Most real comparisons are narrower: a SAR 300 headphone is expensive for headphones and cheap for a laptop. PARTITION BY splits the rows into groups, and each row's window becomes the rows in the same group as this one. The calculation restarts for each group, and every row still comes back.

Included with a Formula Quest membership

This lesson, its 4 graded exercises and every other Members lesson in SQL Quest come with a Formula Quest membership that includes SQL (Single Quest with SQL selected, Quest Trio including SQL, or Quest Unlimited), or with SQL access from your organization.

Free to everyone: all of SQL Foundations and Retrieving and Filtering Data, the first lesson of every track, the playground, the dataset library and the SQL reference. See free lessons