Skip To Content

Select Layer By Attribute

Summary

Adds, updates, or removes a selection based on an attribute query.

Usage

  • If the input is a feature class or dataset path, this tool will automatically create and return a new layer with the result of the tool applied.

  • If the input's data source is a feature service, it is recommended that the underlying ArcGIS Server use standardized SQL queries.

  • If a definition query is present on the input, only the features or rows matching the definition query will be used in the selection.

  • When using is Above Average and is Below Average queries, the AVG function will always execute on the source data, even if the input layer is a subset of source data.

  • The Get Count tool can be used to determine the number of features or rows selected. This can be especially useful in a script or model to determine if further processing is desired.

Syntax

SelectLayerByAttribute_management (in_layer_or_view, {selection_type}, {where_clause}, {invert_where_clause})
ParameterExplanationData Type
in_layer_or_view

The data to which the selection will be applied.

Table View; Raster Layer; Mosaic Layer
selection_type
(Optional)

Determines how the selection will be applied and what to do if a selection already exists.

  • NEW_SELECTIONThe resulting selection replaces the current selection. This is the default.
  • ADD_TO_SELECTIONThe resulting selection is added to the current selection if one exists. If no selection exists, this is the same as the new selection option.
  • REMOVE_FROM_SELECTIONThe resulting selection is removed from the current selection. If no selection exists, this option has no effect.
  • SUBSET_SELECTIONThe resulting selection is combined with the current selection. Only records that are common to both remain selected.
  • SWITCH_SELECTIONSwitches the selection. All records that were selected are removed from the current selection; all records that were not selected are added to the current selection. The Expression parameter (where_clause in Python) is ignored when this option is specified.
  • CLEAR_SELECTIONClears or removes any selection. The Expression parameter (where_clause in Python) is ignored when this option is specified.
String
where_clause
(Optional)

An SQL expression used to select a subset of records. For more information on SQL syntax, see SQL reference for query expressions used in ArcGIS.

SQL Expression
invert_where_clause
(Optional)

Specifies whether the result of the expression should be inverted.

  • NON_INVERTThe result of the query will be used as is. This is the default.
  • INVERTThe result of the query will be inverted. If the selection_type parameter is used, the inversion occurs before the selection is combined with existing selections.
Boolean

Code sample

SelectLayerByAttribute example (Python window)

The following Python window script demonstrates how to use the SelectLayerByAttribute function in immediate mode.

import arcpy
arcpy.SelectLayerByAttribute_management ("states", "NEW_SELECTION", "[NAME] = 'California'")
SelectLayerByAttribute example 2 (stand-alone script)

The following stand-alone script shows how to use the SelectLayerByAttribute function in a workflow to extract features to a new feature class based on location and an attribute query.

# Name: ExtractFeaturesByLocationAndAttribute.py
# Description: Extract features to a new feature class based on a spatial relationships to another layer AND an attribute query
 
# Import system modules
import arcpy

# Set the workspace
arcpy.env.workspace = "c:/data/mexico.gdb"
 
# Select all cities which overlap the chihuahua polygon
chihuahua_cities = arcpy.SelectLayerByLocation_management("cities", "intersect", "chihuahua", 0, "new_selection")

# Within selected features, further select only those cities which have a population > 10,000   
arcpy.SelectLayerByAttribute_management(chihuahua_cities, "SUBSET_SELECTION", '"population" > 10000')
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management(chihuahua_cities, "chihuahua_10000plus")

Environments

This tool does not use any geoprocessing environments.

Licensing information

  • ArcGIS Desktop Basic: Yes
  • ArcGIS Desktop Standard: Yes
  • ArcGIS Desktop Advanced: Yes

Related topics