I. Intro
The QUERY function, which uses SQL-like syntax, is a versatile tool for searching and filtering data, with support for aggregate functions.
II. About the function
- Formula: QUERY(range, [query], [headers])
- Arguments:
- Range: The range of data to be queried. Each column can only contain one type of data, which must be a boolean, number (including date/time types), or string.
- Query: Optional. When this is omitted, the function will return all values within the range. The query clause should be written in SQL-like syntax and needs to be enclosed in double quotation marks. You can combine clauses led by different keywords, and the clauses need to be separated by spaces. In clauses led by a keyword, you can use aggregate functions, date functions, and operators. A comma should go before the function.
- Note: The following keywords are supported: Select, where, group by, pivot, order by, limit, offset, label. If multiple keywords are used, they must follow the order listed. For example, select must be at the beginning, and so on.
- Headers (optional): The number of rows to be used as the header. When this is omitted, it is automatically set by the function. Enter 1 to set the first row in the source data as the query header; enter 2 to set the first two rows as headers, and so on.
- In this example, the total sales volume for each product needs to be summarized. To try it out yourself, use this template: Use the Query Function (Template).
III. Query description
Keywords
Aggregate and date functions
Note: The columns referenced in the date function need to be in time format. For more details, see Change the number format in Sheets.
Operators
IV. Steps
Use Query function
- Open the spreadsheet, select a cell, and enter =QUERY(range, [query], [headers]).
- Press Enter.
For example, you can use the forumla =QUERY(A:D, "select B,year(A),sum(D) where B='Southeast' and C='Latte' group by B, year(A) order by sum(D) DESC" to find the total sales of latte in Southeast over three years in the chart below. At the same time, you can group data by store and year, and sort in descending order by total sales. Full details of the formula below:
- select: Use the select function to choose the columns you want to display in the query results. In this case, we use:
- B: Column B, the column that contains the region.
- year(A): The year information from column A.
- sum(D): The total of the relevant data in column D (The total sales of the selected region in the selected year)
- where: Set the query conditions with the where function, filtering records that meet the query conditions. In this case, we use:
- B='Southeast': The value in column B needs to be 'Southeast', where the text value 'Southeast' is enclosed in single quotes.
- and C = 'Latte': At the same time, the value in column C needs to be 'Latte', where the text value 'Latte' is enclosed in single quotes.
- group by: Set the grouping dimension and sorting method. In this case, we use:
- B: Group data by store
- year(A): Group data by year
- order by: Set the sorting method. In this case, we use:
- sum(D): Sort by total sales
- desc: Sort in descending order
250px|700px|reset
Delete Query function
Select the cell with the function and press Delete.
V. FAQs