Lesson 69 of 92 · Free Excel Course
Excel · Excel function reference
Excel QUERY Function? Use FILTER, SORT and Power Query Instead
Excel has no QUERY function (=QUERY gives #NAME?). Here is the Excel formula for each Google Sheets QUERY job: select, where, order by, group by and limit.
People searching for the Excel QUERY function usually have a Google Sheets formula they want to use in Excel. The catch: Excel has no QUERY function. QUERY belongs to Google Sheets, and if you type =QUERY(...) in any version of Excel you get #NAME?, the error Excel shows for a function name it doesn't recognize. The good news is that every common QUERY job has an Excel answer. You build it from a few smaller functions: CHOOSECOLS picks columns, FILTER keeps the rows you want, SORT and SORTBY put them in order, and TAKE keeps the top few. Each returns a live result that spills into the cells below and updates when the data changes. FILTER and SORT work in Excel for Microsoft 365, Excel for the web, Excel 2024 and Excel 2021; CHOOSECOLS and TAKE need Microsoft 365, the web version or Excel 2024. For SQL-style jobs on big or messy data, Excel's tool is Power Query.
Syntax
This is the Google Sheets function you may be translating:
=QUERY(data, query, [headers])
- data (required): the range to query. Google says each column should hold one type of value (numbers, text or TRUE/FALSE).
- query (required): the query text in Google's query language, in quotes, such as
"select A, D where D > 300". Columns are named by letter. Text values usually go in single quotes,B = 'East', because the whole query is already inside double quotes. - headers (optional): how many header rows
datahas. If you leave it out, Sheets guesses.
In Excel, the two functions that do most of that work are:
=FILTER(array, include, [if_empty])
=SORT(array, [sort_index], [sort_order], [by_col])
- array (required in both): the range to return, without its header row.
- include (required): a TRUE/FALSE test the same height as
array, such asD2:D9>300. Multiply tests for AND, add them for OR. - if_empty (optional): what to show when nothing matches. Leave it out and an empty result shows #CALC!.
- sort_index (optional): which column of
arrayto sort by. Default 1. - sort_order (optional): 1 for ascending (the default) or -1 for descending.
- by_col (optional): FALSE (the default) sorts rows; TRUE sorts columns.
Two helpers finish the job: CHOOSECOLS(array, col_num1, ...) returns the columns you list by number, and TAKE(array, rows) keeps the first rows rows (a negative number takes from the bottom).
Here is the whole translation at a glance:
| QUERY clause | Excel function |
|---|---|
| select A, D | CHOOSECOLS(range,1,4) |
| where | FILTER |
| order by | SORT or SORTBY |
| group by with sum() | SUMIFS on a UNIQUE list, or a PivotTable |
| limit | TAKE |
Examples
All five examples use this sales list in A1:D9. Each one shows the Google Sheets formula first, then the Excel formula that gives the same rows.
| A | B | C | D | E | F | G | |
|---|---|---|---|---|---|---|---|
| 1 | Rep | Region | Product | Sales | select A, D | ||
| 2 | Ana | East | Chairs | 420 | Rep | Sales | |
| 3 | Bo | West | Desks | 610 | Ana | 420 | |
| 4 | Cole | East | Desks | 380 | Bo | 610 | |
| 5 | Dina | North | Chairs | 150 | Cole | 380 | |
| 6 | Eli | West | Lamps | 95 | Dina | 150 | |
| 7 | Fay | East | Lamps | 450 | Eli | 95 | |
| 8 | Gil | North | Desks | 540 | Fay | 450 | |
| 9 | Hal | West | Chairs | 305 | Gil | 540 | |
| 10 | Hal | 305 |
1. select A, D
Google Sheets: =QUERY(A1:D9,"select A, D",1)
Excel: =CHOOSECOLS(A1:D9,1,4)
CHOOSECOLS counts columns from the left edge of the range, so 4 means the fourth column of A1:D9, not column D of the sheet. Start the range at A2 if you don't want the headers.
2. where
Google Sheets: =QUERY(A1:D9,"select A, B, D where D > 300",1)
Excel: =FILTER(CHOOSECOLS(A2:D9,1,2,4),D2:D9>300)
| I | J | K | |
|---|---|---|---|
| 1 | Rep | Region | Sales |
| 2 | Ana | East | 420 |
| 3 | Bo | West | 610 |
| 4 | Cole | East | 380 |
| 5 | Fay | East | 450 |
| 6 | Gil | North | 540 |
| 7 | Hal | West | 305 |
For two conditions, QUERY writes where B = 'West' and D > 200. In Excel, multiply the tests: =FILTER(A2:D9,(B2:B9="West")*(D2:D9>200)) returns Bo and Hal. Use + instead of * for "or".
3. where plus order by
Google Sheets: =QUERY(A1:D9,"select A, D where B = 'East' order by D desc",1)
Excel: =SORT(FILTER(CHOOSECOLS(A2:D9,1,4),B2:B9="East"),2,-1)
| M | N | |
|---|---|---|
| 1 | Rep | Sales |
| 2 | Fay | 450 |
| 3 | Ana | 420 |
| 4 | Cole | 380 |
4. group by with sum
Google Sheets: =QUERY(A1:D9,"select B, sum(D) group by B",1)
Excel: P2: =SORT(UNIQUE(B2:B9))
Q2: =SUMIFS(D2:D9,B2:B9,P2) (copy down to Q4)
| P | Q | |
|---|---|---|
| 1 | Region | sum(D) |
| 2 | East | 1250 |
| 3 | North | 690 |
| 4 | West | 1010 |
In Excel 2021, 2024 and Microsoft 365 you can type =SUMIFS(D2:D9,B2:B9,P2#) once in Q2 instead. P2# means "the whole list spilled from P2", so new regions get a total automatically. Excel for Microsoft 365 (not Excel 2024 or 2021) also has GROUPBY, which does the grouping and the sum in one formula. For a summary you'll filter and rearrange, a PivotTable is quicker still.
5. order by plus limit
Google Sheets: =QUERY(A1:D9,"select A, D order by D desc limit 3",1)
Excel: =TAKE(SORTBY(CHOOSECOLS(A2:D9,1,4),D2:D9,-1),3)
| S | T | |
|---|---|---|
| 1 | Rep | Sales |
| 2 | Bo | 610 |
| 3 | Gil | 540 |
| 4 | Fay | 450 |
When the job is bigger: Power Query
If your QUERY joins tables, cleans imported data or runs on thousands of rows every week, Power Query is the closer match. Select the list, choose Data > From Table/Range, then use the editor's buttons to filter rows, remove columns, sort and Group By. Click Close & Load and the result lands on a new sheet; next time, Refresh repeats every step. The full walk-through is in Power Query and Power Pivot.
Common errors
- #NAME? on
=QUERY(...): Excel has no QUERY function. Rebuild the formula with the examples above. You also get #NAME? if your Excel is too old for FILTER and SORT (Excel 2019 and earlier) or for CHOOSECOLS and TAKE (Excel 2021 and earlier). - #CALC!: FILTER found no matching rows. Add the third argument, for example
=FILTER(A2:D9,B2:B9="South","No sales"). - #SPILL!: something is in the cells where the result needs to go. Clear them, or move the formula to an empty area.
- #VALUE! from FILTER: the
includerange isn't the same height asarray. Make both cover the same rows (A2:D9 and D2:D9). - #VALUE! from CHOOSECOLS: a column number is 0 or bigger than the number of columns in the range.
- Criteria copied with single quotes: QUERY writes
'East'; Excel needs double quotes,"East". Single quotes give a formula error. - Groups in a different order: UNIQUE lists values in the order they first appear. Wrap it in SORT to match QUERY's sorted groups.
Related functions
- Sort and filter: the lesson that teaches FILTER, SORT and SORTBY in depth.
- UNIQUE function: one entry per value, the first half of a group-by.
- Pivot tables: group and total with no formulas.
- Power Query and Power Pivot: repeatable, SQL-style queries.
- SEQUENCE function: another spilling function that pairs well with these.
Try it yourself
Download the practice workbook. The Practice sheet has the sales list and six yellow cells, each labelled with the QUERY clause to rebuild: select, where, where plus order by, group by with sum, and limit. Type one formula in each and let it spill, then compare with the Answers sheet.
Frequently asked questions
Is there a QUERY function in Excel?
No. QUERY is a Google Sheets function, and typing =QUERY in Excel returns #NAME?. In Excel you combine smaller functions instead: CHOOSECOLS picks columns, FILTER keeps matching rows, SORT or SORTBY orders them and TAKE keeps the first few. For bigger, repeatable jobs use Power Query.
What happens to QUERY formulas when I open a Google Sheet in Excel?
Excel can't run them, because it has no QUERY function. Expect to rebuild each one with FILTER, SORT, CHOOSECOLS and TAKE using the table on this page. Until you do, treat any numbers that came across as a snapshot, not a live result.
Can I write SQL in Excel?
Not in a worksheet formula. Power Query does the same jobs as SQL (filter rows, choose columns, group and sum, join tables) with buttons, and it records each step so you can refresh it. A PivotTable is the quickest way to group and total without formulas.
Which Excel versions have FILTER, SORT, CHOOSECOLS and TAKE?
FILTER and SORT are in Excel for Microsoft 365, Excel for the web, Excel 2024 and Excel 2021. CHOOSECOLS and TAKE are newer: Microsoft 365, Excel for the web and Excel 2024 only. In older versions they show #NAME?, so use the Filter button, Sort, or Power Query.