You are on page 1of 28

Ecosystem

Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Structured Query Language


Dr. Michelle Lawing
Ecosystem Science and Management
Texas A&M University
College Sta*on, TX 77843
alawing@tamu.edu

Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Today In Class
Announcements

Quiz

Lecture (sql)

Break

Lab
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Announcements
Website is updated

ASPRS for Thursday, April 09 at 5:30 in 214 Centeq

Jenny Oakley (Family emergency) work on case


study regarding EIH

Homework read case studies (I will post online)
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Job Announcement: GIS Analyst


Loca*on: Tyler, TX
Job Purpose
- E.L.S. Surveying & Mapping, Inc. is an all-inclusive surveying rm specializing in oil and gas.
Primary du*es for this posi*on include: crea*ng geodatabase deliverables using ArcGIS, CAD
and Microsog Excel. This posi*on is intended to provide our clients with survey grade data in
a polished deliverable format.
Du:es and Responsibili:es
- Create geodatabase deliverables using ESRI sogware
- Entering data eciently and accurately
- Using CAD in conjunc*on with ESRI sogware
- Maintain survey grade precision in ArcMap environment
Qualica:ons
- Procient with ESRI sogware (ArcMap, ArcCatalog, etc.)
- Microsog Oce experience
- Knowledge of CAD preferred
- Programming skills are a plus.
- Ability to work well with others in a team oriented environment
To apply, please submit resume to buster@elssurveying.com
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

ESSM Recep*on
April 17th 6pm
hjp://essm.tamu.edu/
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Footprints 5K
April 18th
hjp://essm.tamu.edu/footprints/
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Updates on Team Progress (your role+)


Overview of project

Overall project status

The objec*ves accomplished since the last week



Upcoming ac*vi*es
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Quiz
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Structured Query Language (SQL)


Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

SQL
standard computer language for accessing and
managing databases
SQL expressions are used in many parts of ArcGIS
dene a subset of data on which to perform some
opera*on
You use it in ArcMap to select features with the
Select by Ajributes dialog box
You use it in ArcMap in the Query Builder dialog box
to set a layer deni*on query
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

SQL
You use it in geoprocessing to dene a subset of features
You use it in geoprocessing to dene records to perform
an opera*on on
You also use SQL to select features programma*cally
We will review the basic WHERE clause expressions
For the SQL beginner
Detailed reference here (hjp://webhelp.esri.com/
arcgisdesktop/9.2/index.cfm?
TopicName=SQL_Reference)
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

A simple SQL expression


SELECT * FROM forms the rst part of the SQL
expression
This is usually automa*cally supplied for you
The hard-coded SELECT * syntax means that you
must select ALL the columns
DISTINCT, ORDER BY, GROUP BY, and so on, cannot
be used readily in a SQL query except when using
subqueries
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

A simple SQL expression


Depending on the dialog box in which you are
building your query, the name of the layer or table
may be supplied for you or you may need to choose
it from a drop-down list
The next part of the expression is the WHERE clause,
which is the part you must build.
A basic SQL WHERE clause would look like
STATE_NAME = Texas

Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

SQL Syntax
The syntax you use to build a SQL expression diers
depending on the data source.
SQL is a standard, but not all database sogware
implements the same dialect of SQL.
To query personal geodatabases, you use the same
syntax used in Microsog Access.
To query an ArcSDE geodatabase, you use the SQL
syntax of the underlying database management
system (DBMS)either Oracle, SQL Server, DB2, or
Informix.
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

SQL Syntax
The dialog boxes you create SQL WHERE clauses with
help you
use the correct syntax for the data you're querying
list the eld names and values with the appropriate
delimiters
select the relevant keywords and operators for you
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Searching Strings
Strings must always be enclosed within single quotes.
For example
"STATE_NAME" = California

Strings in expressions are case sensi*ve


