Sort and order Google Sheets results
Ordering rows is the most common thing you do after filtering. Whether you want it inside the sheet or from your app, here is how to sort a Google Sheets query — including by date.
Last updated
Key facts
-
In the
QUERYfunction,order bytakes column identifiers (A,B) and sorts ascending unless you adddesc. Clauses must also appear in a fixed order —select,where,group by,pivot,order by,limit. source -
QUERY()is a spreadsheet formula: its result spills into the cells below it. It cannot serve sorted rows to an app or an AI agent. source -
Over a REST endpoint the equivalent is
?sort=created&order=desc. Because PasteSheet infers column types when the sheet is connected, a date column sorts chronologically rather than as text — soorder=descgenuinely means newest-first.
ORDER BY in the QUERY() function
Inside the sheet, add an ORDER BY clause (use DESC for newest-first):
=QUERY(A1:D, "SELECT * ORDER BY D DESC")sort & order over the API
From your app, sort with the sort and order parameters:
# newest first
curl 'https://pastesheet.com/api/your-endpoint-id?sort=created&order=desc'
# order by date, oldest first
curl 'https://pastesheet.com/api/your-endpoint-id?sort=joined&order=asc'Ordering by date
Because PasteSheet infers column types, a date column sorts chronologically rather than as text — so order=desc on a date really means newest-first. Combine it with filters and paging for a full query, as covered in querying Google Sheets like SQL.
Frequently asked questions
How do I order a Google Sheets query?
Use ORDER BY in the QUERY() function inside the sheet, or the sort and order parameters (e.g. ?sort=created&order=desc) over the PasteSheet REST API.
How do I sort by date?
Sort on the date column. PasteSheet infers date types, so ordering is chronological — order=desc gives you newest-first.
Can I sort ascending and descending?
Yes. Use order=asc for ascending or order=desc for descending; QUERY() uses ASC/DESC.
Sources
- Query Language Reference (Google Visualization API) — Google
- QUERY function — Google Docs Editors Help
Related guides
How to Query Google Sheets With SQL
Query Google Sheets like SQL: the built-in QUERY() function for in-sheet queries, and a REST API for SQL-style filtering, sorting, and grouping from your app.
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 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.