Select Layer By Attribute (Data Management)
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 anExtent 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
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. The syntax for the expression differs slightly depending on the data source. For example, if you're querying file or ArcSDE geodatabases, shapefiles, coverages, or dBASE or INFO tables, enclose field names in double quotes: "MY_FIELD" If you're querying personal geodatabases, enclose fields in square brackets: [MY_FIELD] In Python, strings are enclosed in matching single or double quotes. To create a string that contains quotes (as is common with a WHERE clause in SQL expressions), you can escape the quotes (using a backslash) or triple quote the string. For example, if the intended WHERE clause is "CITY_NAME" = 'Chicago' you could enclose the entire string in double quotes, then escape the interior double quotes like this: " \"CITY_NAME\" = 'Chicago' " Or you could enclose the entire string in single quotes, then escape the interior single quotes like this: ' "CITY_NAME" = \'Chicago\' ' Or you could enclose the entire string in triple quotes without escaping: """ "CITY_NAME" = 'Chicago' """ For more information on SQL syntax and how it differs between data sources, see the help topic SQL reference for query expressions used in ArcGIS. | SQL Expression |
Code Sample
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' ")
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 # Author: ESRI # 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")