Find a course

Module 4: Spatial AnalysisLesson 7 of 1115 min

Attribute and Spatial Queries

Select features by their attributes with SQL-style expressions and by their location with spatial relationships such as within, intersects and nearest.

Step 1 of 13Learn

Asking questions of an attribute table

Every vector layer has an attribute table: one row per feature, one column per field. An attribute query selects features whose attributes meet conditions, much like filtering a spreadsheet.

Most GIS software uses expressions based on SQL (Structured Query Language). For a parcels layer, examples include:

  • zone = 'residential'
  • area_ha > 2
  • zone = 'residential' AND area_ha > 2
  • land_use IN ('park', 'forest')
  • owner LIKE '%County%'

Key operators:

  • Comparison: =, <>, <, >, <=, >=
  • Logical: AND (both conditions must be true), OR (either one), NOT
  • IN for a list of values, LIKE for text patterns, IS NULL for missing values.

AND narrows a selection; OR widens it. A classic mistake: "zone = 'residential' OR zone = 'commercial' AND area_ha > 2" may not mean what you think, because AND is usually evaluated before OR. Use parentheses: "(zone = 'residential' OR zone = 'commercial') AND area_ha > 2."

Text values need quotes; numbers do not. Text comparisons can be case-sensitive, so 'Residential' and 'residential' may not match. Checking the unique values in a field before querying avoids many surprises.

Attribute queries feed almost every analysis: you select the features you care about, then measure, buffer, overlay or summarize them.

Step 2 of 13Compare

Two ways to select features

AspectAttribute querySpatial query
Asks aboutValues in the tableWhere features are relative to others
ToolSelect by expression (SQL-style)Select by location
Examplezone = 'residential' AND area_ha > 2Bus stops relative to a park layer
LikeFiltering a spreadsheetSomething a spreadsheet cannot do

Step 3 of 13Practice · Code challenge · SQL

A parcels table stores each parcel's id, zone and area in hectares. Write a query that returns the id of every parcel that is zoned 'residential' and larger than 2 hectares.

The database for this exercise (tables and rows)
CREATE TABLE parcels (id INTEGER PRIMARY KEY, zone TEXT, area_ha REAL, value INTEGER);
INSERT INTO parcels VALUES (1,'residential',1.5,320000),(2,'residential',2.8,410000),(3,'commercial',3.2,900000),(4,'residential',4.1,520000),(5,'park',6.0,0),(6,'residential',2.0,300000),(7,'commercial',1.1,450000);
Real SQLite, running in your browser on a fresh copy of the database.
  • Result matches the expected table

Step 4 of 13Tap to reveal

Query building blocks with examples

  1. Comparison operators

    population > 50000 selects places with more than 50,000 people; land_use <> 'water' selects everything that is not water. Use >= when the boundary value itself should be included.

  2. AND and OR

    zone = 'commercial' AND vacant = 1 finds vacant commercial parcels (both conditions). type = 'school' OR type = 'library' finds either kind of building. AND makes selections smaller; OR makes them larger.

  3. IN

    county IN ('Adams', 'Boulder', 'Denver') is a compact way to write several OR conditions on one field.

  4. LIKE

    name LIKE '%Park%' finds names containing "Park" anywhere. The % sign stands for any sequence of characters. Matching can be case-sensitive depending on the software.

  5. IS NULL

    year_built IS NULL finds features with missing values. Writing year_built = NULL does not work in SQL, because missing values are not equal to anything, even each other.

Step 5 of 13Learn

Spatial queries: selecting by location

A spatial query (often called "select by location") selects features based on their geometric relationship to other features. This is where GIS goes beyond a spreadsheet.

Common spatial relationships:

  • Intersects: features that touch or overlap in any way. Which parcels intersect the flood zone?
  • Within / completely within: features entirely inside another. Which schools are within the city limits?
  • Contains: the reverse of within. Which counties contain a national park?
  • Within a distance of: features inside a set distance. Which homes are within 300 m of the planned highway?
  • Touches / shares a boundary: features that meet at edges without overlapping. Which counties border this one?
  • Nearest: the closest feature. Which fire station is nearest to each building?

The difference between intersects and within matters. A parcel that is half inside a flood zone intersects it but is not within it. For a flood-risk notice, you would want intersects; for counting parcels entirely protected by a levee district, you might want within.

Spatial queries combine with attribute queries. "Select hospitals with more than 200 beds (attribute) within 10 km of the earthquake epicenter (spatial)." Many GIS programs let you build such selections step by step: create a new selection, then add to it, remove from it or select from the current selection.

Behind the scenes, spatial databases use spatial indexes so these queries run fast even on millions of features.

Step 6 of 13Practice · Match the pairs

Match each question with the spatial relationship it uses.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. Which parcels overlap the 100-year flood zone at all?
  2. Which wells are entirely inside the county boundary?
  3. Which states share a border with Colorado?
  4. Which homes are within 500 m of a school?
  5. Which ambulance base is closest to the crash?

Step 7 of 13Practice · True or false

A parcel that lies partly inside a wetland boundary will be selected by a "completely within" query on the wetland layer.

Step 8 of 13Note

Step 9 of 13Practice · Diagram

Vulnerable AND exposed

Tap the area holding the homes the team will count.

Tap the shape that answers the question, then press Check.

Vulnerable AND exposedVenn diagram: Vulnerable, Exposed, Vulnerable only, Exposed only, Exposed + Vulnerable (overlap).VulnerableExposed• Old wood roofs in town• Newer homes by the forest• High-risk homes
Vulnerable

Attribute query: roof_material = 'wood' AND year_built < 1990.

Exposed

Spatial query: within 100 m of the forest layer.

Vulnerable only
  • Old wood roofs in town
Exposed only
  • Newer homes by the forest
Exposed + Vulnerable (overlap)

Only these are counted per fire district.

  • High-risk homes

Step 10 of 13Note

Step 11 of 13Practice · Spot the mistake

Click the condition that makes this query return parcels of any zone larger than 5 ha, not just residential or commercial ones.

Step 12 of 13Practice · What would you do?

What would you do?

A planner wants to notify owners of every property that is touched by a new 50 m wide road corridor polygon. Which query should she use?

Step 13 of 13Practice · Choose one

Which expression selects parcels zoned either park or forest?