Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Filter by Categories
ArcGIS Pro

Using the IN operator in QGIS

How to use the IN operator in QGIS

In QGIS, the IN operator is used in expressions, particularly when you need to test if a value exists within a set of values. This is particularly useful in filtering data, styling layers, or defining conditional expressions in the Field Calculator.

Working in QGIS? You should be listening to our podcast!

Here’s how you can use the IN operator in different scenarios in QGIS:

1. Filtering Features

You can use the IN operator to filter features within a layer based on the values in a specific field. For instance, if you have a layer of locations with a field called “Type,” and you want to display only the features where the type is either “Restaurant,” “Cafe,” or “Bar,” you can set a filter expression like:

"Type" IN ('Restaurant', 'Cafe', 'Bar')

To apply this filter:

  • Right-click on the layer in the ‘Layers’ panel.
  • Choose Filter... from the context menu.
  • Enter the expression in the query builder and apply it.

2. Using IN Operator in Field Calculator

The IN operator can be used in the Field Calculator to create a new field based on conditional checks against a set of values. For example, to create a new field that checks if the “Type” of each feature is one of several categories and returns a specific value if true:

  1. Open the Field Calculator.
  2. Create a new field or update an existing one.
  3. Enter an expression like:
   CASE WHEN "Type" IN ('Restaurant', 'Cafe', 'Bar') THEN 'Food and Drink' ELSE 'Other' END

3. Styling Based on Multiple Conditions

You can also use the IN operator to apply a style to features that match any of the values in a list. This is useful in categorized or rule-based styling.

  • Open the ‘Layer Properties’.
  • Go to the ‘Symbology’ tab.
  • Choose ‘Categorized’ or ‘Rule-based’ from the style type drop-down.
  • For rule-based, enter expressions similar to the filtering example for each rule.

4. Selecting Features

You can use the IN operator in the ‘Select by Expression’ tool to select multiple features that match the criteria.

  • Open the ‘Select by Expression’ dialog from the ‘Attribute Table’ or the toolbar.
  • Enter the expression similar to the filter example.
  • Click ‘Select’ to apply.

Using the IN operator in QGIS expressions can simplify queries that would otherwise require multiple OR conditions, making your expressions cleaner and more efficient. Here’s a practical comparison to illustrate how IN can replace multiple OR statements:

Scenario: Filtering Data Based on Multiple Values

Using Multiple OR Statements

Suppose you want to filter a dataset to include only records where the “Type” field is either “Restaurant,” “Cafe,” or “Bar.” Using OR statements, your expression would look like this:

"Type" = 'Restaurant' OR "Type" = 'Cafe' OR "Type" = 'Bar'

This method works, but it gets cumbersome and error-prone as you add more conditions.

Using the IN Operator

The same filter can be written more succinctly with the IN operator:

"Type" IN ('Restaurant', 'Cafe', 'Bar')

This expression is much easier to read and maintain, especially as the list of conditions grows. For instance, if you wanted to include more types like “Bakery” and “Bistro,” the IN version simply extends like this:

"Type" IN ('Restaurant', 'Cafe', 'Bar', 'Bakery', 'Bistro')


Using IN instead of multiple OR statements not only simplifies your expressions but also helps keep your project organized and easy to understand. This is particularly beneficial when dealing with a large number of conditions that need to be checked against a single field value.

Frequently asked questions about the IN operator in QGIS:

1. What is the IN operator and how does it work in QGIS?

The IN operator in QGIS is used within expressions to check if a value exists within a list of values. It simplifies queries by replacing multiple OR conditions with a single statement. For example, instead of writing "Type" = 'Restaurant' OR "Type" = 'Cafe' OR "Type" = 'Bar', you can use "Type" IN ('Restaurant', 'Cafe', 'Bar'). This operator checks if the value of the “Type” field matches any value in the list provided.

2. Can the IN operator be used with numerical data as well as text?

Yes, the IN operator can be used with both numerical and textual data in QGIS. It is versatile and can handle any data type that can be compared. For instance, if filtering a numerical field like “ID” that should match any of several specific numbers, you could use "ID" IN (101, 102, 103).

3. How does the IN operator improve the performance of queries in QGIS?

Using the IN operator can enhance the readability and maintenance of your code, which indirectly contributes to performance by reducing the chance of errors and simplifying the query logic. However, the actual execution performance compared to multiple OR statements typically remains similar since the underlying SQL engine optimizes both expressions in a similar manner.

4. What are the limitations of the IN operator in QGIS?

The IN operator can become inefficient if used with a very large list of values, as it may slow down query processing times. Additionally, it cannot be used to pattern match within strings; for that, operators like LIKE or regex functions are required.

5. How can the IN operator be combined with other SQL functions in QGIS?

The IN operator can be effectively combined with other SQL functions and operators to create complex expressions. For example, you might exclude certain values with NOT IN, combine conditions with AND, or use LIKE for pattern matching in conjunction with IN for specific value checks, such as:

"Type" IN ('Restaurant', 'Cafe') AND "Name" LIKE 'Star%'

6. Are there any tips for troubleshooting common errors when using the IN operator in QGIS?

Common errors when using the IN operator include syntax mistakes, such as forgetting parentheses or quotes around string values. Always ensure that the data types in the IN list match the field’s data type. If errors persist, check the expression in the Query Builder or Expression dialog, which provides feedback and syntax highlighting.

7. Can the IN operator be used in the Field Calculator for creating or updating fields based on multiple criteria?

Absolutely! The IN operator is ideal for use in the Field Calculator to assign values to new or existing fields based on multiple criteria. For example, you might want to label certain feature types with a specific category:

CASE WHEN "Type" IN ('Restaurant', 'Cafe', 'Bar') THEN 'Food and Beverage' ELSE 'Other' END

8. How can I use the IN operator for filtering data in QGIS plugins or custom scripts?

In plugins or custom scripts, the IN operator can be used within SQL queries or layer filtering expressions to manage data dynamically. For example, using Python and the PyQGIS library, you can set a layer’s filter with:

layer.setSubsetString("\"Type\" IN ('Restaurant', 'Cafe', 'Bar')")

9. What are some examples of advanced expressions using the IN operator in QGIS for data analysis?

Advanced use cases include using the IN operator within statistical or spatial analysis queries to group and analyze subsets of data. For instance, calculating the average area of certain land use types:

sum("Area" / "Total", "LandUse" IN ('Residential', 'Commercial'))

This expression could be part of a larger analysis determining how land use affects various ecological or urban factors.

About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.