Filter features with definition queries

When you add a dataset as a layer to a map or scene, sometimes you only want to work with and draw a subset of the features in the dataset. Examples include:

  • From a dataset of world cities, working with only those with a population greater than 1 million.
  • From a dataset of hydrology lines, working with only perennial streams.
  • From a dataset of parcels, working with only those that are zoned commercial.

Definition queries allow you to define a subset of features to work with in a layer by filtering which features are retrieved from the dataset by the layer. That means that a definition query impacts not only drawing, but also which features appear in the layer's attribute table, can be selected, labeled, identified, and processed by geoprocessing tools.

If you want to limit which features are drawn, but retain all features from querying and processing, consider using display filters instead. Definition queries and display filters can be used together on a layer, but remember that definition queries limit the features available in the layer, while display filter only limit which features are displayed.

Definition queries are comprised of one or more clauses. They are written in SQL syntax, constructed using the query builder. See Write a query in the query builder to learn how to work with clauses and queries and how to save and load queries from files.

A layer can have multiple definition queries associated with it but only one definition query can be active at a time. Even with multiple definition queries defined for a layer, all can be inactive so that no definition query is applied.

Note:

This topic outlines definition queries on a feature layer, but definition queries can be used in the same way on stand-alone tables.

Create a definition query for a layer

To create a definition query on a layer, follow these steps:

  1. Ensure the layer is selected in the Contents pane.
  2. Under Feature Layer, on the Data tab, in the Definition Query group, click Launcher to open the Layer Properties dialog box with its Definition Query tab open.
  3. Click New definition query.

    A new query is created, with a default name and a single, unfinished clause.

  4. Choose values from the menus to construct the clause. These menus and their values are specific to the underlying source data.
  5. Optionally click Add Clause Add New Clause to add another clause to the query. Choose values from the menus in the new clause as required. Repeat this step to add additional clauses as necessary.
  6. Optionally hover over the query name and click to rename it.
  7. Optionally click Verify the SQL expression is valid Verify the SQL expression is valid to verify the query.
  8. To verify and finalize the query, click Apply.
  9. If this is the only definition query on the layer, it becomes the active definition query, and is immediately applied. If there are other definition queries on the layer, click Set as Active Set as Active to apply a definition query to the layer.

Set the active definition query

You can define more than one definition query for a layer or table, and switch between them. Only one is active at any time, or you can have no queries active. The definition queries are stored as a property of the layer.

To switch between active definition queries, follow these steps:

  1. Ensure that the layer is selected in the Contents pane.
  2. Under Feature Layer, on the Data tab, in the Definition Query group, choose a query from the Definition Query menu to apply to the layer.
  3. Alternatively, you can set the active query from the Layer Properties dialog box.
    • Click Set as Active Set as Active to apply that query to the layer. Any other queries become inactive. The active query is named at the bottom of the Properties dialog box. If you have many queries listed and the active query is out of view, click Show the active definition query Show the active definition query to scroll to it.
    • Click Clear active query Clear active query to have no query applied to the layer or table. All queries are retained by the layer or table, but none of them are currently applied.

Modify existing queries

With the layer selected in the Contents pane, under Feature Layer, on the Data tab, in the Definition Query group, click Launcher to open the Layer Properties dialog box with its Definition Query tab open. To modify a definition query, hover over the query and follow any of these actions:

  • To modify, add, or remove clauses from the definition query, click Edit in the query's cell.
    • To edit a clause within a query, adjust values as necessary.
    • To remove a clause from the query, click Remove Clause Remove Clause.
    • To add a clause to the query, click Add Clause Add New Clause.

    Click Apply to apply the changes to the definition query.

  • To remove the definition query from the layer or table, hover over the definition query cell and click Remove defintion query Remove .
  • To rename a definition query, click the name of a definition query and type a new name.
  • To export the query syntax to a query expression file (.exp file), click Save Save.
  • To replace the current query with a different query from a query expression file (.exp file ), click Load Load Query.

Set definition queries on multiple layers at once

If multiple layers share definition queries with a common name, even if the content of the queries are not the same, the queries can be activated for the layers simultaneously. This means you can construct a number of related queries for a certain scenario and apply them all at once. You can then switch to a different set of queries to satisfy a different scenario, if necessary.

To apply definition queries with a common name to multiple layers simultaneously, follow these steps:

  1. Ensure the layers are selected in the Contents pane.
  2. Under Feature Layer, on the Data tab, in the Definition Query group, choose a query from the Definition Query menu to apply to the layer. Only definition queries with a name common to all selected layers are shown.

Sharing layers with definition queries

When a layer is shared as a feature layer or within a map image layer, only the active definition query is retained and honored. All other inactive definition queries are dropped.

Related topics