Select Layer By Attribute (Data Management)

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.

Parameters

LabelExplanationData Type
Input Rows

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.

  • New selection — The resulting selection replaces the current selection. This is the default.
  • Add to the current selection — The resulting selection is added to the current selection if one exists. If no selection exists, this is the same as the new selection option.
  • Remove from the current selection — The resulting selection is removed from the current selection. If no selection exists, this option has no effect.
  • Select subset from the current selection — The resulting selection is combined with the current selection. Only records that are common to both remain selected.
  • Switch the current selection — The selection is switched. All records that were selected are removed from the current selection, and all records that were not selected are added to the current selection. The Expression parameter (where_clause in Python) is ignored when this option is specified.
  • Clear the current selection — The selection is cleared or removed. The Expression parameter (where_clause in Python) is ignored when this option is specified.
String
Expression
(Optional)

An SQL expression used to select a subset of records.

SQL Expression
Invert Where Clause
(Optional)

Specifies whether the expression will be used as is, or the opposite of the expression will be used.

  • Unchecked—The query will be used as is. This is the default.
  • Checked—The opposite of the query will be used. If the Selection Type parameter is used, the reversal of the selection occurs before it is combined with existing selections.
Boolean

Derived Output

LabelExplanationData Type
Updated Layer Or Table View

The updated inputs with selections applied.

Table View; Raster Layer; Feature Layer
Count

The number of selected records.

Long

arcpy.management.SelectLayerByAttribute(in_layer_or_view, {selection_type}, {where_clause}, {invert_where_clause})
NameExplanationData 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.

  • NEW_SELECTIONThe resulting selection replaces the current selection. This is the default.
  • ADD_TO_SELECTIONThe resulting selection is added to the current 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 the current selection. If no selection exists, this option has no effect.
  • SUBSET_SELECTIONThe resulting selection is combined with the current selection. Only records that are common to both remain selected.
  • SWITCH_SELECTIONThe selection is switched. All records that were selected are removed from the current selection, and all records that were not selected are added to the current selection. The Expression parameter (where_clause in Python) is ignored when this option is specified.
  • CLEAR_SELECTIONThe selection is cleared or removed. The Expression parameter (where_clause in Python) is ignored when this option is specified.
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.

  • NON_INVERTThe query will be used as is. This is the default.
  • INVERTThe opposite of the query will be used. If the selection_type parameter is used, the reversal of the selection occurs before it is combined with existing selections.
Boolean

Derived Output

NameExplanationData 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

SelectLayerByAttribute example (Python window)

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'")
SelectLayerByAttribute example 2 (stand-alone script)

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

This tool does not use any geoprocessing environments.

Licensing information

  • Basic: Yes
  • Standard: Yes
  • Advanced: Yes

Related topics