Skip To Content

Make Query Table

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 fields or rows from the original data in the database.

This tool accepts data from a geodatabase or database 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 field 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 field to the data. Without an ObjectID field, selections will not be supported.

  • The key fields list lets you choose several fields if the combination of these fields 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.

  • For details on the syntax for the Expression parameter, see SQL query (where clause).

  • The Add Field button in the Fields and Key Fields parameters 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 fields 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 where_clause parameter can be used to define how they are to be joined.

The input table can be from a geodatabase or a database 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

Layers and table views in ArcGIS require an ObjectID field. An ObjectID field is an integer field used to uniquely identify rows in the data being used. In this tool, this parameter is used to indicate how an ObjectID field will be generated (if at all) for the query. The default is Use key fields (USE_KEY_FIELDS in Python).

  • USE_KEY_FIELDSThis indicates that the fields chosen in the Key Fields parameter can be used to uniquely identify a row in the output table. This can be a single field or a combination of multiple fields, which, when combined, uniquely identify a row in the output table. If there are no fields chosen in the key fields list, the Generate a key field option (ADD VIRTUAL_KEY_FIELD in Python) is automatically applied.
  • ADD_VIRTUAL_KEY_FIELDIf no key fields have been specified, you can choose this option to have an ObjectID generated that uniquely identifies each row in the output table.
  • NO_KEY_FIELDThis option indicates that no ObjectID field is to be generated. Choosing this option means that selections will not be supported for the table view.
    Note:

    If there is already a field 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 USE_KEY_FIELDS is set.

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. If a Shape field is added to the field list, the result is a layer; otherwise it is a table view.

Value Table
where_clause
(Optional)

An SQL expression used to select a subset of records.

SQL Expression

Code sample

MakeQueryTable example 1 (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
 
# Local variables...
tableList = ["c:/Connections/balrog.odc/vtest.COUNTIES",\
             "c:/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")

Environments

Licensing information

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

Related topics