Google Sheets intermediate query sql filter sort

Google Sheets QUERY Function: The Complete Guide

QUERY Function Basics is a Google Sheets function that query uses a sql-like syntax to filter and sort data. Formula Genius generates and validates this formula automatically from a plain-English prompt.

QUERY brings SQL-like power to Google Sheets. Filter, sort, group, and aggregate data with a single formula.

The Formula

Prompt

"Show all rows where Department is Sales, sorted by Revenue descending"

Google Sheets
=QUERY(A1:D100,"SELECT * WHERE B='Sales' ORDER BY D DESC")

QUERY uses a SQL-like syntax to filter and sort data. SELECT * returns all columns, WHERE filters rows, and ORDER BY sorts results.

Step-by-Step Breakdown

  1. A1:D100 is the data range (including headers)
  2. SELECT * returns all columns (or specify: SELECT A, B, D)
  3. WHERE B='Sales' filters to rows where column B equals Sales
  4. ORDER BY D DESC sorts by column D in descending order
  5. String values in WHERE must be in single quotes

Edge Cases & Warnings

  • Column references use letters (A, B, C), not column names
  • Dates require special format: date '2026-01-01'
  • Mixed data types in a column can cause unexpected results
  • QUERY with headers: add third argument for header row count

Examples

Prompt

"Department=Sales, sorted by Revenue"

Google Sheets
All Sales rows, highest revenue first
Prompt

"Department=Engineering, top 5 by Revenue"

Google Sheets
Add LIMIT 5 to the query string

Frequently Asked Questions

Is QUERY available in Excel?

No. QUERY is exclusive to Google Sheets. The closest Excel equivalents are FILTER + SORT functions in Excel 365, or Power Query for complex transformations.

Can I reference cell values in QUERY?

Yes: =QUERY(A1:D100,"SELECT * WHERE B='"&E1&"'"). Concatenate the cell reference into the query string.

Can't find what you need?

Describe any formula in plain English and Formula Genius will generate, explain, and validate it — instantly.