To run a query in Google Sheets, enter the QUERY function in an empty cell, give it a data range and a query written in Google’s query language, then press Enter. For example, =QUERY(A1:D6,"select A, B, C where D = 'Open' order by C desc",1) returns selected columns for open items, sorted by the values in column C. You’ll need a sheet with organized data and a blank area where the results can appear.
What You’ll Need
- A Google account and a spreadsheet open in Google Sheets in a web browser.
- A data table with one field per column, such as Date, Product, Sales, and Status. Put descriptive headers in the first row when possible.
- An empty cell or clear area large enough for the query results to expand into. The formula returns a result table, rather than changing or rearranging the source data.
Step-by-Step
- Check and organize the source data. Make sure each column has a consistent meaning and that the header row is distinct from the records. Remove merged cells from the data area, and check that values you intend to compare use consistent types. For example, sales amounts should be numbers, not a mixture of numbers and text such as “$100” typed into some cells. Suppose columns A through D contain Product, Region, Sales, and Status, with headers in row 1 and records in rows 2 through 6.
- Choose where the results should go. Click a blank cell outside the source table, such as F1. Leave enough empty space below and to the right for the output. QUERY results expand into neighboring cells; existing content in the way can prevent the result from displaying. Putting the formula on a separate results area or a new sheet also makes it easier to compare the output with the original data.
- Start the QUERY function. Type
=QUERY(into the selected cell. Google Sheets will show a function hint. The function takes a data range, a query string, and an optional header-row count, in that order. The query string is text inside quotation marks and contains instructions such as which columns to select and how to filter or sort them. - Enter the source range. For the example table, type
A1:D6,after the opening parenthesis. Include the header row if you plan to tell the function that the data has one header row. Make the range large enough to include every record you want considered. If you add new rows later, a range that ends at row 6 will not include them; update the range or choose an appropriate larger range. - Write the query string. After the range and comma, enter
"select A, B, C where D = 'Open' order by C desc". The wordsselect,where, andorder byare query-language clauses. Here, the formula returns columns A, B, and C; keeps rows where Status in column D equals Open; and sorts the results by Sales in descending order. In the query, put text values such as Open in single quotes. The whole query itself is enclosed in double quotes. - Set the number of header rows and finish the formula. Add
,1)after the closing quote, then press Enter. The complete formula is=QUERY(A1:D6,"select A, B, C where D = 'Open' order by C desc",1). The final1tells Sheets that the first row of the selected range contains one header row. Use0if the selected range has no header row. The argument is optional, but specifying it can help Sheets interpret the table reliably. - Review the returned table. With sample rows for Widget, Gadget, Cable, and Case, the formula returns only records marked Open. If their Sales values are 150, 80, 130, and 95 respectively, the output is Widget (150), Cable (130), Case (95), and Gadget (80), in that order. The formula does not delete or edit the original records; it creates a live result based on them.
- Adjust the query to answer a different question. Change the selected columns or conditions in the quoted query string. For example, to return only Product and Region for sales above 100, use
=QUERY(A1:D6,"select A, B where C > 100",1). To sort all records by Sales from largest to smallest, use=QUERY(A1:D6,"select A, B, C order by C desc",1). Keep the range and header count aligned with the data you actually selected.
Common Mistakes
Mixing up the two kinds of quotation marks. The entire query is a text string enclosed in double quotes, while text values inside that query use single quotes. For instance, the condition is written D = 'Open' inside the double-quoted query. Leaving out a quote or using mismatched quotation marks can cause a formula parse error.
Using the wrong header count. The third argument describes how many header rows are in the selected range, not how many columns the query returns. If the range starts on the header row, use 1 for one header row. If it starts with data, use 0. An incorrect setting can make the first record look like a heading or cause headers to be treated as data.
Referencing a column that is not in the range. The column references in the query must correspond to columns included in the selected data. If a query refers to a field outside its range, revise the range or the query. When experimenting, start with a range that includes all the fields your conditions and sorting need, even if you do not display every field.
Expecting the formula to overwrite occupied cells. QUERY returns an array of results, which needs room to expand. If cells in the output area contain anything, including a stray space or an old value, the result may show an expansion error. Clear the blocked cells or move the formula to a clear area.
Comparing inconsistent values. A condition for the text Open will not match a differently spelled status such as open, Open with a trailing space, or In Progress. QUERY also determines a column’s data type from its contents; when a column mixes types, values of the minority type may be treated as null. Clean or standardize the source column if some expected rows are missing.
Tips
- Build the query gradually. Start with
select A, B, confirm it returns the expected columns, then add awherecondition and sorting. This makes it easier to spot which clause introduced a problem. - Use plain, consistent headers and avoid using the data table’s top row for a report title. Keep headers in one row and records directly underneath; this makes the header count and column meanings easier to manage.
- Use
order byto sort the returned results without changing the order of the source records. Addascfor ascending order ordescfor descending order. If you leave the direction out, ascending order is used. - Use
labelwhen you want clearer headings in the output. For example,=QUERY(A1:C6,"select A, C label C 'Sales total'",1)changes the displayed heading for column C to Sales total. This changes the result’s label, not the original header cell. - Keep a copy of a working query before making a complicated edit. You can copy the formula into another cell or document its purpose in a nearby note. If you share the sheet, explain what the query filters and which source range it uses.
- Remember that QUERY recalculates from its source data. A change to a matching source value can change the results, and adding rows outside a fixed range will not. Choose the range with the sheet’s expected growth in mind.
Troubleshooting
“Formula parse error” appears. Check that the formula begins with an equals sign, that parentheses are balanced, and that the function arguments are separated by commas. Confirm that the full query string has opening and closing double quotes and that any text condition has matching single quotes. If your spreadsheet’s locale expects semicolons between function arguments, use the separators Sheets requires for that locale; the query-language clauses inside the quoted string remain part of the query.
The formula returns an error saying the result was not expanded. Inspect the cells where the output should appear and clear any existing values or merged cells in the way. The result table needs an unobstructed space. If you do not want to clear existing content, move the formula elsewhere.
The result is empty or some rows are missing. Check the condition’s column and value for spelling, spaces, and data type. A numeric comparison such as C > 100 expects numeric values in column C, while a text comparison needs quoted text. Also verify that the source range contains the rows in question and that the header argument is set correctly.
The output has unexpected headings or values. Recheck whether the selected range includes the header row and whether the third argument matches that range. If the source has mixed numbers and text in one column, standardize it. For a quick check, temporarily select fewer columns and remove conditions so you can see what the query reads from the source.
The formula worked, but new records do not appear. The query may use a fixed range that ends before the new rows. Edit the range to include them, or use a broader range when appropriate. If the sheet contains many unrelated rows, choose a range that includes the full data table without unnecessarily querying a very large area.
Frequently Asked Questions
What does QUERY do in Google Sheets?
QUERY reads a data range and returns rows or columns that meet instructions written in Google’s query language. You can use it to select fields, filter records, sort results, group values, and perform supported calculations, while leaving the source data in place.
Do I need to install anything to use QUERY?
No. QUERY is a built-in Google Sheets function. Open the spreadsheet in a browser, enter the formula in a cell, and press Enter. You do not need an add-on or a separate database tool for a basic query.
Can I use QUERY to filter by more than one condition?
Yes. Add conditions in the query string using and or or. For example, =QUERY(A1:D6,"select A, C where C > 100 and D = 'Open'",1) returns columns A and C for rows where Sales is above 100 and Status is Open.
Can QUERY change or delete my original data?
No. The function displays a calculated result elsewhere in the sheet. Editing or removing the formula affects that output, not the source records. If you need to change the source, edit the original cells separately.
Why use QUERY instead of a filter?
A filter is handy for interactively showing or hiding source rows. QUERY is useful when you want a separate, formula-driven result table with chosen columns, conditions, or a particular sort order. The better choice depends on whether you want to inspect the original table or generate a separate result.
Is Google Sheets QUERY the same as Excel’s query tools?
No. QUERY is a Google Sheets function with its own query-language syntax. Excel has different tools and functions, including Power Query; a Google Sheets QUERY formula cannot simply be pasted into Excel and expected to work the same way.
Related
- Outlook Image Cache: How to Clear It on Any Device
- Clear the To Field Cache in Outlook: Step-by-Step Guide
- Clear Calendar Cache in New Outlook: Step-by-Step
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- How to Create Custom Rules in Outlook to Manage EmailTo create a custom rule in Outlook, open your mail rules settings, choose a condition such as who…
- File Folder Labels in Word: How to Create ThemTo create file folder labels in Word, open the desktop app, choose Mailings > Labels, select the label…
- Different Ways to Group Rows in ExcelIn Excel for Microsoft 365 on Windows, the main ways to group rows are to create a collapsible…
- How to Get Rid of the Search Bar in ExcelTo hide Excel’s large Search box in Microsoft 365 for Windows, go to File > Options > General,…
- How to Make a Quick Timeline in PowerPoint QuicklyTo make a quick timeline in PowerPoint, choose Insert > SmartArt, select a layout under Process, and enter…
- Google Sheets IF Formula: Syntax, Examples, and TipsIn Google Sheets, an IF formula checks whether a condition is true and returns one value if it…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.