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

Syntax

SelectLayerByAttribute_management (in_layer_or_view, {selection_type}, {where_clause})
ParameterExplanationData 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.

  • NEW_SELECTIONThe resulting selection replaces any existing selection. This is the default.
  • ADD_TO_SELECTIONThe resulting selection is added to an existing 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 an existing selection. If no selection exists, this option has no effect.
  • SUBSET_SELECTIONThe resulting selection is combined with the existing selection. Only records that are common to both remain selected.
  • SWITCH_SELECTIONSwitches the selection. All records that were selected are removed from the selection; all records that were not selected are added to the selection. The Expression is ignored when this option is specified.
  • CLEAR_SELECTIONClears or removes any selection. The Expression is ignored when this option is specified.
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

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
# 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")
 

Environments

Related Topics

Licensing Information

ArcView: Yes
ArcEditor: Yes
ArcInfo: Yes

12/10/2012