New Tool Radar

AI for Data & Spreadsheets

How to Check an AI-Written QUERY Formula in Google Sheets for Valid Syntax

Check an AI-written QUERY formula for syntax the QUERY language accepts, including quoted text and column letters.

Schematic with three steps: Read the query string clause by clause; Check the quotes around text values; Run the query and count the rows.

QUERY in Google Sheets takes a range and a query string written in the Google Visualization API query language, with clauses such as select, where and order by. It looks like SQL but is not SQL: column labels must be quoted in a particular way, and text values need single quotes inside the double-quoted query string. An AI-written QUERY often mixes SQL habits into it and fails. Test the query on a small range before trusting it.

What you need

  • The AI-written QUERY formula
  • The source range, with its headers known
  • The rows the query should return, worked out by hand

Step 1: Read the query string clause by clause

Copy the query string out of the formula and read each clause: select, then where, then order by. Check that every column name or letter used in the query exists in the range.

Step 2: Check the quotes around text values

Text values in a where clause need single quotes inside the double-quoted query string, such as where B = ‘East’. A missing pair of single quotes is the most common reason a QUERY fails.

Step 3: Run the query and count the rows

Paste the formula into a test cell and count the rows it returns against your hand-worked list. If it returns an error, read the error message: it usually names the clause that failed.

Worked example: synthetic region query

The table below is a synthetic example. It shows the expected result of checking an AI-written QUERY formula; no formula was run.

Check Value Result
Query as written select * where B = East Text value unquoted
Result Error QUERY cannot parse East
Corrected query select * where B = ‘East’ Single quotes added
Rows returned 6 Matches the hand count

Expected result (UNTESTED). the QUERY with East unquoted returns an error, because text values need single quotes; quoting it as ‘East’ returns six rows, matching the hand count. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • QUERY is a Google Sheets function; Excel has no QUERY function, so the formula does not transfer.
  • Column letters in a query refer to the range, not the sheet, so Col1 is the first column of the range.
  • A header row counts as row 1 of the range unless the third argument says otherwise.
  • QUERY treats numbers and text in one column as different types, and a mixed column can return blanks.

Sources

See all