From the course: Advanced SQL – Window Functions

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

PARTITION BY and ORDER BY

PARTITION BY and ORDER BY - SQL Tutorial

From the course: Advanced SQL – Window Functions

Start my 1-month free trial

PARTITION BY and ORDER BY

- [Instructor] The second filter we can place in front of our window is the partition by clause. To partition something, means to divide it into parts and this is exactly what partition by does. The partitioning expressions limit the function's visible window only to rows that share the same values as the current row. Let's bring back the simplified animal set. Partition by species limits the function visibility to animals of the same species. For Buddy's row, the window is limited to his and Abby's. And the same is true for Abby. For Pumpkin, the window is limited to cats. Only herself in this case, and similarly for Peanut. Let's bring back the Max name function and add partition by species. For the first row, the function won't see the entire set. It will only see cats and Felix is the winner. The following row happens to be a cat too, so the function picks Felix once again. Next, is Prince. Prince is a dog, so the…

Contents