
Filter Features by Attribute Values in QGIS
What you'll learn
- How to filter vector data
- How to use filter expressions
Recommended for
- Anyone who wants to narrow down a vector layer by attribute values
- Anyone who wants to display only the features they need
Introduction
When you visualize map data, have you ever wanted to display only the features that meet a specific condition? For example, “show only the polygons of a specific municipality from prefecture polygon data” or “show only the prefectures with a population of at least a certain number.”
In these cases, the QGIS filter feature lets you extract only the features with specific values, based on the attribute values of the vector data, and display them.
This article introduces the filter feature for vector data.
The data used in this article
This article applies various filters to the administrative boundary data from Global Map Japan (Geospatial Information Authority of Japan, GSI). When you add the data to QGIS, the municipalities of all of Japan appear.

Before you apply a filter, check the attributes of the data you added.

In the attribute table, the nam field (third column) holds prefecture names, laa (fourth column) holds municipality names, and pop (fifth column) holds population values.
How to use filters
Filter by text
First, filter the layer to show only the features whose nam field is Hokkai Do.
Right-click the vector layer you want to filter in the Layers panel and select Filter. The Query Builder dialog opens. The Fields list at the top left shows the field names of the layer.

- In the field list, double-click nam, which holds the prefecture names. QGIS enters
"nam"in the filter expression box at the bottom of the dialog. - Click All. The attribute values in
namappear as candidate values. The values that All lists come from the field currently selected under Fields on the left. - Click = in the operators.
- In the Values list, double-click Hokkai Do.

"nam" = 'Hokkai Do'
Check that the filter expression looks like the one above, then click OK. The map narrows down to the matching features.
The attribute table also shows only the features whose nam is Hokkai Do (in this article, the data is overlaid on a blank map of Japan so that the filtering is easy to see).

Filter by numbers
Next, display only the features whose pop field (population) is 100,000 or more. Open the filter again and delete all the text in the filter expression.
- In the field list, double-click pop.
- Click >= in the operators.
- Type
100000(half-width digits) directly in the filter expression, and click OK.

"pop" >= 100000
The map shows only the features whose pop value is 100,000 or more.
Filter result

Various filter expressions
Show features that meet all of two or more conditions (AND)
To filter with two or more conditions, such as “nam is Hokkai Do” and “pop is 100,000 or more,” join the expressions with AND.
"nam" = 'Hokkai Do' AND "pop" >= 100000

Filter result

Show features that meet either condition (OR)
To display the features that meet at least one of several conditions, use OR.
"nam" = 'Hokkai Do' OR "nam" = 'Osaka Fu'

Filter result

Specify multiple values at once (IN, NOT IN)
To display the features whose nam is Hokkai Do, Aomori Ken, Akita Ken, or Iwate Ken, you can join the conditions with OR, like this:
"nam" = 'Hokkai Do' OR "nam" = 'Aomori Ken' OR "nam" = 'Akita Ken' OR "nam" = 'Iwate Ken'
This makes the expression very long, though. To display features with any of several values, use IN instead:
"nam" IN ('Hokkai Do','Aomori Ken','Akita Ken','Iwate Ken')
Filter result

NOT IN returns the features whose value is not one of the specified values.
"nam" NOT IN ('Hokkai Do','Aomori Ken','Akita Ken','Iwate Ken')
Filter result

Show features that do not meet a condition (NOT, !=, <>)
To display all features except those that meet a specific condition, such as nam being Hokkai Do, build the expression with NOT, !=, or <>.
Using NOT:
NOT "nam" = 'Hokkai Do'
Using !=:
"nam" != 'Hokkai Do'
Using <>:
"nam" <> 'Hokkai Do'
All three give the same result.
Filter result

Search for text patterns (LIKE)
To search for features that contain certain characters, use LIKE. Combine it with the wildcard _ (underscore) or % to specify the condition.
_ stands for one unspecified character.
"nam" LIKE 'Fuku_ Ken'
The expression above matches names made of Fuku, one unspecified character, and Ken. In this case, only Fukui Ken matches.
Filter result

The condition allows as many unspecified characters as there are underscores. With two underscores, as below, only Toyama Ken matches. Wakayama Ken and Okayama Ken do not.
"nam" LIKE '__yama Ken'
Filter result

To match any number of unspecified characters, use %. The expression below returns the features whose name has some characters before “yama Ken.”
"nam" LIKE '%yama Ken'
Filter result

Putting % after a string returns the features that start with that string. The expression below returns the features that start with “Yama.”
"nam" LIKE 'Yama%'
Filter result

Putting % before and after a string returns the features that contain that string anywhere. The expression below returns the features that contain “shima” anywhere in the name, so it returns Fukushima Ken, Hiroshima Ken, Tokushima Ken, and Kagoshima Ken.
"nam" LIKE '%shima%'
Filter result

Search for text patterns, ignoring case (ILIKE)
ILIKE works the same way as LIKE, but it does not distinguish between uppercase and lowercase. The expression below returns the features that contain “shima,” “SHIMA,” “Shima,” or any other capitalization, so Shimane Ken is also included in the results.
"nam" ILIKE '%shima%'
Filter result

Frequently asked questions
Clicking All does not show all the candidate values
The candidate values that All lists come from the features left by the filter that is currently active. So while a filter is applied, the list does not include all the values in the layer. To get the candidate values from all features while a filter is applied, select the Use unfiltered layer checkbox and click All.

Save a filter expression for reuse
In the filter dialog, click Save to save the filter expression you are entering.
To open a saved filter expression, click Load.

All features disappear after applying a filter
After you enter a filter expression and click OK, all features may disappear. Most likely, either no feature meets the condition in your filter expression, or the expression is wrong.
If you click Test before you click OK, you can check whether the filter expression has any errors and how many features match it.
Use Test whenever you want to validate a filter expression you entered.

Conclusion
This article introduced various expressions you can use in filters. Try using filters to display the data you need efficiently.


QGIS LAB is a comprehensive information hub for QGIS, the open-source GIS software. Under the concept of “Geospatial for Greater Good,” we share the knowledge and skills to open up the world through location data.


