eyecatch

Filter Features by Attribute Values in QGIS

Published: Last updated:
This article uses QGIS 3.34. The current LTR is 3.44.

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.

All prefectures are displayed before any filter is applied (Global Map Japan, GSI)
Global Map Japan (Source: GSI)

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

Check the attributes of the layer you want to filter.
Check the attributes of the layer you want to filter.

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.

Filter settings dialog
Filter settings dialog
  1. 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.
  2. Click All. The attribute values in nam appear as candidate values. The values that All lists come from the field currently selected under Fields on the left.
  3. Click = in the operators.
  4. 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).

Only Hokkai Do is displayed. (Adapted from Global Map Japan, GSI)
Only Hokkai Do is displayed. (Adapted from Global Map Japan, GSI)

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.

  1. In the field list, double-click pop.
  2. Click >= in the operators.
  3. Type 100000 (half-width digits) directly in the filter expression, and click OK.
Filter settings for a numeric value
Filter settings for a numeric value
"pop" >= 100000

The map shows only the features whose pop value is 100,000 or more.

Filter result

Features with a pop value of 100000 or more are displayed. (Adapted from Global Map Japan, GSI)
Features with a pop value of 100000 or more are displayed. (Adapted from Global Map Japan, GSI)

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 expression using AND
Filter expression using AND

Filter result

Features where nam is Hokkai Do and pop is 100000 or more are displayed. (Adapted from Global Map Japan, GSI)
Features where nam is Hokkai Do and pop is 100000 or more are displayed. (Adapted from Global Map Japan, GSI)

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 expression using OR
Filter expression using OR

Filter result

Only Hokkai Do and Osaka Fu are displayed. (Adapted from Global Map Japan, GSI)
Only Hokkai Do and Osaka Fu are displayed. (Adapted from Global Map Japan, GSI)

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

Only Hokkai Do, Aomori Ken, Akita Ken, and Iwate Ken are displayed. (Adapted from Global Map Japan, GSI)
Only Hokkai Do, Aomori Ken, Akita Ken, and Iwate Ken are displayed. (Adapted from Global Map Japan, GSI)

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

Everything except Hokkai Do, Aomori Ken, Akita Ken, and Iwate Ken is displayed. (Adapted from Global Map Japan, GSI)
Everything except Hokkai Do, Aomori Ken, Akita Ken, and Iwate Ken is displayed. (Adapted from Global Map Japan, GSI)

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

Everything except Hokkai Do is displayed. (Adapted from Global Map Japan, GSI)
Everything except Hokkai Do is displayed. (Adapted from Global Map Japan, GSI)

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

Only Fukui Ken is displayed. (Adapted from Global Map Japan, GSI)
Only Fukui Ken is displayed. (Adapted from Global Map Japan, GSI)

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

Only Toyama Ken is displayed. (Adapted from Global Map Japan, GSI)
Only Toyama Ken is displayed. (Adapted from Global Map Japan, GSI)

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

Okayama Ken and Wakayama Ken are displayed in addition to Toyama Ken. (Adapted from Global Map Japan, GSI)
Okayama Ken and Wakayama Ken are displayed in addition to Toyama Ken. (Adapted from Global Map Japan, GSI)

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

Yamagata Ken, Yamanashi Ken, and Yamaguchi Ken are displayed. (Adapted from Global Map Japan, GSI)
Yamagata Ken, Yamanashi Ken, and Yamaguchi Ken are displayed. (Adapted from Global Map Japan, GSI)

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

Fukushima Ken, Hiroshima Ken, Tokushima Ken, and Kagoshima Ken are displayed. (Adapted from Global Map Japan, GSI)
Fukushima Ken, Hiroshima Ken, Tokushima Ken, and Kagoshima Ken are displayed. (Adapted from Global Map Japan, GSI)

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

Fukushima Ken, Shimane Ken, Hiroshima Ken, Tokushima Ken, and Kagoshima Ken are displayed. (Adapted from Global Map Japan, GSI)
Fukushima Ken, Shimane Ken, Hiroshima Ken, Tokushima Ken, and Kagoshima Ken are displayed. (Adapted from Global Map Japan, GSI)

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.

If not all candidates appear under “Values,” select “Use unfiltered layer”
If not all candidates appear under “Values,” select “Use unfiltered layer”

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.

Running a test. In this case, 0 features match the filter expression.
Running a test. In this case, 0 features match the filter expression.

Conclusion

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

About the author
QGIS LAB Editorial Team
QGIS LAB Editorial Team

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.