GuidesGoogle Sheets

How to use the QUERY function in Google Sheets

Learn how to use the QUERY function in Google Sheets to filter, sort, and summarize data with a single SQL-like formula.

Quick answer

To use QUERY in Google Sheets, type =QUERY(data, query, headers) into a cell, referencing your data range, a search-language string like "select A, B where C > 10", and a header row count. Google Sheets returns filtered, sorted, or aggregated results instantly, updating automatically whenever your source data changes.

Steps at a glance

  1. Open your sheet and click an empty cell for the results
  2. Type =QUERY( to start the formula
  3. Enter your data range as the first argument
  4. Add a query string in quotes with select, where, or order by clauses
  5. Set the headers argument to match your header row count
  6. Press Enter to run the formula
  7. Adjust the query string as your data or needs change

Summary

The QUERY function lets you filter, sort, select specific columns, and aggregate data in Google Sheets using a single formula written in a syntax similar to SQL. It replaces multiple nested formulas or manual filtering with one flexible statement that updates automatically as your source data changes.

Step-by-step guide

  1. Step 1

    Identify your data range

    Open the sheet containing your source data and note the exact range you want to query, including a header row if your data has column labels. If your data lives on a separate tab, you can reference it directly in the formula, or use /guides/google-sheets/add-a-new-sheet to organize your source and results on different tabs.

  2. Step 2

    Click the cell where you want results to appear

    Choose an empty cell away from your source data, ideally on a new sheet or a blank area, since QUERY spills results into adjacent cells automatically. Avoid placing it where existing data could get overwritten.

  3. Step 3

    Type the QUERY formula structure

    Enter =QUERY( followed by three arguments separated by commas: the data range, the query string in quotation marks, and a headers value. For example: =QUERY(A1:E100,"select A, C where D > 50",1).

  4. Step 4

    Write your query string

    Inside the quotation marks, use clauses like select to choose columns, where to filter rows, order by to sort, and group by with aggregate functions like sum or avg to summarize data. Reference columns by letter (A, B, C) based on their position in the range you selected, not their actual sheet column letters.

  5. Step 5

    Set the headers argument

    The third argument tells QUERY how many rows at the top of your range are headers. Use 1 if your first row has column titles, or 0 if your data starts immediately with values.

  6. Step 6

    Press Enter and review the results

    Run the formula and check that the returned rows match what you expected. If you need to further refine or visually scan results, pair QUERY with /guides/google-sheets/create-a-filter on the output range or sort the results using /guides/google-sheets/sort-a-table.

  7. Step 7

    Refine the query as needed

    Edit the query string directly in the formula bar to add more conditions, change sort order, or select different columns. Since QUERY recalculates automatically, any edits to your source data or the query string update the results instantly.

Why this matters

You need to pull a filtered, sorted subset of a large spreadsheet — like top sales by region — without building a separate report each time. Learning QUERY lets you write one formula that updates live, saving you from rebuilding pivot logic or stacking IF and FILTER formulas manually.

Frequently asked questions

  • Do I need to know SQL to use the QUERY function?

    No, but familiarity with SQL helps since the syntax uses similar clauses like select, where, and order by. Beginners can start with simple select and where statements before adding aggregation or grouping.

  • Can QUERY reference data from another sheet or spreadsheet?

    Yes, you can reference another tab by including its name in the range, such as Sheet2!A1:E100. To pull data from a completely separate spreadsheet file, combine QUERY with /guides/google-sheets/use-importrange.

  • What is the difference between QUERY and FILTER?

    FILTER is simpler and only returns rows matching a condition, while QUERY can filter, sort, select specific columns, and aggregate data all in one formula, making it more powerful for complex reporting needs.

  • Why does my QUERY formula only show part of my expected results?

    This usually happens when the data range in the formula doesn't cover all your rows, or when mixed data types in a column cause some values to be treated as null. Confirm the range and check for consistent formatting in each column.

Create interactive demos like this one — free, no coding required.

Start for free →