Skip to main content
Geosciences LibreTexts

6.2: Searches and Queries

  • Page ID
    6337
    • Anonymous
    • LibreTexts
    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Learning Objectives
    • The objective of this section is to outline the basics of the SQL language and to understand the various query techniques available in a GIS.

    Access to robust search and query tools is essential to examine the general trends of a dataset. Queries are essentially questions posed to a database. The selective display and retrieval of information based on these queries are essential components of any geographic information system (GIS). There are three basic methods for searching and querying attribute data: (1) selection, (2) query by attribute, and (3) query by geography.

    Selection

    Selection represents the easiest way to search and query spatial data in a GIS. Selecting features highlight those attributes of interest, both on-screen and in the attribute table, for subsequent display or analysis. To accomplish this, one selects points, lines, and polygons simply by using the cursor to “point-and-click” the feature of interest or by using the cursor to drag a box around those features. Alternatively, one can select features by using a graphic object, such as a circle, line, or polygon, to highlight all of those features that fall within the object. Advanced options for selecting subsets of data from the larger dataset include creating a new selection, selecting from the currently selected features, adding to the current selection, and removing from the current selection.

    Query by Attribute

    Map features and their associated data can be retrieved via the query of attribute information within the data tables. For example, search and query tools allow a user to show all the census tracts that have a population density of 500 or greater, to show all counties that are less than or equal to 100 square kilometers, or to show all convenience stores within 1 mile of an interstate highway.

    Specifically, SQL (Structured Query Language) is a commonly used computer language developed to query attribute data within a relational database management system. Created by IBM in the 1970s, SQL allows for the retrieval of a subset of attribute information based on specific, user-defined criteria via the implementation of particular language elements. More recently, the use of SQL has been extended for use in a GIS (Shekhar and Chawla 2003).Shekhar, S., and S. Chawla. 2003. Spatial Databases: A Tour. Upper Saddle River, NJ: Prentice Hall. One important note related to the use of SQL is that the exact expression used to query a dataset depends on the GIS file format being examined. For example, ANSI SQL is a particular version used to query ArcSDE geodatabases, while Jet SQL is used to access personal geodatabases. Similarly, shapefiles, coverages, and dBASE tables use a restricted version of SQL that doesn’t support all the features of ANSI SQL or Jet SQL.

    As discussed in Section 5.2, all attribute tables in a relational database management system (RDBMS) used for an SQL query must contain primary and/or foreign keys for proper use. In addition to these keys, SQL implements clauses to structure database queries. A clause is a language element that includes the SELECT, FROM, WHERE, ORDER BY, and HAVING query statements.

    • SELECT denotes what attribute table fields you wish to view.
    • FROM denotes the attribute table in which the information resides.
    • WHERE denotes the user-defined criteria for the attribute information that must be met in order for it to be included in the output set.
    • ORDER BY denotes the sequence in which the output set will be displayed.
    • HAVING denotes the predicate used to filter output from the ORDER BY clause.

    While the SELECT and FROM clauses are both mandatory statements in an SQL query, the WHERE is an optional clause used to limit the output set. The ORDER BY and HAVING are optional clauses used to present the information in an interpretable manner.

    e0fe0a05ac22294241978243657ad15f.jpg
    Figure 6.6 Personal Addresses in “ExampleTable” Attribute Table

    The following is a series of SQL expressions and results when applied to Figure 6.6. The title of the attribute table is “ExampleTable.” Note that the asterisk (*) denotes a special case of SELECT whereby all columns for a given record are selected:

    SELECT * FROM ExampleTable WHERE City = “Upland”

    This statement returns the following:

    84ea86f47f2ebf999e9ea90e87cb8b8c.jpg

    Consider the following statement:

    SELECT LastName FROM ExampleTable WHERE State = “CA” ORDER BY FirstName

    This statement results in the following table sorted in ascending order by the FirstName column (not included in the output table as directed by the SELECT clause):

    f295ebb0a4bdbb24e1e7b12f6f09fe58.jpg

    In addition to clauses, SQL allows for the inclusion of specific operators to further delimit the result of query. These operators can be relational, arithmetic, or Boolean and will typically appear inside of conditional statements in the WHERE clause. A relational operator employs the statements equal to (=), less than (<), less than or equal to (<=), greater than (>), or greater than or equal to (>=). Arithmetic operators are those mathematical functions that include addition (+), subtraction (−), multiplication (*), and division (/). Boolean operators (also called Boolean connectors) include the statements AND, OR, XOR, and NOT. The AND connector is used to select records from the attribute table that satisfies both expressions. The OR connector selects records that satisfy either one or both expressions. The XOR connector selects records that satisfy one and only one of the expressions (the functional opposite of the AND connector). Lastly, the NOT connector is used to negate (or unselect) an expression that would otherwise be true. Put into the language of probability, the AND connector is used to represent an intersection, OR represents a union, and NOT represents a complement. Figure 6.7 illustrates the logic of these connectors, where circles A and B represent two sets of intersecting data. Keep in mind that SQL is a very exacting language and minor inconsistencies in the statement, such as additional spaces, can result in a failed query.

    f29eee3abd014546748ecf5a5a598b1b.jpg
    Figure 6.7 Venn Diagram of SQL Operators

    Used together, these operators combine to provide the GIS user with powerful and flexible search and query options. With this in mind, can you determine the output set of the following SQL query as it is applied to Figure 6.1?

    SELECT LastName, FirstName, StreetNumber FROM ExampleTable WHERE StreetNumber >= 10000 AND StreetNumber < 100 ORDER BY LastName

    The following are the results:

    0b2fbb5a504bc4af7b28def08865c012.jpg

    Query by Geography

    Query by geography, also known as a “spatial query,” allows one to highlight particular features by examining their position relative to other features. For example, a GIS provides robust tools that allow for the determination of the number of schools within 10 miles of a home. Several spatial query options are available, as outlined here. Throughout this discussion, the “target layer” refers to the feature dataset whose attributes are selected, while the “source layer” refers to the feature dataset on which the spatial query is applied. For example, if we were to use a state boundary polygon feature dataset to select highways from a line feature dataset (e.g., select all the highways that run through the state of Arkansas), the state layer is the source, while the highway layer is the target.

    • INTERSECT. This oft-used spatial query technique selects all features in the target layer that share a common locale with the source layer. The “intersect” query allows points, lines, or polygon layers to be used as both the source and target layers (Figure 6.8).
    7a0a60095f2f1d6fff13793fe5ee1d7e.jpg
    Figure 6.8: The highlighted blue and yellow features are selected because they intersect the red features.
    • ARE WITHIN A DISTANCE OF. This technique requires the user to specify some distance value, which is then used to buffer (Figure 6.9).
    9b3e5ff24812a99e9258433b2aa95897.jpg
    Figure 6.9: The highlighted blue and yellow features are selected because they are within the selected distance of the red features; tan areas represent buffers around the various features.
    • COMPLETELY CONTAIN. This spatial query technique returns those features that are entirely within the source layer. Features with coincident boundaries are not selected by this query type. The “completely contain” query allows for points, lines, or polygons as the source layer, but only polygons can be used as a target layer (Figure 6.10).
    fda78d9b8a0a10435047510dbdffc1b0.jpg
    Figure 6.10: The highlighted blue and yellow features are selected because they completely contain the red features.
    • ARE COMPLETELY WITHIN. This query selects those features in the target layer whose entire spatial extent occurs within the geometry of the source layer. The “are completely within” query allows for points, lines, or polygons as the target layer, but only polygons can be used as a source layer (Figure 6.11).
    d17f218d182a354e38a64a0a90981abb.jpg
    Figure 6.11: The highlighted blue and yellow features are selected because they are completely within the red features.
    • HAVE THEIR CENTER IN. This technique selects target features whose center, or centroid, is located within the boundary of the source feature dataset. The “have their center in” query allows points, lines, or polygon layers to be used as both the source and target layers (Figure 6.12).
    febda1bfc18bd42532873ed546bb43ba.jpg
    Figure 6.12: The highlighted blue and yellow features are selected because they have their centers in the red features.
    • SHARE A LINE SEGMENT. This spatial query selects target features whose boundary geometries share a minimum of two adjacent vertices with the source layer. The “share a line segment” query allows for line or polygon layers to be used for either of the source and target layers (Figure 6.13).
    6c65cad889f14c080ec6bd140042d536.jpg
    Figure 6.13: The highlighted blue and yellow features are selected because they share a line segment with the red features.
    • TOUCH THE BOUNDARY OF. This methodology is similar to the INTERSECT spatial query; however, it selects line and polygon features that share a common boundary with target layer. The “touch the boundary of” query allows for line or polygon layers to be used as both the source and target layers (Figure 6.14).
    1eea122af6a0514477053c9fd1e4ae09.jpg
    Figure 6.14: The highlighted blue and yellow features are selected because they touch the boundary of the red features.
    • ARE IDENTICAL TO. This spatial query returns features that have the exact same geographic location. The “are identical to” query can be used on points, lines, or polygons, but the target layer type must be the same as the source layer type (Figure 6.15).
    5d759c185697124c78c237c38e36f30d.jpg
    Figure 6.15: The highlighted blue and yellow features are selected because they are identical to the red features.
    • ARE CROSSED BY THE OUTLINE OF. This selection criteria returns features that share a single vertex but not an entire line segment. The “are crossed by the outline of” query allows for line or polygon layers to be used as both source and target layers (Figure 6.16).
    bc4348930205b5d1b86977f9ba505860.jpg
    Figure 6.16: The highlighted blue and yellow features are selected because they are crossed by the outline of the red features.
    • CONTAIN. This method is similar to the COMPLETELY CONTAIN spatial query; however, features in the target layer will be selected even if the boundaries overlap. The “contain” query allows for point, line, or polygon features in the target layer when points are used as a source; when line and polygon target layers with a line source; and when only polygon target layers with a polygon source (Figure 6.17).
    b9fb88c22ae4490c609a22997f05f550.jpg
    Figure 6.17: The highlighted blue and yellow features are selected because they contain the red features.
    • ARE CONTAINED BY. This method is similar to the ARE COMPLETELY WITHIN spatial query; however, features in the target layer will be selected even if the boundaries overlap. The “are contained by” query allows for point, line, or polygon features in the target layer when polygons are used as a source; when point and line target layers with a line source; and when only point target layers with a point source (Figure 6.18).
    ebbcab4d654e600bc258816adbb8863b.jpg
    Figure 6.18: The highlighted blue and yellow features are selected because they are contained by the red features.

    Key Takeaways

    • The three basic methods for searching and querying attribute data are selection, query by attribute, and query by geography.
    • SQL is a commonly used computer language developed to query by attribute data within a relational database management system.
    • Queries by geography allow a user to highlight desired features by examining their position relative to other features. The eleven different query-by-geography options listed here are available in most GIS software packages.

    Exercises

    1. Using Figure 6.1, develop the SQL statement that results in the output of all the street names of people living in Los Angeles, sorted by street number.
    2. When querying by geography, what is the difference between a source layer and a target layer?
    3. What is the difference between the CONTAIN, COMPLETELY CONTAIN, and ARE CONTAINED BY queries?

    This page titled 6.2: Searches and Queries is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Anonymous via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.