Select Layer By Attribute (Data Management)

License Level:BasicStandardAdvanced

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. 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

Related Topics

Licensing Information

ArcGIS for Desktop Basic: Yes
ArcGIS for Desktop Standard: Yes
ArcGIS for Desktop Advanced: Yes
5/7/2015