Use an SQL func*on to convert all values to the same
case
UPPER or LOWER func*on
For example, the following will select a last name stored
as either Jones or JONES:
UPPER("LAST_NAME") = 'JONES'

Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Searching Strings
UCASE and LCASE perform the same opera*on for a
personal geodatabase
Use the LIKE operator (instead of the = operator) to
build a par*al string search.
For example, this expression would select Mississippi
and Missouri among USA state names:
"STATE_NAME" LIKE 'Miss%'

Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Search Strings
% means that anything is acceptable in its place
one character
a hundred characters
no character
Alterna*vely, if you want to search with a wildcard
that represents one character, use _
For example, this expression would nd Catherine
Smith and Katherine Smith:
"OWNER_NAME" LIKE '_atherine smith'

Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Search Strings
The wildcards work for any le-based data or
mul*user geodatabase data.
The wildcards you use to query personal
geodatabases are * for any number of characters
and ? for one character.
Wildcard characters appear as bujons on the Select
by Ajributes and Query Builder dialog boxes.
If you use a wildcard character in a string with the =
operator, the character is treated as part of the
string, not as a wildcard.
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Search Strings
Select based on sor*ng order
greater than (>)
less than (<)
greater than or equal (>=)
less than or equal (<=)
BETWEEN
not equal (<>)
For example, this expression will select all the ci*es in a
coverage with names star*ng with the lejers M to Z:
"CITY_NAME" >= 'M'
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

The Null Keyword


Select null values for a specied eld.
The NULL keyword is always preceded by IS or IS
NOT.
For example, to nd ci*es whose 1996 popula*on
has not been entered, you can use
"POPULATION96" IS NULL
Alterna*vely, to nd ci*es whose 1996 popula*on
has been entered, you can use
"POPULATION96" IS NOT NULL
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Searching Numbers
Query numbers by
the equal (=)
not equal (<>)
greater than (>)
less than (<)
greater than or equal (>=)
less than or equal (<=)
BETWEEN

For example
"POPULATION96" >= 5000

Numeric values are always listed using the point as the decimal delimiter
regardless of your regional se}ngs (PROBLEM IN INTERNATIONAL
SETTING).
The comma cannot be used as a decimal or thousands delimiter in an
expression.
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Calcula*ons
Calcula*ons can be included in expressions using the
arithme*c operators: + - * /
Calcula*ons can be between elds and numbers.
For example:
"AREA" >= "PERIMETER" * 100

Calcula*ons can also be performed between elds.


For example, to nd the countries with a popula*on density
of less than or equal to 25 people per square mile, you could
use this expression:
"POP1990" / "AREA" <= 25

Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Operator precedence
Expressions evaluate according to standard operator
precedence rules.
For example, the part of an expression enclosed in
parentheses is evaluated before the part that isn't
enclosed.

"HOUSEHOLDS" > "MALES" * "POP90_SQMI" + "AREA

evaluates dierently from

"HOUSEHOLDS" > "MALES" * ("POP90_SQMI" + "AREA")

Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Combining expressions
Complex expressions can be built by combining expressions with the AND and OR operators.

For example, the following expression would select all the houses that have more than 1,500 square feet and a
garage for three or more cars:


"AREA" > 1500 AND "GARAGE" > 3

When you use the OR operator, at least one side of the expression of the two separated by the OR operator must
be true for the record to be selected.

For example:

"RAINFALL" < 20 OR "SLOPE" > 35

Use the NOT operator at the beginning of an expression to nd features or records that don't match the specied
expression.
For example:

NOT "STATE_NAME" = 'Colorado'

NOT expressions can be combined with AND and OR.

For example, this expression would select all the New England states except Maine:

"SUB_REGION" = 'New England' AND NOT "STATE_NAME" = 'Maine'
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Subqueries
A subquery is a query nested within another query and
are supported by geodatabase data sources only. They
can be used to apply predicate or aggregate func*ons or
to compare data with values stored in another table. For
example, this query would select only the countries that
are not also listed in the table indep_countries:


"COUNTRY_NAME" NOT IN (SELECT "COUNTRY_NAME"
FROM indep_countries)

For more informa*on, see SQL Reference.
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

Querying Dates
Some changes have been introduced in ArcGIS 9 for
building expressions that query date elds. The
appropriate syntax for the underlying data source is
automa*cally shown in the unique values list or
generated by the Query Builder. Most of the *me,
you will only need to click the eld, the operator,
and the value to generate the proper syntax.
Ecosystem Science and Management | Texas A&M University ESSM 464: Spa*al Project Management
(c) 2015, A. Michelle Lawing

You might also like