Spatial Query Language
Spatial Query Language
What is a query?
What is a Query ?
A query is a “question” posed to a database
Queries are expressed in a high-level declarative manner
• Algorithms needed to answer the query are not specified in the query
Examples:
Mouse click on a map symbol (e.g. road) may mean
• What is the name of road pointed to by mouse cursor ?
Typing a keyword in a search engine (e.g. google, yahoo) means
• Which documents on web contain given keywords?
SELECT S.name FROM Senator S WHERE S.gender = ‘F’ means
• Which senators are female?
1
13-09-2023
CONTINENT
NAME
LIFE-EXP
POPULATION POPULATION
CAPITAL
NAME
CAPITAL-OF ORIGINATES
CITY COUNTRY RIVER
GDP LENGTH
NAME
2
13-09-2023
•3 Relations
Country(Name, Cont, Pop, GDP, Life-Exp, Shape)
City(Name, Country, Pop,Capital, Shape)
River(Name, Origin, Length, Shape)
• Keys
• Primary keys are Country.Name, City.Name, River.Name
• Foreign keys are River.Origin, City.Country
3
13-09-2023
What is SQL?
4
13-09-2023
• Related statements
• SELECT statement with INTO clause can insert multiple rows in a table
• Bulk load, import commands also add multiple rows
• DELETE statement removes rows
•UPDATE statement can change values within selected rows
5
13-09-2023
6
13-09-2023
SELECT Example 1.
• Simplest Query has SELECT and FROM clauses
• Query: List all the cities and the country they belong to.
Result
SELECT Example 2.
• Commonly 3 clauses (SELECT, FROM, WHERE) are used
•Query: List the names of the capital cities in the CITY table.
SELECT *
FROM CITY
WHERE CAPITAL=‘Y ’
Result
7
13-09-2023
SELECT Co.Name,Co.Life‐Exp
FROM Country Co
WHERE Co.Life‐Exp <70
Result
SELECT Ci.Name,Co.Pop
FROM City Ci,Country Co
WHERE Ci.Country =Co.Name
AND Co.GDP >1000.0
AND Ci.Capital=‘Y ’
8
13-09-2023
Note: Three tables are joined together pair at a time. River.Origin is matched
with Country.Name and City.Country is matched with Country.Name. The
order of join is decided by query optimizer and does not affect the result.
Query: What is the average population of the noncapital cities listed in the
City table?
SELECT AVG(Ci.Pop)
FROM City Ci
WHERE Ci.Capital=‘N ’
9
13-09-2023
Query: For each country in which at least two rivers originate, find the length
of the smallest river.
Query: List the countries whose GDP is greater than that of Canada.
SELECT Co.Name
FROM Country Co
WHERE Co.GDP >ANY (SELECT Co1.GDP
FROM Country Co1
WHERE Co1.Name =‘Canada ’)
Motivation
SQL has simple atomic data-types, like integer, dates and string
Not convenient for spatial data and queries
• Spatial data (e.g. polygons) is complex
• Spatial operation: topological, euclidean, directional, metric
SQL 3 allows user defined data types and operations
Spatial data types and operations can be added to SQL3
Open Geodata Interchange Standard (OGIS)
Half a dozen spatial data types
Several spatial operations
Supported by major vendors, e.g. ESRI, Intergraph, Oracle, IBM,...
10
13-09-2023
11
13-09-2023
12
13-09-2023
Note: Spatial operator Touch() is used in WHERE clause to join Country table
with itself. This query is an example of spatial self join operation.
13
13-09-2023
Note: Spatial operation “Cross” is used to join River and Country tables. This
query represents a spatial join operation.
Query: The St. Lawrence River can supply water to cities that are
within 300 km. List the cities that can use water from the St.
Lawrence.
SELECT Ci.Name
FROM City Ci, River R
WHERE Overlap(Ci.Shape, Buffer(R.Shape,300))=1
AND R.Name =‘St.Lawrence ’
14
13-09-2023
Earlier version of OGIS did not provide spatial aggregate operation to support
GIS operations like reclassify.
15
13-09-2023
SELECT Co.Name
FROM Country Co
WHERE Co.Name IN (SELECT Co.Name
FROM Country Co,Country Co1
WHERE Touch(Co.Shape,Co1.Shape)
GROUP BY Co.Name
HAVING Count(*)=1)
Note: It shows a complex nested query with aggregate operations. Such queries can be written into
two expression, namely a view definition, and a query on the view. The inner query becomes a view
and outer query is runon the view. This is illustrated in the next slide.
16
13-09-2023
17
13-09-2023
Summary
Queries to databases are posed in high level declarative manner
SQL is the “lingua-franca” in the commercial database world
Standard SQL operates on relatively simple data types
SQL3/OGIS supports several spatial data types and operations
Additional spatial data types and operations can be defined
CREATE TYPE statement
18