ArcGIS for Desktop

  • Documentation
  • Pricing
  • Support

  • My Profile
  • Help
  • Sign Out
ArcGIS for Desktop

ArcGIS Online

The mapping platform for your organization

ArcGIS for Desktop

A complete professional GIS

ArcGIS for Server

GIS in your enterprise

ArcGIS for Developers

Tools to build location-aware apps

ArcGIS Solutions

Free template maps and apps for your industry

ArcGIS Marketplace

Get apps and data for your organization

  • Documentation
  • Pricing
  • Support
Esri
  • Sign In
user
  • My Profile
  • Sign Out

Help

  • Home
  • Get Started
  • Map
  • Analyze
  • Manage Data
  • Tools
  • More...

Select Layer By Attribute

  • Summary
  • Usage
  • Syntax
  • Code sample
  • Environments
  • Licensing information

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 an Extent 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

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_SELECTION —The resulting selection replaces any existing selection. This is the default.
  • ADD_TO_SELECTION —The 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_SELECTION —The resulting selection is removed from an existing selection. If no selection exists, this option has no effect.
  • SUBSET_SELECTION —The resulting selection is combined with the existing selection. Only records that are common to both remain selected.
  • SWITCH_SELECTION —Switches 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_SELECTION —Clears 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

  • Auto Commit
  • Current Workspace
  • Extent

Licensing information

  • ArcGIS for Desktop Basic: Yes
  • ArcGIS for Desktop Standard: Yes
  • ArcGIS for Desktop Advanced: Yes

Related topics

  • An overview of the Layers and Table Views toolset
  • Using layers and table views
  • Working with layers and table views
  • Creating and using layer selections
  • Building a query expression
  • SQL reference for query expressions used in ArcGIS

ArcGIS for Desktop

  • Home
  • Documentation
  • Pricing
  • Support

ArcGIS Platform

  • ArcGIS Online
  • ArcGIS for Desktop
  • ArcGIS for Server
  • ArcGIS for Developers
  • ArcGIS Solutions
  • ArcGIS Marketplace

About Esri

  • About Us
  • Careers
  • Insiders Blog
  • User Conference
  • Developer Summit
Esri
Tell us what you think.
© Copyright 2016 Environmental Systems Research Institute, Inc. | Privacy | Legal