33
Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Embed Size (px)

Citation preview

Page 1: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-1

Chapter 6

Queries

Page 2: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-2

Outline

• Understanding queries and how they are used• Knowing how to select features interactively

using a variety of methods• Selecting features using SQL queries on their

attributes• Selecting features based on their spatial

location with respect to other features• Using Definition Queries to create subsets of

data layers

Page 3: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-3

What are queries?

• Extract certain records from a map or table

• Records meet certain criteria– Aspatial queries

• All parcels with value greater than $100,000.

– Spatial queries• All parcels that lie completely within the flood plain

Page 4: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-4

Selected features

Highlighted in tableHighlighted in map

States for which POP2000>2 million

Page 5: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-5

Types of Queries

• Interactive selection– Choose features by pointing to them on the

screen

• Select By Attribute– Select features based on attribute criteria

• Select By Location– Select features based on their spatial

relationships

Page 6: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-6

Interactive Selection

Select Features tool

Click on feature to select

Hold down shift key to select more than one feature

Draw a rectangle that passes through features to be selected.

Page 7: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-7

Selectable LayersStates selectableAll layers selectable

Page 8: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-8

Selection options

Page 9: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-9

Clear SelectionOn toolbar

From table options menu From main menu

Page 10: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-10

The Selection tab

Right-click entry for context menu

You can also make layers selectable here by checking them

This one clears features only for this layer

Page 11: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-11

Selection logic

• No selection– All features exported

• No selected features– No features exported

• Selected features– Only selected features

exported

Example: Exporting features to a new shapefile

Page 12: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-12

Two ways to select features

• Selecting by Attribute– Queries based on one or more fields in the

table• Find all wells with chloride concentration > 100ppm

• Selecting by Location– Queries based on the relationship of features

to another layer• Find all wells that lie inside industrial parcels

Page 13: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-13

Select By Attribute

Some Valid Queries

SELECT *FROM cities WHERE "POP1990" >= 500000

SELECT *FROM counties WHERE “BEEFCOW_92” < “BEEFCOW_87”

SELECT *FROM parcels WHERE “LU-CODE” = 42 AND “VALUE” > 50000

SELECT *FROM rentals WHERE “RENT” > 700 AND “RENT” < 1500

Note: Shapefile tables use quotes for field names; geodatabase tables use brackets

Page 14: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-14

The Like Operator

• “NAME” LIKE ‘%(D)%’ – Finds all of the (D)

Democrats

• % is wildcard• Ignores Don or Danforth

• “NAME” LIKE ‘%New %’ – Would find New Hampshire

and New York, but not Newcastle or Kennewick

Page 15: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-15

Boolean expressions

A AND B

A XOR B A NOT B

A OR B

A B

0

1

Blue circles represent areas where condition holds

(True) = 1

Page 16: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-16

AND vs OR

“Land-use” = ‘RES’ AND

“Land-use” = ‘COM’

“Land-use” = ‘RES’ OR

“Land-use” = ‘COM’

“Pop2000” ≥ 5000 OR

“Pop2000” < 9000

“Pop2000” ≥ 5000 AND

“Pop2000” < 9000

Wrong Right

Remember—you test each feature separately

Page 17: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-17

Multiple conditions

• Use parentheses to enforce order of evaluation

(“LU” = ‘RES’ or “LU” = ‘COM’) and “Value” > 100000

“LU” = ‘RES’ or (“LU” = ‘COM’ and “Value” > 100000)

$75,000

$125,000

$75,000

$125,000

$75,000

$125,000

$75,000

$125,000

RES COM

Page 18: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-18

Select By Location

Select counties that completely contain state capitals

Select counties that are within 200 miles of Denver

Page 19: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-19

Select By Location

Select counties that intersect rivers

Select rivers that intersect Texas

Page 20: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-20

Select By Location

Select cities that are within 20 miles of an interstate highway

Select cities that are completely within counties named Washington

Page 21: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-21

Selecting by location

Page 22: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-22

Intersect

Page 23: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-23

Within distance of

Page 24: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-24

Using a selected set

Page 25: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-25

Within the same layer

Page 26: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-26

Selection methods

Available for all three types of selection

Page 27: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-27

Applying selection methods

Create new selection: hearts = yellow

Add to current selection: hearts = red

Add to current selection: hearts = blue

Remove from selection: hearts = red

Select from selection: hearts = blue

Select from selection: hearts = orange

Page 28: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-28

Select from currentCities within 50mi of earthquakes

Having > 500,000 people

Page 29: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-29

Creating layers

• Common operation following a query• Creates a layer file

Page 30: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-30

Creating layers

Page 31: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-31

Exporting selections

Creates new shapefile

Page 32: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-32

Definition Queries

• Redefine a layer to include a subset of the actual features

• Only selected features appear on map and in table

• Has no effect on data stored on disk• Temporarily treats a layer as being smaller

than it actually is• Does not require an additional data set to

be stored.

Page 33: Copyright © 2006 by Maribeth H. Price 6-1 Chapter 6 Queries

Copyright © 2006 by Maribeth H. Price

6-33