CartaTerra

← Guides & help

SQL querying

9 min read

SQL lets you ask your layers questions in plain database language, right in your browser. Each layer on your map becomes a table, and CartaTerra runs your query with DuckDB inside your browser, so your layers stay on your device while it runs. SQL opens from the side rail into the bottom dock. It is part of Pro (see plans).

Open SQL

  1. Sign in with Pro and open your own map.
  2. Add the layers you want to query. Put them on your map first, from Add layer or with Upload.
  3. Press SQL on the side rail, in the data tools section. If you've hidden it under + › Show on rail, turn it back on there. It opens as a bottom-dock tab. Press SQL again while that tab is showing to close it.

The SQL window has your layers and their fields as buttons, a box for your query and a Run button. The results show as a table under the box. The first run loads the query engine ("Loading the query engine…"), which can take a moment.

The chips around the box

See our how-to on SQL querying under the box opens this page.

Tables and columns

Table names. Each table is named after its layer, in lowercase, with every run of spaces and symbols turned into one underscore. Accents are dropped.

A name that would start with a number gets layer_ in front, and two layers with the same name become name and name_2. A name that's also an SQL word gets an underscore at the end. The Layers chip always inserts the exact name, so you don't have to work it out.

Columns. Each field in a layer is a column, named the same way: "notable people" becomes notable_people. Every table also has a last column, geom, which holds the shape of each row: a point, line or area, in longitude and latitude. Numbers, text, true or false and dates come through as those types.

Which layers. Layers from Add from a link (live map services) aren't tables. Very large built-in layers that the map draws in tiles appear as tables with no rows, and they don't get a Layers chip. Your own, uploaded and analysis layers, and built-in layers that load in full, have all their rows.

What you can run. One SELECT at a time. A query can also start with WITH for a named step. You can use WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, JOIN, CASE, text tools such as ILIKE and regexp_extract, and math. Queries that change data or read files are refused.

Worked examples

These use built-in layers. Add them to your map first, then paste a query and press Run.

1. Filter rows (WHERE). Level I trauma centers in Colorado, from Hospitals:

SELECT name, city, beds, trauma FROM hospitals WHERE state = 'CO' AND trauma = 'Level I'

Text goes in single quotes and has to match exactly. For a loose match, use ILIKE with % around the words: WHERE name ILIKE '%children%'.

2. Count by a field (GROUP BY). Power plants by type:

SELECT type, COUNT(*) AS plants FROM power_plants GROUP BY type ORDER BY plants DESC

3. Sort and take the top 10 (ORDER BY, LIMIT). The highest SNOTEL snow stations:

SELECT name, state, elevation_ft FROM snotel_snow_stations ORDER BY elevation_ft DESC LIMIT 10

4. Math on columns. Lock chamber size in square feet, and lift in meters, from Mississippi Locks & Dams:

SELECT name, town, state, main_chamber_length_ft * main_chamber_width_ft AS chamber_sq_ft, ROUND(lift_ft / 3.281, 1) AS lift_m FROM mississippi_locks_dams ORDER BY chamber_sq_ft DESC LIMIT 10

5. Join two layers by a field. Hospitals per state, with the state's full name from States (Census). Hospitals stores the two-letter code, and States (Census) has it as postal_code:

SELECT s.name AS state_name, COUNT(*) AS hospitals, ROUND(AVG(h.beds)) AS avg_beds FROM hospitals h JOIN states_census s ON h.state = s.postal_code GROUP BY s.name ORDER BY hospitals DESC

The short names after each table (h and s) save typing. Use them in front of any column both tables share, like name.

6. Join two layers by location (points inside areas). SNOTEL stations inside national park units, highest first:

SELECT n.name AS park, s.name AS station, s.elevation_ft, s.geom FROM snotel_snow_stations s JOIN national_park_service_sites n ON ST_Within(s.geom, n.geom) ORDER BY s.elevation_ft DESC

Because it keeps s.geom, you can save the result as a layer of just those stations.

7. Count points in each area, and map it. Superfund sites per state, with each state's shape kept so the result can go on the map:

SELECT st.name AS state_name, COUNT(*) AS sites, st.geom FROM states_census st JOIN superfund_sites_npl f ON ST_Within(f.geom, st.geom) GROUP BY st.name, st.geom ORDER BY sites DESC

Save it as a layer, then use Style to color the states by sites.

Area, length, distance and inside

With geom you can measure shapes and compare places. The Functions chips use these same functions on the layer you picked (Distance there measures to the nearest row of another open layer, or to the first row's point, which you can change):

Two things to know:

ST_Centroid turns an area into its center point: SELECT name, ST_Centroid(geom) AS geom FROM national_park_service_sites gives a point for each park unit.

Save and export results

Common errors

Related: Data tools on the map · Upload and export map files · What's in this area? · All guides