# QUERY basics: select, where, order by (Google Sheets lesson 12 of 17)

> Ask a range a question in plain, SQL-like words and get a live table of answers back.

Source: https://spreadsheetpractice.com/google-sheets/learn/query-function-basics · Updated October 29, 2026

- **Course:** [Google Sheets course](https://spreadsheetpractice.com/google-sheets), track "Sheets power tools"
- **Time:** about 12 minutes, 3 graded exercises
- **Functions:** `QUERY`, `select`, `where`, `order by`, `limit`
- **Access:** Pro (the lesson text is free; the graded exercises need Pro)
- **Key formula:** `=QUERY(A1:E9,"select A, B, D",1)`: Three columns, in the order listed

## How it works

`QUERY` runs a short question, written in a language much like SQL, against a range: `=QUERY(A1:E9, "select A, B where D < 10", 1)`. The first argument is the data, the second is the query in double quotes, and the third is how many header rows the data has. The results, header row included, fill the cells below and to the right.

Columns are named by their letters in the sheet, not by their headers. `select A, B, D` returns those three columns in that order, and `select *` returns all of them. Pass the headers argument every time. Without it, Sheets guesses from the data, and a wrong guess can treat your header row as data or your first row of data as a header.

`where` keeps the rows that pass a test. Numbers go in as they are: `where D < 10`. Text goes in single quotes, because the whole query already sits inside double quotes: `where C = 'Leafline'`. Join tests with `and` and `or`, and use parentheses when you mix them. For part of a text value, use `contains`, `starts with` or `ends with`.

Text comparisons in QUERY are case-sensitive, so `where B contains 'green'` misses "Green tea". Wrap the column in `lower()` and compare with lowercase text to match any capitalization. Finally, `order by E desc` sorts the result largest first and `limit 3` keeps the first three rows. The clauses always come in this order: select, where, order by, limit.

## Syntax

| Formula | What it does |
|---|---|
| `=QUERY(A1:E9,"select A, B, D",1)` | Three columns, in the order listed |
| `=QUERY(A1:E9,"select * where D < 10",1)` | Every column, only rows that pass the test |
| `=QUERY(A1:E9,"select B where C = 'Leafline' and D > 0",1)` | Text in single quotes; join tests with and, or |
| `=QUERY(A1:E9,"select A, B where A starts with 'TEA'",1)` | Text that begins with something; also contains, ends with |
| `=QUERY(A1:E9,"select B, E order by E desc limit 3",1)` | The three highest values in column E |
| `=QUERY(A1:E9,"select B where lower(B) contains 'green'",1)` | Match any capitalization |

## Watch out for

- Columns are the sheet's letters, not the header names. "select Product" returns an error; "select B" works.
- Text inside the query needs single quotes: where C = 'Leafline'. A double quote would end the query string early.
- Clauses have a fixed order: select, where, order by, limit. Putting order by before where is an error.
- QUERY needs empty cells below and to the right for its results, header row included.

## The exercises

Solve them in the live grid at [QUERY basics: select, where, order by](https://spreadsheetpractice.com/google-sheets/learn/query-function-basics). Each answer is graded, then re-checked on changed data so typed-in numbers don't pass.

1. **Low stock report.** In G1, use QUERY on A1:E9 to list the SKU, product and stock (columns A, B and D) of every item with fewer than 10 in stock. Pass 1 as the headers argument.
2. **Two suppliers, priciest first.** In G1, list the product, stock and price (columns B, D and E) of every item from Leafline or Hillside Co that is in stock (stock above 0), most expensive first.
3. **Search by name.** A customer asks for anything green. In G1, list the SKU, product and price (columns A, B and E) of every product whose name contains "green", whether it's written "Green" or "green". Sort cheapest first and show at most 2 rows.

Previous: [REGEXMATCH, REGEXEXTRACT and REGEXREPLACE](https://spreadsheetpractice.com/google-sheets/learn/regexmatch-regexextract-regexreplace) · Next: [QUERY: group by, totals and labels](https://spreadsheetpractice.com/google-sheets/learn/query-group-by)

---

Spreadsheet Practice (https://spreadsheetpractice.com) teaches spreadsheet formulas by practice: every exercise is graded in a live grid and re-checked on changed data.
