Top N and Row Limiting Techniques in Google Sheets QUERY

Published on

When using the QUERY function to group and aggregate data in Google Sheets, “Top N” and “row limiting” often sound like the same thing—but they are not.

Depending on your data structure and reporting goal, you may need one of the following approaches:

  • Native row limiting (LIMIT)
    Restricting the number of rows returned after aggregation, with or without sorting.
    (Single row-field grouping)
  • Top N (global ranking)
    Ranking aggregated results using SORTN() with full control over duplicate (tie) handling.
    (Single row-field grouping)
  • Top N per group
    Extracting the highest N values within each category, similar to an Excel Pivot Table, using COUNTIFS() with QUERY.
    (Two row-field groupings)

This hub explains all three techniques, their limitations, and—most importantly—when to use each one. It also acts as a navigation guide to the detailed tutorials linked under each section.

First: Row Limiting vs Ranking (Critical Distinction)

Before choosing a formula, clarify what problem you’re solving.

QuestionYou need
Show only the first N rows, with or without sorting aggregated valuesRow limiting
Show the highest N values with control over duplicates (ties)Ranking (Top N)
Show the highest N values per category (similar to an Excel Pivot Table)Per-group ranking

Many QUERY formulas appear to rank data—but in reality, they only limit rows after sorting.

Sample Data

In all the techniques below, we use the same sample dataset representing fruit production quantities across the North, South, East, and West regions.

  • Column A: Fruit picking date
  • Column B: Fruit name
  • Column C: Region
  • Column D: Quantity
Google Sheets sample data with fruit, region, date, and quantity for Top N and row limiting techniques

Technique 1: Limiting Rows Using QUERY (LIMIT)

This is the simplest and fastest method.

Use this when:

  • You don’t care about ranking logic
  • You only want to restrict the output size
  • Duplicate aggregated values don’t matter

Example: Basic row limiting

=QUERY(A1:D,
 "SELECT Col2, SUM(Col4)
  WHERE Col2 IS NOT NULL
  GROUP BY Col2 LIMIT 3", 1)

This returns three fruits because the number of output rows is limited to 3.

Productsum Qty
Apple330
Banana135
Litchi330

It is useful when you want to pick any three fruits along with their total production quantity, without considering which fruits have the highest totals.

Sorting + limiting

=QUERY(A1:D,
 "SELECT Col2, SUM(Col4)
  WHERE Col2 IS NOT NULL
  GROUP BY Col2 ORDER BY SUM(Col4) DESC LIMIT 3", 1)

This also returns three fruits, but only after sorting the total production quantity in descending order.

Productsum Qty
Mango410
Pineapple405
Apple330

Important limitation

If the aggregated values are:

410, 405, 330, 330, …

LIMIT 3 simply returns the first three rows after sorting. There is no control over ties, and duplicate values beyond the third row are ignored.

Detailed tutorial: Limiting Number of Rows in Google Sheets QUERY

Note: In this hub article, the QUERY examples use Col1, Col2, etc., for clarity and consistency. In the detailed child tutorial, the same logic is shown using direct column references (A, B, etc.). Both approaches are functionally identical in Google Sheets.

Technique 2: Extract Top N from Aggregated QUERY Results (Global Ranking)

Here, ranking actually matters.

While ORDER BY + LIMIT may look like ranking, it cannot handle duplicate aggregated values (ties). This is where SORTN() becomes essential.

Why QUERY alone is not enough

If your aggregated values are:

10, 10, 9, 9, 7, 6

Using QUERY with ORDER BY and LIMIT 3 returns:

10, 10, 9

There is no way to:

  • Return 10, 10, 9, 9 (include all ties at the Nth position), or
  • Return 10, 9, 7 (unique ranking)
  • Return 10, 10, 9, 9, 7 (unique ranking with all duplicates included)

Correct approach using SORTN()

=SORTN(
 QUERY(
  QUERY(A1:D,
   "SELECT Col2, SUM(Col4)
    WHERE Col2 IS NOT NULL
    GROUP BY Col2
    ORDER BY SUM(Col4) DESC"),
  "OFFSET 1", 0),
 3, 1, 2, FALSE)

Result (Top 3 with ties included):

Productsum Qty
Mango410
Pineapple405
Apple330
Litchi330

Why this works

  • Inner QUERY → performs aggregation and sorts the aggregated values
  • Outer QUERY → removes the header row
  • SORTN() → applies true ranking with full control over duplicates

Tie handling (briefly)

  • SORTN() supports multiple tie modes
  • In this example, the tie mode returns N rows plus all duplicates of the Nth value

Full explanations, examples, and edge cases are covered in the detailed tutorial below.

Detailed tutorial: Extract Top N from Aggregated Query Results in Google Sheets

This is the best method for extracting Top N values after aggregation in Google Sheets.

Technique 3: Extract Top N Per Group (QUERY Workaround)

This is the most advanced scenario.

Think of an Excel Pivot Table with two row fields:

  • Category (e.g., Region)
  • Item (e.g., Product)

Goal

Show the Top N products within each region, including duplicates at the Nth rank.

Example formula

=ArrayFormula(
 LET(
  qry,
   QUERY(
    QUERY(A1:D,
     "SELECT Col3, Col2, SUM(Col4)
      WHERE Col3 IS NOT NULL
      GROUP BY Col3, Col2
      ORDER BY Col3, SUM(Col4) DESC", 1),
    "OFFSET 1", 0),

  rnk,
   COUNTIFS(
    CHOOSECOLS(qry,1), CHOOSECOLS(qry,1),
    CHOOSECOLS(qry,3), ">"&CHOOSECOLS(qry,3)
   ) + 1,

  FILTER(qry, rnk <= 3)
 )
)

Result

This returns the Top 3 fruits for each region. If the 3rd-ranked total quantity is tied, all tied rows are included in the result.

RegionProductsum QTY
EastApple110
EastMango110
EastOrange95
EastPineapple95
NorthPineapple310
NorthApple100
NorthMango100
SouthMango200
SouthBanana135
SouthApple120
WestLitchi330
WestWatermelon200

How it works

  • Inner QUERY → aggregates data by category and item
  • Outer QUERY → removes the header row
  • COUNTIFS → calculates the rank within each category
  • FILTER → keeps only the Top N rows per category

This approach mimics Excel Pivot Table ranking (Top N per group), which Google Sheets’ QUERY function cannot perform natively.

Detailed tutorial: Google Sheets: Extract Top N per Group from Query Aggregation

Conclusion

While Google Sheets’ QUERY function is powerful, row limiting and ranking are not the same thing.

Quick guidance

  • Use LIMIT when you just want fewer rows
  • Use SORTN() when ranking and duplicates matter
  • Use COUNTIFS() + QUERY for Top N per group

Understanding this distinction prevents incorrect reports and ensures your formulas match your analytical intent.

Use this guide as your starting point, then dive into the linked tutorials for full step-by-step explanations and variations.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Top Discussions

More like this

The Complete Guide to XLOOKUP in Google Sheets (15+ Practical Examples)

The XLOOKUP function largely replaces traditional lookup functions such as LOOKUP, VLOOKUP, and HLOOKUP...

How to Sort and Filter Pivot Tables in Google Sheets (Complete Guide)

Sorting and filtering are two of the most important techniques for analyzing data in...

Pivot Table Formatting, Output & Special Behavior in Google Sheets

Pivot Tables in Google Sheets are powerful—but they can get tricky once you move...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.