Summary
Adds, updates, or removes a selection on a layer or table view based on an attribute query.
Usage
The input must be a feature layer or a table view. The input cannot be a feature class or table.
This tool works on layers or table views in the ArcMap table of contents, and also on layers or table views created in a scripts using the Make Feature Layer or Make Table View tools.
If an Extent environment is specified, or if a definition query is present on the Input Layer or Table View, only the features or rows within the extent or matching the definition query may be selected.
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})
Parameter | Explanation | Data Type |
in_layer_or_view | The feature layer or table view to which the selection will be applied. The input can be a layer or table view in the ArcMap table of contents, or a layer or table view created in ArcCatalog or in scripts using the Make Feature Layer or Make Table View tools. | 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.
| String |
where_clause (Optional) |
An SQL expression used to select a subset of records. For more information on SQL syntax see the help topic SQL reference for query expressions used in ArcGIS. | SQL Expression |
Code sample
Select Layer By Attribute Example (Python Window)
The following Python window script demonstrates how to use the SelectLayerByAttribute function in immediate mode.
import arcpy
arcpy.MakeFeatureLayer_management ("C:/data/data.mdb/states", "stateslyr")
arcpy.SelectLayerByAttribute_management ("stateslyr", "NEW_SELECTION", " [NAME] = 'California' ")
Select Layer By Attribute Example 2 (Stand-alone Script)
The following stand-alone script shows how to use the SelectLayerByAttributes function in a workflow to extract features to a new feature class based on location and an attribute query.
# Name: ExtactFeaturesByLocationAndAttribute.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
env.workspace = "c:/data/mexico.gdb"
# Make a layer from the feature class
arcpy.MakeFeatureLayer_management("cities", "lyr")
# Select all cities which overlap the chihuahua polygon
arcpy.SelectLayerByLocation_management("lyr", "intersect", "chihuahua", 0, "new_selection")
# Within selected features, further select only those cities which have a population > 10,000
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", ' "population" > 10000 ')
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", "chihuahua_10000plus")
Environments
Licensing information
- ArcGIS for Desktop Basic: Yes
- ArcGIS for Desktop Standard: Yes
- ArcGIS for Desktop Advanced: Yes