Selecting Features Using Attribute Queries in QGIS
Credit: Youtube Channel “Statistics Canada”
When working with geographic data in QGIS, the ability to quickly isolate specific features based on their attribute values is essential for spatial analysis, map styling, and data quality checks. Attribute queries let you craft logical expressions that identify precisely the features you need, whether they belong to a particular category, fall within a numeric range, or satisfy a combination of conditions. Below is a detailed guide on how to use attribute queries in QGIS to select features, covering both simple and advanced use cases.
Why Select by Attribute?
- Data Exploration: Identify outliers or test assumptions about attribute distributions.
- Cartography: Style only a subset of features (e.g., color all forests green).
- Analysis Preparation: Clip layers to a specific subset, or export only the selected features for further processing.
- Quality Control: Find records that violate expected rules (e.g., missing values, negative area).
Basic Workflow: Select by Expression
- Open the layer containing the features in the Layers Panel.
- Right‑click the layer and choose Open Attribute Table.
- In the attribute table, click the Select features using an expression icon (the
ε
button). - In the Expression Builder, construct your query using field names and operators. Example:
"landuse" = 'residential'
. - Click Select to apply the query.
- Close the attribute table. The selected features will be highlighted on the map and listed in the Locator Panel (if enabled).
Common Operators
Operator | Meaning | Example |
---|---|---|
= | equals | "type" = 'road' |
!= | not equals | "status" != 'closed' |
< | less than | "length" < 1000 |
>= | greater or equal | "elevation" >= 300 |
LIKE | string pattern | "name" LIKE 'Main %' |
IN | in a set | "type" IN ('road', 'path') |
IS NULL | missing value | "height" IS NULL |
AND | logical conjunction | "type" = 'park' AND "capacity" > 100 |
OR | logical disjunction | ("type" = 'lake' OR "type" = 'river') |
NOT | negation | NOT ("type" = 'school') |
Advanced Query Techniques
Using Functions for Spatial Attributes
QGIS expression functions can compute geometry statistics or evaluate spatial relationships.
area($geometry) > 5000 AND
within($geometry, geom_from_wkt('POLYGON((...))'))
This selects features with an area larger than 5 000 m² that also lie within a specified polygon.
Support for Regular Expressions
When dealing with text attributes, regular expressions can capture complex patterns.
regexp_match("city_name", '^[A-Z]{2,}$')
Above, only cities whose names consist solely of 2 or more uppercase letters are selected.
Dynamic Filtering with Variables
Define a custom variable and use it in an expression.
set_variable('min_length', '2000')
length($geometry) > variable('min_length')
This creates a reusable parameter that can be changed without editing the expression.
Quick Filters and Layer Properties
For permanent selections that influence styling, use Query Builder on the layer.
- Right‑click the layer and open Properties.
- Navigate to the General tab and click Query Builder.
- Enter the same expression as before and click OK.
All features that satisfy the query are automatically selected, and any symbology rules can be set to apply only to them.
Exporting or Styling Selected Features
Once features are selected, you can export them:
- Right‑click the layer ➜ Export ➜ Save Selected Features As.
- Choose format (e.g., GeoPackage, Shapefile, GeoJSON).
For styling, open the Layer Styling panel, use a Layer Rendering rule that references the selection selected = true()
or a subset rule based on your query.
Tips & Tricks
- Use the Field Calculator first to create helper columns that simplify complex expressions.
- Enable Show on map in the Expression Builder to preview the result before executing the query.
- For large datasets, upgrade to a spatial database (PostGIS, Spatialite) and run expressions in SQL for performance gains.
- Store frequently used expressions as Macros by clicking the Save as Macro button in the Expression Builder.
- When dealing with multilingual datasets use the
ignorecase
function to ignore text case:
ignorecase("name") LIKE 'paris'
Conclusion
Attribute queries in QGIS provide a flexible, powerful mechanism to select features based on a rich set of conditions. Whether you're filtering a single field, building complex spatial expressions, or automating selections for styling, mastering the Expression Builder unlocks advanced GIS workflows. Experiment with the examples above, and adapt them to your specific data to streamline both analysis and map production.