Query Google Sheets like SQL
There are two ways to run SQL-style queries against a Google Sheet: the built-in QUERY() function inside the sheet, and a REST API for querying from your app or an AI agent. Here is when to use each.
Last updated
Key facts
-
Google Sheets has no SQL engine. The
QUERYfunction uses the Google Visualization API Query Language — SQL-like, withselect,where,group by,order by,limitandpivot. source -
It has no joins and no subqueries, and columns are referenced by letter (
A,B) rather than by header name. source - Over a REST endpoint the equivalent is query parameters — filter, sort, paginate and aggregate by column name — which is what most people mean when they ask to "query a sheet with SQL".
Can you query Google Sheets with SQL?
Almost. Google Sheets has a QUERY() function that takes a SQL-like language — it supports SELECT, WHERE, GROUP BY, and ORDER BY, but it is not real SQL: there are no joins, no subqueries, and it only runs inside a cell, not over an API.
So there are two ways to "query a sheet with SQL", and which one you want depends on where the answer needs to end up. Inside the spreadsheet, use QUERY(). From an app, a website, or an AI agent, put an API in front of the sheet and filter over HTTP instead.
Option 1: the QUERY() function
Google Sheets ships a QUERY() function that uses a SQL-like syntax right inside a cell:
=QUERY(A1:D, "SELECT A, B WHERE C = 'active' ORDER BY D DESC LIMIT 10")This is perfect for computed views within the spreadsheet. It cannot, however, serve data to your app or an AI agent — it only writes results into another range.
Option 2: query over a REST API
To query from code, put PasteSheet in front of the sheet and use URL parameters as your query:
# WHERE status = 'active' ORDER BY created DESC LIMIT 10
curl 'https://pastesheet.com/api/your-endpoint-id?status=active&sort=created&order=desc&limit=10'The mapping is direct: exact filters are your WHERE, sort/order is ORDER BY, and limit/offset is LIMIT/OFFSET. For grouped totals, see group by and aggregation; for sorting details, see order by.
SQL, QUERY(), and the API side by side
The same query expressed three ways — in SQL, in the QUERY() function, and as PasteSheet URL parameters:
| SQL | QUERY() clause | PasteSheet API |
|---|---|---|
SELECT a, b |
select A, B |
Columns set by your column config |
WHERE col = 'x' |
where C = 'x' |
?col=x |
WHERE col LIKE 'x%' |
where C starts with 'x' |
?contains[col]=x |
ORDER BY col DESC |
order by D desc |
?sort=col&order=desc |
GROUP BY col |
group by A |
?group_by=col&sum=… |
LIMIT 10 OFFSET 20 |
limit 10 offset 20 |
?limit=10&offset=20 |
What QUERY() cannot do
Neither option is full SQL. There are no joins and no subqueries — a Google Sheet is a single flat table, not a relational schema — and QUERY() references columns by letter (A, B) rather than by header name, so inserting a column shifts what your formula reads. If you need joins across tables, that is the signal to move to a real database.
Frequently asked questions
Can you query Google Sheets with SQL?
Yes. Use the built-in QUERY() function for SQL-like queries inside the sheet, or a REST API to run SQL-style filters, sorting, and grouping from your app.
What is the QUERY() function?
A Google Sheets formula that runs a SQL-like statement (SELECT, WHERE, ORDER BY, GROUP BY) over a range and writes the result into the sheet.
How do I query a sheet from my app?
Expose it via PasteSheet and pass query parameters — filters map to WHERE, sort/order to ORDER BY, and group_by/count/sum/avg to GROUP BY.
Can I do JOINs in a Google Sheets query?
No. The QUERY() function and the Google Visualization Query Language have no JOIN or subquery support — a sheet is a single flat table. For joins across tables, use a real database.
Is the QUERY() function case-sensitive?
Yes, its string comparisons are case-sensitive; wrap a column in LOWER() to match regardless of case. Over the PasteSheet API, filters match by column name.
Sources
- Query Language Reference (Google Visualization API) — Google
- QUERY function — Google Docs Editors Help
Related guides
Google Sheets Query: Sort & Order By
Sort Google Sheets query results by any column, ascending or descending, including by date. Use the QUERY() function or sort and order URL parameters.
Google Sheets Query: Group By & Totals
Group Google Sheets rows and compute totals: count, sum, and average by any column. Use the QUERY() function in-sheet or group_by aggregation over a REST API.
Google Sheets Query: Starts With Filter
Filter Google Sheets rows by a starts-with or partial text match. Use the QUERY() function's STARTS WITH, or the contains and search parameters over a REST API.
Turn Google Sheets Into a REST API (No Code)
Turn a public or restricted Google Sheet into a live JSON REST API with filtering, sorting, and pagination — no backend, no code.
Turn your sheet into an API in minutes
Paste a Google Sheet URL and get a live REST API and MCP server — no backend, no code, free to start.