Google Sheets Query: How to Use LIMIT to Limit Rows


You can use the LIMIT clause to limit the number of rows returned by a Google Sheets query.

The LIMIT clause is particularly useful when you want to get a glimpse at a few rows that meet some criteria without displaying every row in the entire dataset that meets some criteria.

This clause uses the following basic syntax:

=QUERY(A1:C11, "SELECT * LIMIT 5")

This particular query will return the header row from the range A1:C11 along with the first 5 rows.

The following example shows how to use the LIMIT clause in practice

Example: Using LIMIT in Google Sheets Query

Suppose we have the following dataset that contains information about various basketball players:

We can use the following query to return the header row along with the first five rows in the dataset:

=QUERY(A1:C11, "SELECT * LIMIT 5")

The following screenshot shows how to use this query in practice:

Notice that the header row along with the first five rows are returned.

It’s worth noting that if the query would naturally return less than 5 rows, then the LIMIT clause won’t force the output to display five rows.

For example, suppose we use the following query to display all players who have more than 25 points:

=QUERY(A1:C11, "SELECT * WHERE C > 25 LIMIT 5")

The following screenshot shows how to use this query in practice:

Notice that only two rows are returned because there are only two players in the original dataset that have more than 25 points.

Notice that the LIMIT 5 clause didn’t force the output to have 5 rows.

Rather, the LIMIT clause simply sets a maximum value on the number of rows that will be returned.

Additional Resources

The following tutorials explain how to perform other common operations in Google Sheets:

Google Sheets Query: How to Return Only Unique Rows
Google Sheets Query: How to Remove Header from Results
Google Sheets Query: How to Ignore Blank Cells in Query

2 Replies to “Google Sheets Query: How to Use LIMIT to Limit Rows”

    1. Thanks for the follow-up. If you’re trying to limit how many rows appear for each position in your data using Google Sheets, the regular QUERY function can’t do this directly because it doesn’t support limiting rows within each group like SQL does. But there’s a workaround that you can use, and it works well.

      Let’s say you have a table with names, positions, and scores. For example:

      | Name | Position | Score |
      | —– | ——– | —– |
      | Alice | Manager | 95 |
      | Bob | Manager | 89 |
      | Carol | Manager | 88 |
      | Dave | Clerk | 91 |
      | Emma | Clerk | 85 |
      | Frank | Clerk | 84 |

      Now, let’s say you want to show only the top 2 rows for each position based on score.

      Here’s how you can do that using a helper column:

      1. First, sort your table by position (A to Z) and then by score (Z to A), so that the highest scores for each position appear at the top.

      2. Next, add a new column to the right (let’s say Column D), and in row 2 of that column, enter this formula:

      “`
      =COUNTIFS(B$2:B2, B2)
      “`

      This formula counts how many times the same position appears up to that row. In other words, it ranks the entries within each position.

      3. Finally, use the FILTER function to only show the top entries. For example, if you want only the top 2 for each position, use this formula:

      “`
      =FILTER(A2:C, D2:D<=2) ``` This filters your table to include only those rows where the rank (from the helper column) is 2 or less. That’s it. This method helps simulate what SQL calls "ROW\_NUMBER() over PARTITION BY", which isn't available directly in Google Sheets.

Leave a Reply

Your email address will not be published. Required fields are marked *