thinQmaster Virtual testing partner Adaptive practice exams that find your weak spots and drill them. Every answer explained. From $69
Enroll

thinQmaster

Find your exam

Five questions free, before you decide.

Step 1 · choose a vendor

In one of our camps? Your practice exams come with the tuition — they are on your study page already.

Data & Analytics · Intermediate

Top-N Query

A query that returns the N highest or lowest rows overall or per group.

Definition

An overall Top-N uses ORDER BY with LIMIT or TOP. Top-N per group, such as the top 3 products per region, needs a window function like ROW_NUMBER partitioned by group, then a filter through QUALIFY or a subquery.

Example

SELECT * FROM Sales QUALIFY ROW_NUMBER() OVER (PARTITION BY Region ORDER BY Revenue DESC) <= 3;

Exam tip

Top-N per group cannot be done with LIMIT alone; it needs a window function.

Related terms