๐Ÿ“Š We use cookies & analytics to improve your experience. Learn more
Latest

Data Analytics with SQL & Excel Module 4 ยท Lesson 11 of 15

0% Exit to course

Module 4 ยท Dates, windows and time Lesson 11 of 15

Window functions: rank without collapsing rows

Learning objective

A window function computes across a set of rows while keeping every row: rank jobs inside each city, or show a row next to its group total. GROUP BY cannot do this because it collapses the rows.

Worked example

ROW_NUMBER() OVER (PARTITION BY city ORDER BY salary DESC) numbers the jobs within each city, so a filter of rnk = 1 gives the best-paid job per city.

Your practice task

List the top two roles by salary for each city, using ROW_NUMBER in a subquery and filtering on the rank.

Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.

Key takeaway

A window function computes across a set of rows while keeping every row: rank jobs inside each city, or show a row next to its group total. GROUP BY cannot do this because it collapses the rows.

Create free account or sign in

Almost there

15 lessons left of 15

Finish every lesson to unlock the certificate. Each one ends in a practice task โ€” do them and the final project brief writes itself.

Next recommended course Interview DSA Crash Course