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 a definition query is present on the input, only the features or rows matching the definition query will be used in the selection.

  • The number of selected records will be listed in the geoprocessing history. Click Parameters > Count to access them. 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.

  • The field delimiters used in an SQL expression differ depending on the format of the queried data. For instance, file geodatabases and shapefiles use double quotes, and enterprise geodatabases don't use field delimiters. You can use the AddFieldDelimiters function to help ensure that the field delimiters used with the SQL expression are correct.

  • If the input's data source is a feature service, it is recommended that the underlying ArcGIS Server use standardized SQL queries.

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 selectionThe resulting selection will replace the current selection. This is the default.
  • Add to the current selectionThe resulting selection will be 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 selectionThe resulting selection will be removed from the current selection. If no selection exists, this option has no effect.
  • Select subset from the current selectionThe resulting selection will be combined with the current selection. Only records that are common to both remain selected.
  • Switch the current selectionThe selection will be switched. All records that were selected will be removed from the current selection, and all records that were not selected will be added to the current selection. The Expression parameter is ignored when this option is specified.
  • Clear the current selectionThe selection will be cleared or removed. The Expression parameter 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 will replace the current selection. This is the default.
  • ADD_TO_SELECTIONThe resulting selection will be 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 will be removed from the current selection. If no selection exists, this option has no effect.
  • SUBSET_SELECTIONThe resulting selection will be combined with the current selection. Only records that are common to both remain selected.
  • SWITCH_SELECTIONThe selection will be switched. All records that were selected will be removed from the current selection, and all records that were not selected will be added to the current selection. The where_clause parameter is ignored when this option is specified.
  • CLEAR_SELECTIONThe selection will be cleared or removed. The where_clause parameter is ignored when this option is specified.
String
where_clause
(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.

  • 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.management.SelectLayerByAttribute("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 
# relationship 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.management.SelectLayerByLocation('cities', 'INTERSECT', 
                                                          'chihuahua', 0, 
                                                          'NEW_SELECTION')

# Within selected features, further select only those cities with a 
# population > 10,000   
arcpy.management.SelectLayerByAttribute(chihuahua_cities, 'SUBSET_SELECTION', 
                                        '"population" > 10000')

# Write the selected features to a new feature class
arcpy.management.CopyFeatures(chihuahua_cities, 'chihuahua_10000plus')

Environments

This tool does not use any geoprocessing environments.

Licensing information

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

Related topics