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

Make Query Table

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

Summary

This tool applies an SQL query to a database and the results are represented in a layer or table view. The query can be used to join several tables or return a subset of columns or rows from the original data in the database.

This tool accepts data from an ArcSDE geodatabase, a file geodatabase, a personal geodatabase, or an OLE DB connection.

Usage

  • The layer that is created by the tool is temporary and will not persist after the session ends unless the document is saved.

  • All input feature classes or tables must be from the same input workspace.

  • If a Shape column is added to the field list, the result is a layer; otherwise, it is a table view.

  • If the output result is a layer, it may be persisted to a layer file using the Save To Layer File tool or to a feature class using the Copy Features tool.

  • The order of the fields in the field list indicates the order the fields will appear in the output layer or table view.

  • The tool allows you to provide a key field option and key fields list. This information defines how rows are uniquely identified and is used to add a dynamically generated ObjectID column to the data. Without an ObjectID column, selections will not be supported.

  • The key fields list lets you choose several columns if the combination of these columns is needed to define unique values.

  • If an SQL expression is used but returns no matching records, the output feature class will be empty.

  • Feature classes can be joined, but the fields list must contain at most one field of type geometry. If you add more than one geometry column, the tool will display an error when you click OK and execution will stop.

  • For details on the syntax for the Expression parameter, see Building an SQL Expression or SQL Reference.

  • The Fields and Key Fields parameters' Add Field button is used only in ModelBuilder. In ModelBuilder, where the preceding tool has not been run, or its derived data does not exist, the Fields and Key Fields parameters may not be populated with field names. The Add Field button allows you to add expected field(s) so you can complete the Make Query Table dialog box and continue to build your model.

  • When input tables are from a file geodatabase, tables generally join in the order listed in the Input Tables parameter. For example, if Table1 is listed before Table2, Table2 will be joined by getting a row from Table1, then getting matching rows from Table2. However, if this would result in querying Table2 on an nonindexed field, and reversing the order would result in querying Table1 on an indexed field, the order will be reversed in an attempt to maximize performance. This is the sole query optimization logic at work when you're using file geodatabase data with this tool. In general, joins in file geodatabases perform best when they are one-to-many and one-to-one.

Syntax

MakeQueryTable_management (in_table, out_table, in_key_field_option, {in_key_field}, {in_field}, {where_clause})
ParameterExplanationData Type
in_table
[in_table,...]

The name of the table or tables to be used in the query. If several tables are listed, the Expression parameter can be used to define how they are to be joined.

The input table can be from an ArcSDE geodatabase, a file geodatabase, a personal geodatabase, or an OLE DB connection.

Table View; Raster Layer
out_table

The name of the layer or table view that will be created by the tool.

Table View;Raster Layer
in_key_field_option

Indicates how an ObjectID field will be generated, if at all, for the query. The default is USE_KEY_FIELDS.

  • USE_KEY_FIELDS —This indicates that the fields chosen in the key fields list should be used to define the dynamic ObjectID column. If there are no fields chosen in the key fields list, the ADD VIRTUAL_KEY_FIELD option is automatically applied.
  • ADD_VIRTUAL_KEY_FIELD —This option indicates that no key fields have been chosen, but a dynamic ObjectID column is to be generated. This is done by copying the data to a local, system-managed workspace and adding a field with unique values to the copy. The layer or table view can then access the copy and use the added field as the key field.
  • NO_KEY_FIELD —This option indicates that no dynamic ObjectID column is to be generated. Choosing this option means that selections will not be supported for the table view. If there is already a column of type ObjectID in the fields list, it will be used as the ObjectID even if this option is chosen.
String
in_key_field
[in_key_field,...]
(Optional)

Specifies a field or combination of fields that can be used to uniquely identify a row in the query. This parameter is used only when the USE_KEY_FIELDS option is set.

The Add Field button, which is used only in ModelBuilder, allows you to add expected field(s) so you can complete the dialog and continue to build your model.

Field
in_field
[[Field, {Alias}],...,...]
(Optional)

The fields to include in the layer or table view. If an alias is set for a field, this is the name that appears. If no fields are specified, all fields from all tables are included.

Value Table
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

MakeQueryTable Example (Python Window)

The following Python window script demonstrates how to use the MakeQueryTable function in immediate mode.

import arcpy
arcpy.env.workspace = "C:/data/data.gdb"
arcpy.MakeQueryTable_management (["Counties","codemog"], "queryout","ADD_VIRTUAL_KEY_FIELD", "",
                   [["Counties.OBJECTID", 'ObjectID'],["Counties.NAME", 'Name'],
                    ["codemog.Males", 'Males'], ["codemog.Females", 'Females']],
                   "Counties.FIPS = codemog.Fips and Counties.STATE_NAME = 'California'")
MakeQueryTable Example 2 (Stand-alone Script)

The following script is an example of how to use the MakeQueryTable tool in the Python scripting environment.

# MakeQueryTableOLEDB.py
# Description: Create a query table from two OLE DB tables using a limited set of
#               fields and establishing an equal join.
 
# Import system modules
import arcpy
 
try:
    # Local variables...
    tableList = ["Database Connections/balrog.odc/vtest.COUNTIES",\
                 "Database Connections/balrog.odc/vtest.CODEMOG"]
    
    fieldList = [["vtest.COUNTIES.OBJECTID", 'ObjectID'],["vtest.COUNTIES.NAME", 'Name']\
                 ["vtest.CODEMOG.Males", 'Males'],["vtest.CODEMOG.Females", 'Females']]
    whereClause = "vtest.COUNTIES.FIPS = vtest.CODEMOG.Fips" +\
                  "and vtest.COUNTIES.STATE_NAME = 'California'"
    keyField = "vtest.COUNTIES.OBJECTID"
    lyrName = "CountyCombined"
    # Make Query Table...
    arcpy.MakeQueryTable_management(tableList, lyrName,"USE_KEY_FIELDS", keyField, fieldList, whereClause)
 
    # Print the total rows
    print(arcpy.GetCount_management(lyrName))
 
    # Print the fields
    Fields = arcpy.ListFields(lyrName)
    for field in Fields:
        print(Field.name)
        
 
    # Save as a dBASE file
    arcpy.CopyRows_management(lyrName, "C:/temp/calinfo.dbf")
 
except Exception as err:
    print(err.args[0])

Environments

  • Current Workspace

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
  • Examples of queries with the Make Query Table tool

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