Show Navigation | Hide Navigation
You are here:
Geoprocessing tool reference > Data Management toolbox > Layers and Table Views toolset > Tools

Select Layer By Attribute (Data Management)

Release 9.2
Last modified November 29, 2010
E-mail This Topic Printable Version Give Us Feedback

Print all topics in : "Tools"


Related Topics

Creates, updates, or removes a selection on the layer or table view using an attribute query.


Usage tips

Command line syntax
An overview of the Command Line window
SelectLayerByAttribute_management <in_layer_or_view> {NEW_SELECTION | ADD_TO_SELECTION | REMOVE_FROM_SELECTION | SUBSET_SELECTION | SWITCH_SELECTION | CLEAR_SELECTION} {where_clause}

Parameter Explanation Data Type
<in_layer_or_view>

The feature layer or table view to which the selection will be applied. This can be a layer or table in ArcMap or a layer or table view created by Make Layer or Make Table View in ArcCatalog or in a script.

Table View
{NEW_SELECTION | ADD_TO_SELECTION | REMOVE_FROM_SELECTION | SUBSET_SELECTION | SWITCH_SELECTION | CLEAR_SELECTION}

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}

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

For more information on SQL syntax and how it differs between data sources, see SQL Reference.

SQL Expression
Data types for geoprocessing tool parameters


Command line example

SelectLayerByAttribute states NEW_SELECTION  " [NAME] = 'California' "

Scripting syntax
About getting started with writing geoprocessing scripts
SelectLayerByAttribute_management (in_layer_or_view, selection_type, where_clause)

Parameter Explanation Data Type
in_layer_or_view (Required)

The feature layer or table view to which the selection will be applied. This can be a layer or table in ArcMap or a layer or table view created by Make Layer or Make Table View in ArcCatalog or in a script.

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

The syntax for the expression differs slightly depending on the data source. For example, if you're querying file or ArcSDE geodatabases, shapefiles, coverages, 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].

For more information on SQL syntax and how it differs between data sources, see SQL Reference.

SQL Expression

Data types for geoprocessing tool parameters


Script example

# ExtactFeaturesByLocationAndAttribute.py
# Description: Extract features to a new feature class based on a Location and an attribute query
# Requirements: Python and the Python win32all extension
# Author: ESRI
# Data 1/1/2004

# Create the Geoprocessor
import arcgisscripting
gp = arcgisscripting.create()

# Put in error trapping in case an error occurs when running tool
try:
    # Make a layer from the feature class
    gp.MakeFeatureLayer("c:/mexico.mdb/cities","lyr") 

    # Select all cities which overlap the chihuahua polygon
    gp.SelectLayerByLocation("lyr", "intersect", "c:/mexico.mdb/chihuahua", 0, "new_selection")

    # Within the selection (done above) further select only those cities which have a population > 10,000   
    gp.SelectLayerByAttribute("lyr", "SUBSET_SELECTION", " [population] > 10000 ")

    # Write the selected features to a new featureclass
    gp.CopyFeatures("lyr", "c:/mexico.mdb/chihuahua_10000plus")

except:
    # If an error occurred print the message to the screen
    print gp.GetMessages()

Please visit the Feedback page to comment or give suggestions on ArcGIS Desktop Help.
Copyright © Environmental Systems Research Institute, Inc.