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 number of selected records will be listed in the geoprocessing history under Parameters > Count. Additionally, the Get Count tool can be used to count the number of selected records. From Python, the number of selected records can also be accessed from the tool's Result object.
Syntax
arcpy.management.SelectLayerByAttribute(in_layer_or_view, {selection_type}, {where_clause}, {invert_where_clause})
Parameter | Explanation | Data Type |
in_layer_or_view | The data to which the selection will be applied. | Table View; Raster Layer; Mosaic Layer |
selection_type (Optional) | Specifies how the selection will be applied and what to do if a selection already exists.
| String |
where_clause (Optional) |
An SQL expression used to select a subset of records. For more information about SQL syntax, see SQL reference for query expressions used in ArcGIS. | SQL Expression |
invert_where_clause (Optional) | Specifies whether the expression will be used as is, or the opposite of the expression will be used.
| Boolean |
Derived Output
Name | Explanation | Data Type |
out_layer_or_view | The updated inputs with selections applied. | Table View; Raster Layer; Feature Layer |
count | The number of selected records. | Long |
Code sample
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'")
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 that overlap the chihuahua polygon
chihuahua_cities = arcpy.SelectLayerByLocation_management('cities', 'INTERSECT',
'chihuahua', 0,
'NEW_SELECTION')
# Within selected features, further select only those cities with a
# population > 10,000
arcpy.SelectLayerByAttribute_management(chihuahua_cities, 'SUBSET_SELECTION',
'"population" > 10000')
# Write the selected features to a new feature class
arcpy.CopyFeatures_management(chihuahua_cities, 'chihuahua_10000plus')
Environments
Licensing information
- Basic: Yes
- Standard: Yes
- Advanced: Yes