Docsity

SQL and Spatial Queries in Geography: University Course at UMD, Spring 2009 - Prof. Kuan S, Study notes of Geography

Partial preview of the text

Download SQL and Spatial Queries in Geography: University Course at UMD, Spring 2009 - Prof. Kuan S and more Study notes Geography in PDF only on Docsity!

Spring 2009Geog 473 GIS and Spatial Analysis

Spatial Databases:

SQL and Selection

Kuan Song

Department of Geography

University of Maryland

February 9, 2009

Outline

  • Structured Query Language (SQL)• Querying non-spatial data in a single relation• Querying non-spatial data in multiple relations• Querying spatial data• Querying spatial relationships

2.1 An example:

PublicSchools

2. Querying non-spatial data in a singlerelation (table)

Query: find all public elementary schools.

2.2 Structured Query Language (SQL) • One problem of file-processing systems: difficulty in

accessing data.

  • SQL is a standard computer language for accessing

and manipulating relational or object-relationaldatabases.

  • Created in the 1970s, first known as SEQUEL• SQL-1986 and SQL

2.4 SQL Examples

Find all information of the public elementary schools

in PublicSchools. SELECT *FROM PublicSchoolsWHERE category='public elementary school'

The SELECT clause can present all columns or asubset of columns in query results.•

Find the school names in PublicSchools.

SELECT school_name

FROM PublicSchools

•OR in WHERE clause

SELECT *FROM PublicSchoolsWHERE category='public elementary school'

OR city=' Adelphi'

•NOT in WHERE clause

SELECT street, cityFROM PublicSchoolsWHERE category='public elementary school'

AND NOT city=' Adelphi'

The WHERE clause can have multiple logical connectivesusing AND, OR, NOT

Using comparison operators in WHERE clause

Laurel

Laurel

Allenwood

Allenwood

Apple Grove

Apple Grove

StudentNumber

StudentNumber

SchoolName

SchoolName

Enrollment = schema (schoolname, studentnumber)

3. Querying non-spatial data inmultiple tables

4300 Wicomico Ave 4300 Wicomico Ave

Beltsville Beltsville

Beltsville Beltsville

6300 Harley 6300 Harley Ln

Ln

Temple Hills Temple Hills

Allenwood Allenwood

8802 Riggs Rd 8802 Riggs Rd

Adelphi Adelphi

Adelphi Adelphi

street street

city city

school_nam school_nam

Laurel Laurel

Apple Grove Apple Grove

Allenwood Allenwood

studentnumber studentnumber

schoolname schoolname

Find the cities havingschools with less than200 students, list thecity names, schoolnames and enrollment.

PublicSchools

Enrollment

3.1 Example

SELECT publicschools.*, enrollment.studentnumberFROM

publicschools, enrollment

WHERE publicschools.school_name =enrollment.schoolname

AND enrollment.studentnumber < 200

  • The FROM clause joins two tables• The SELECT and WHERE clauses must specify

which table each column belongs to.

3.2 Join type •Inner join:

Only matched tuples are presented in the result.

Attributes of the left-hand-side table appears firstfollowed by the attributes of the right-hand-side table.

Allenwood Allenwood

6300 Harley 6300 Harley

Ln Ln

Temple Hills Temple Hills

Allenwood Allenwood

studentnumber studentnumber

schoolname schoolname

street street

city city

school_nam school_nam

SELECT Publicschools., Enrollment.FROM Publicschools INNER JOIN Enrollment ONPublicschools.School_name=Enrollment.schoolname;

  • Left join:

The result of the inner join is computed.

For every tuple

t

in the left-hand-side table that did not

match any tuple in the right-hand-side table, theattributes values of tuple

t

is added to the result of join

and the values of the attributes from the right-hand-side relation are set null.

The LEFT JOIN keyword returns all rows from the lefttable , even if there are no matches in the right table.

SELECT Publicschools., Enrollment.FROM Publicschools LEFT JOIN Enrollment ONPublicschools.School_name=Enrollment.schoolname;

Right join:

The result of the inner join is computed.

Symmetric to the left join.

For every tuple

r

in the right-hand-side table that did

not match any tuple in the left-hand-side table, theattributes of tuple

r

is added to the result of join and

the values of the attributes from the left-hand-sidetable are set null.

The RIGHT JOIN keyword Return all rows from theright table , even if there are no matches in the lefttable.

Laurel Laurel

null null

null null

null null

Apple Grove Apple Grove

null null

null null

null null

Allenwood Allenwood

6300 Harley 6300 Harley Ln

Ln

Temple Temple

Hills Hills

Allenwood Allenwood

studentnumber studentnumber

schoolname schoolname

street street

city city

school_nam school_nam

Right out join