Google Sheets intermediate query sort limit data-analysis

How to Sort and Limit QUERY Results in Google Sheets?

Sort and Limit QUERY Results is a Google Sheets function that sorts the data in column b in descending order and returns the top 10 rows.. Formula Genius generates and validates this formula automatically from a plain-English prompt.

Sorting and limiting your data can help you focus on the most important information. This guide shows you how to achieve that using the QUERY function.

The Formula

Prompt

"Use QUERY to sort results by a column descending and return only the top 10 rows"

Google Sheets
=QUERY(A2:B, "SELECT * ORDER BY B DESC LIMIT 10", 0)

This formula sorts the data in column B in descending order and returns the top 10 rows.

Step-by-Step Breakdown

  1. A2:B specifies the range of data to query.
  2. SELECT * retrieves all columns from the specified range.
  3. ORDER BY B DESC sorts the results by column B in descending order.
  4. LIMIT 10 restricts the output to the top 10 rows.
  5. 0 indicates that the data does not have headers.

Edge Cases & Warnings

  • If there are fewer than 10 rows, the formula will return all available rows.
  • If column B contains non-numeric values, sorting may not behave as expected.
  • If the range A2:B is empty, the formula will return an error.

Examples

Prompt

"Data range A2:B with values: (1, 10), (2, 20), (3, 15)"

Google Sheets
Returns top 10 rows sorted by column B: (2, 20), (3, 15), (1, 10)
Prompt

"Data range A2:B with values: (1, 5), (2, 5), (3, 10)"

Google Sheets
Returns top 10 rows sorted by column B: (3, 10), (1, 5), (2, 5)

Frequently Asked Questions

Can I change the column to sort by?

Yes, just replace 'B' in the formula with the desired column letter.

What if I want to sort in ascending order?

Change 'DESC' to 'ASC' in the ORDER BY clause.

How do I include headers in my output?

Change the last parameter from 0 to 1 if your data range has headers.

Can't find what you need?

Describe any formula in plain English and Formula Genius will generate, explain, and validate it — instantly.