SQL comes from the database industry, and you can "INSERT," "DELETE," and "SELECT" rows in tables. It comes with a very rich syntax, and portions of "SELECT" grammar are used heavily in ArcGIS:
SQL is used in a variety of ways. In the "Select by Attributes" tool, the following is used:
Most of the SQL statement is provided by ArcGIS while the WHERE clause is added by the user. This tool helps you to write the WHERE Clause.
Below is an example of an attribute table from a layer for the countries of the world.
WHERE clauses can contain a number of different types of comparisons. The features that contain attributes that make the comparison true will be selected while those that make it false are not selected.
Here are some examples of WHERE clauses for a layer of lakes in the United States.
If you use the equal sign ("=") to compare two strings, the string have to be exactly the same. There are different approaches to how to deal with this in different database applications. In ArcGIS, you can use the "UPPER()" function to make all attribute values upper case and then compare them:
See the ArcGIS documentation for more examples.
Boolean values are values that can be true or false. The can be represented by:
Boolean values can be combined together to make more complex statements using AND, OR, and NOT. It works like this:
The table below shows the results of all possible combination of boolean operator results.
A | B | A AND B | A OR B | NOT A | NOT B |
---|---|---|---|---|---|
T | T | T | T | F | F |
T | F | F | T | F | T |
F | T | F | T | T | F |
F | F | F | F | T | T |
The comparisons and Boolean operators can now be combined together into more complex WHERE clauses.
|
"Area" > 10000 AND "IMPR" = 0 "Name" LIKE 'Hawaii' AND Area < 10000 "Species" LIKE 'Ponderosa' AND DBH > 1 |
|
"RAINFALL" < 20 OR "SLOPE" > 35 |
|
NOT ("STATE_NAME"='Colorado') |
Order Matters:
(A AND B) OR C is not the same as A AND (B OR C)!
And - Must meet both criteria.
Or - Can meet either criteria.
If you are confused on the ordering, add parenthesis to make it clear
© Copyright 2018 HSU - All rights reserved.