SQL for GIS¶
Goal: Get fluent in SQL for attribute queries — the language behind every "Select by Attributes" and Definition Query in ArcGIS Pro.
What you'll learn
- The SQL clauses you'll use 95% of the time
- The dialect ArcGIS Pro uses
- When to use SQL vs Python
This is the roadmap entry. The full chapter is in SQL for GIS.
Why SQL?¶
You don't need to be a database engineer. But every GIS analyst writes SQL WHERE clauses every day:
- "Select features where
STATE = 'GA'" - "Filter to incidents in the last 30 days"
- "Hide all polygons with population < 1000"
Master the WHERE clause and you've got 95% of GIS SQL.
The SQL you actually need¶
SELECT *
FROM cities
WHERE state = 'GA'
AND population > 50000
AND name LIKE 'A%'
ORDER BY population DESC;
Even ArcGIS Pro's Select by Attributes uses just the WHERE part:
Operators cheat sheet¶
| Operator | Meaning | Example |
|---|---|---|
=, <>, <, >, <=, >= | Compare | pop > 1000 |
AND, OR, NOT | Combine | state='GA' AND pop>1000 |
LIKE | Pattern match | name LIKE 'A%' |
IN (...) | Match a list | state IN ('GA','FL','SC') |
BETWEEN | Range | pop BETWEEN 1000 AND 50000 |
IS NULL / IS NOT NULL | Missing values | notes IS NULL |
→ Each one has a dedicated page in SQL for GIS.
ArcGIS Pro SQL dialects¶
ArcGIS Pro uses different SQL syntax depending on the data source:
| Source | Dialect |
|---|---|
| File geodatabase | Esri's own (close to SQL-92) |
| Shapefile / dBASE | Esri's own (close to SQL-92) |
| Enterprise geodatabase | Native to the underlying RDBMS (SQL Server, Oracle, PostgreSQL) |
The differences are small — quoting field names, date literals, and TOP vs LIMIT.
→ Full reference: Esri SQL reference
Spatial SQL (PostGIS)¶
Beyond attribute SQL, spatial SQL lets you do geoprocessing in the database:
-- All schools within 500 m of a fire station
SELECT s.name
FROM schools s
JOIN fire_stations f
ON ST_DWithin(s.geom, f.geom, 500);
PostGIS, Oracle Spatial, and SQL Server all support spatial SQL. PostGIS is the open-source standard and worth learning.
SQL vs Python¶
| Use SQL when… | Use Python when… |
|---|---|
| Filtering rows | Combining multiple steps |
| One-off queries in a definition query | Repeating a workflow |
| Working in a database | Working with files |
| Quick attribute math | Anything with branching logic |
In practice, you'll use both. SQL inside Python (gdf.query("pop > 1000")) is extremely common.
Practice¶
Try this
- Open a counties feature class.
- Use Select by Attributes with:
STATE_NAME = 'Georgia' AND POPULATION > 100000. - Now make a Definition Query:
LANDUSE LIKE 'Res%' AND LOTSIZE BETWEEN 5000 AND 20000. - Open the SQL for GIS section and walk through each operator with your data.
Next up¶
→ Remote Sensing Basics — work with satellite imagery.