Spreadsheet Practice

Google Sheets lesson 9 of 11, Sheets power tools

QUERY basics: select, where, order by

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

ProSheets mode12 minutes, 3 exercisesQUERYselectwhereorder bylimit

Watch it done

4 steps

Watch the technique worked out on sample data, then try it yourself in the exercises.

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

=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

  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.

The practice grid runs in Sheets mode: FILTER, SORT, QUERY, ARRAYFORMULA, SPLIT and the REGEX functions behave as they do in Google Sheets, and empty results stay blank.

Graded practice for this lesson is part of Pro

The walkthrough and notes are open to everyone. Pro unlocks the exercises here and in the other 27 Pro lessons, Excel and Google Sheets, 79 exercises in all.

  1. Low stock report
  2. Two suppliers, priciest first
  3. Search by name