New Tool Radar

AI for Data & Spreadsheets

How to Check an AI-Written Google Sheets QUERY for Clause Order and Column Names

Test an AI-written QUERY formula in Google Sheets, fix clause order and column letters, and confirm the result against the source range.

Schematic with three steps: Check the clause order; Check the column letters and quotes; Compare the result with the source range.

QUERY runs an SQL-like query on a range: one formula can select, filter, group and sort. The clauses must come in a fixed order, column letters must be capitals, and text values need single quotes; an AI-written QUERY often gets one of those wrong and returns a parse error.

What you need

  • Google Sheets with the source table
  • The AI-written QUERY formula
  • A note of the columns the result should return

Step 1: Check the clause order

Read the query string and confirm the clauses run SELECT, WHERE, GROUP BY, PIVOT, ORDER BY, LIMIT, LABEL, FORMAT in that order. A clause in the wrong place, such as ORDER BY before WHERE, gives a parse error.

Step 2: Check the column letters and quotes

Confirm every column letter is a capital and every text value sits in single quotes. A lower-case column letter returns an error, and text without quotes fails to parse; dates need the date YYYY-MM-DD form.

Step 3: Compare the result with the source range

Run the corrected QUERY and compare its rows with a manual filter of the source range. If rows are missing, check whether a column mixes types, because QUERY gives each column one type and treats the other cells as empty.

Worked example: synthetic QUERY over a sales range

The table below is a synthetic example. It shows the expected result of checking a QUERY formula; no sheet was opened and no formula was run.

Check Finding Result
Clauses in order? No – ORDER BY before WHERE Parse error
Column letters Lower-case b Error
After fixing order and case 3 rows returned Matches manual filter
Missing rows 0 Pass

Expected result (UNTESTED). the AI-written QUERY fails with a parse error because ORDER BY comes before WHERE and the column letter is lower-case. After fixing both, it returns 3 rows that match a manual filter of the source range. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • When the data is an array or comes from IMPORTRANGE, columns are Col1, Col2, not letters.
  • A wrong guess for the headers argument turns the first data row into a header; set it to 1 for one header row.
  • QUERY is fussy about data types; most QUERY problems are really data problems.

Sources

See all