Documentation → How-to guides
Query and model data with SQL
The SQL Workbench runs spatial SQL over the datasets you already have: joins, aggregates, buffers and filters. If you want to keep a query, save it as a model. Running a model produces a new dataset that you can tile and map like any other.
Queries and models
| A query | A model | |
|---|---|---|
| gives you | rows, on screen | a new dataset, tiled and mappable |
| creates | nothing | a dataset you can map, share and query again |
| costs | a moment | a tiling job |
| needs geometry | no | yes, exactly one geometry column |
| use it to | check an assumption, count something, explore | keep a result and put it on a map |
Use queries to explore and models to keep results. A common mistake is running a tiling job to answer a question that a query could answer in a second.
Writing a query
- Open SQL Workbench in the sidebar.
- Add the datasets you want to query as inputs. Each one gets a short alias, which you use as its table name in your SQL.
- Write the query.
FROM parksreads the input with the aliasparks. - Choose Preview to run it and see the rows.
The schema browser beside the editor lists each input's columns, and the table tab shows real rows. Check field names there, because the names in published data often differ from what the source's documentation says.
The dialect
The SQL dialect is DuckDB with its spatial extension. You can use the
ST_* functions and DuckDB's own extras, such as
QUALIFY, GROUP BY ALL and SELECT * EXCLUDE (…).
- The geometry column is
geomon every input. - All data is in WGS84 (EPSG:4326), as longitude and latitude in degrees. mapbench converts each dataset to WGS84 when you import it, so joins across sources line up without reprojection.
- You rarely want measurements in degrees. Use
ST_Distance_Spherefor distances in metres, or project into a metric CRS for areas and buffers. If you reproject, passalways_xy := truetoST_Transform. Without it you get empty geometry and no error.
Reference datasets
Your operator may have loaded shared reference datasets, such as OpenStreetMap roads, waterways, buildings and points of interest. They appear in the catalogue alongside your own datasets. You can use them as model inputs or map layers without importing them, and they don't count towards your storage. They are read-only, so you can't edit or reprocess them.
Turning a query into a model
- Get the query returning what you want in the preview.
- Choose Run.
- Give the output dataset a name and a layer ID. The same rules apply as for any dataset, and the layer ID can't be changed later.
- Run it. A tiling job is queued. When the dataset is Ready, it behaves like any other dataset: you can style it, map it, share it, or use it as the input to another model.
Your result must have exactly one geometry column. A model
produces a map layer, and a layer has one geometry. If you join two spatial
inputs, select parks.geom instead of parks.*, or you
will get two geometry columns. In an aggregate, the geometry must either appear
in the GROUP BY or come from a function such as
ST_Union.
Re-running a model
Edit the SQL and run it again. If the model already has an output, running it replaces that dataset. The dataset keeps the same id, so its styling and map memberships carry over and published links keep working. mapbench warns you before it replaces the output.
Models don't run on a schedule, and they don't re-run when an input changes. When you refresh an input, re-run the models built on it.
Lineage
A model records its inputs, so each dataset it produces keeps a record of where it came from. This helps you work out later which of several similarly named layers a published map is drawing, and which upstream import broke it. Archived inputs still appear in the lineage, which is useful because an archived input is often the one you are looking for.
Limits
- A query returns at most 1000 rows. Use queries for checking and aggregating data. They aren't meant for exporting a dataset.
- Queries are limited to 60 per minute from the app, and 10 per minute from an API key or a connected agent.
- A query that runs too long or uses too much memory is stopped. Narrow it
with a
WHEREclause or a bounding box before you try again. - Your SQL never touches the database that holds your account and catalogue.
It runs in a sandbox over copies of the data files, with no file, network or
write access, so
ATTACH,COPYandread_csvon a path all fail.
Common problems
- A column in my result is entirely null
- mapbench flags this because it usually points to a mistake in the query, such as a mistyped field name in a left join or a reprojection that failed without an error. Check the field name in the schema browser before you assume the data is empty.
- A spatial join returns nothing
-
The two inputs probably don't overlap where you expect. Query the bounding box
of each one (
ST_Extent(geom)) and compare them. A dataset imported with the wrong CRS can end up in the Gulf of Guinea, and then the join correctly finds nothing. - Distances and areas are tiny decimals
- You measured in degrees. See the note on WGS84 above.
- “The model result must have exactly one geometry column”
-
You selected
*across two spatial inputs. Name the geometry column you want and leave out the other. - The preview looks right but the tiled dataset is missing features
-
The preview shows the first rows of your result, but the model runs the whole
query. If the counts disagree, compare
SELECT count(*)with the tiled dataset's feature count before you look for a tiling problem.
See also
- Your first map: what to do with the output.
- Connect Claude to mapbench: let an agent write and run these queries against your data.
- Formats, limits and quotas: all the limits in one place.