SlideShare uma empresa Scribd logo
1 de 40
Chapter 3:Spatial Query Languages 3.1  Standard Database Query Languages 3.2 Relational Algebra 3.3 Basic SQL Primer 3.4 Extending SQL for Spatial Data 3.5 Example Queries that emphasize spatial aspects 3.6 Trends: Object-Relational SQL
Learning Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What is a query?  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What is a query language? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
An Example World Database ,[object Object],[object Object],[object Object],[object Object],[object Object]
An Example Database - Logical Model ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
World database data tables
Learning Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What is SQL? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Three Components of SQL? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating Tables in SQL ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Populating Tables in SQL ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Querying populated Tables in SQL ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
SELECT Statement- General Information ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
SELECT Example 1. ,[object Object],[object Object],[object Object],[object Object],Result  
SELECT Example 2. ,[object Object],[object Object],[object Object],[object Object],[object Object],Result  
Query Example…Where clause Query:  List the attributes of countries in the  Country  relation where the life-expectancy is less than seventy years. SELECT  Co.Name,Co.Life-Exp FROM  Country Co WHERE  Co.Life-Exp <70 Note: use of alias ‘Co’ for Table ‘Country’ Result  
Multi-table Query Examples Query:  List the capital cities and populations of countries whose GDP exceeds one trillion dollars. Note:Tables City and Country are joined by matching “City.Country = Country.Name”. This simulates relational operator “join” discussed in 3.2 SELECT  Ci.Name,Co.Pop FROM  City Ci,Country Co WHERE  Ci.Country =Co.Name  AND  Co.GDP >1000.0  AND  Ci.Capital=‘Y  ’
Multi-table Query Example Query:  What is the name and population of the capital city in the country where the St. Lawrence River originates? SELECT  Ci.Name, Ci.Pop FROM  City Ci, Country Co, River R WHERE  R.Origin =Co.Name  AND  Co.Name =Ci.Country  AND  R.Name =‘St.Lawrence ’ AND  Ci.Capital=‘Y ’ 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 Examples…Aggregate Staistics 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 ’ Query:  For each continent, find the average GDP. SELECT  Co.Cont,Avg(Co.GDP)AS Continent-GDP FROM  Country Co GROUP BY  Co.Cont
Query Example..Having clause, Nested queries Query:  For each country in which at least two rivers originate, find the length of the smallest river. SELECT  R.Origin,  MIN (R.length)  AS  Min-length FROM  River GROUP BY  R.Origin HAVING   COUNT (*) > 1 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 ’)
Learning Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3.4 Extending SQL for Spatial Data ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
OGIS Spatial Data Model ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Spatial Queries with SQL/OGIS ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
List of Spatial Query Examples ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using spatial operation in SELECT clause Query:  List the name, population, and area of each country listed in the  Country  table. SELECT  C.Name,C.Pop,  Area (C.Shape)AS &quot;Area&quot; FROM  Country C Note: This query uses spatial operation, Area().Note the use of spatial operation in place of a column in SELECT clause.
Using spatial operator Distance Query:  List the GDP and the distance of a country’s capital city to the equator for all countries. SELECT  Co.GDP,  Distance (Point(0,Ci.Shape.y),Ci.Shape) AS &quot;Distance&quot; FROM  Country Co,City Ci WHERE  Co.Name = Ci.Country  AND  Ci.Capital =‘Y ’
Using Spatial Operation in WHERE clause Query:  Find the names of all countries which are neighbors of the United States (USA) in the  Country  table. SELECT  C1.Name AS &quot;Neighbors of USA&quot; FROM  Country C1,Country C2 WHERE   Touch (C1.Shape,C2.Shape)=1  AND  C2.Name =‘USA ’ 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.
Spatial Query with multiple tables Query:  For all the rivers listed in the  River  table, find the countries through which they pass. SELECT  R.Name, C.Name FROM  River R, Country C WHERE   Cross (R.Shape,C.Shape)=1 Note: Spatial operation “Cross” is used to join River and Country tables. This query represents a spatial join operation. Exercise:  Modify above query to report length of river in each country. Hint: Q6, pp. 69
Example Spatial Query…Buffer and Overlap 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 ’ Note: This query uses spatial operation of Buffer, which is illustrated in Figure 3.2 (pp. 69).
Recall List of Spatial Query Examples ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using spatial operation in an aggregate query Query:  List all countries, ordered by number of neighboring countries. SELECT  Co.Name, Count(Co1.Name) FROM  Country Co, Country Co1 WHERE   Touch (Co.Shape,Co1.Shape) GROUP BY  Co.Name ORDER BY   Count (Co1.Name) Notes: This query can be used to differentiate querying capabilities of simple GIS software (e.g. Arc/View) and a spatial database. It is quite tedious to carry out this query in GIS. Earlier version of OGIS did not provide spatial aggregate operation to support GIS operations like reclassify.
Using Spatial Operation in Nested Queries Query:  For each river, identify the closest city. SELECT  C1.Name, R1.Name FROM  City C1, River R1 WHERE   Distance  (C1.Shape,R1.Shape) <= ALL (  SELECT   Distance (C2.Shape) FROM  City C2 WHERE  C1.Name <> C2.Name ) Note: Spatial operation Distance used in context of a nested query.  Exercise: It is interesting to note that SQL query expression to find smallest distance from each river to nearest city is much simpler and does not require nested query. Audience is encouraged to write a SQL expression for this query.
Nested Spatial Query Query:  List the countries with only one neighboring country. A country is a neighbor of another country if their land masses share a boundary. According to this definition, island countries, like Iceland, have no neighbors. 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.
Rewriting nested queries using Views ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],SELECT  Co.Name,num neighbors FROM  Neighbor WHERE  num neighbor = ( SELECT   Max (num neighbors) FROM  Neighbor )
Learning Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Defining Spatial Data Types in SQL3 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Defining Spatial Data Types in SQL3 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Summary ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Mais conteúdo relacionado

Mais procurados

Microsoft Access Notes 2007 Ecdl
Microsoft Access Notes 2007 EcdlMicrosoft Access Notes 2007 Ecdl
Microsoft Access Notes 2007 EcdlRichard Butler
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of dataDimara Hakim
 
Tutorial for using SQL in Microsoft Access
Tutorial for using SQL in Microsoft AccessTutorial for using SQL in Microsoft Access
Tutorial for using SQL in Microsoft Accessmcclellm
 
Intro To TSQL - Unit 5
Intro To TSQL - Unit 5Intro To TSQL - Unit 5
Intro To TSQL - Unit 5iccma
 
SQL For Programmers -- Boston Big Data Techcon April 27th
SQL For Programmers -- Boston Big Data Techcon April 27thSQL For Programmers -- Boston Big Data Techcon April 27th
SQL For Programmers -- Boston Big Data Techcon April 27thDave Stokes
 
Database Design and Normalization Techniques
Database Design and Normalization TechniquesDatabase Design and Normalization Techniques
Database Design and Normalization TechniquesNishant Munjal
 
PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)Buck Woolley
 
Microsoft Access 2007
Microsoft Access 2007Microsoft Access 2007
Microsoft Access 2007Reshma Arun
 
SQL Queries
SQL QueriesSQL Queries
SQL QueriesNilt1234
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQLMahir Haque
 
Structured query language(sql)ppt
Structured query language(sql)pptStructured query language(sql)ppt
Structured query language(sql)pptGowarthini
 
Structure query language (sql)
Structure query language (sql)Structure query language (sql)
Structure query language (sql)Nalina Kumari
 
Relational Model - An Introduction
Relational Model - An IntroductionRelational Model - An Introduction
Relational Model - An IntroductionRajeev Srivastava
 

Mais procurados (20)

Microsoft Access Notes 2007 Ecdl
Microsoft Access Notes 2007 EcdlMicrosoft Access Notes 2007 Ecdl
Microsoft Access Notes 2007 Ecdl
 
Physical elements of data
Physical elements of dataPhysical elements of data
Physical elements of data
 
Ijebea14 228
Ijebea14 228Ijebea14 228
Ijebea14 228
 
Sq lite module2
Sq lite module2Sq lite module2
Sq lite module2
 
Tutorial for using SQL in Microsoft Access
Tutorial for using SQL in Microsoft AccessTutorial for using SQL in Microsoft Access
Tutorial for using SQL in Microsoft Access
 
Intro To TSQL - Unit 5
Intro To TSQL - Unit 5Intro To TSQL - Unit 5
Intro To TSQL - Unit 5
 
SQL For Programmers -- Boston Big Data Techcon April 27th
SQL For Programmers -- Boston Big Data Techcon April 27thSQL For Programmers -- Boston Big Data Techcon April 27th
SQL For Programmers -- Boston Big Data Techcon April 27th
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
Database Design and Normalization Techniques
Database Design and Normalization TechniquesDatabase Design and Normalization Techniques
Database Design and Normalization Techniques
 
Access 2010
Access 2010Access 2010
Access 2010
 
PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)
 
Microsoft Access 2007
Microsoft Access 2007Microsoft Access 2007
Microsoft Access 2007
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Structured query language(sql)ppt
Structured query language(sql)pptStructured query language(sql)ppt
Structured query language(sql)ppt
 
Structure query language (sql)
Structure query language (sql)Structure query language (sql)
Structure query language (sql)
 
Ms access 2007
Ms access 2007Ms access 2007
Ms access 2007
 
Relational Model - An Introduction
Relational Model - An IntroductionRelational Model - An Introduction
Relational Model - An Introduction
 
ADBMS Unit-II b
ADBMS Unit-II bADBMS Unit-II b
ADBMS Unit-II b
 
Cse project-1
Cse project-1Cse project-1
Cse project-1
 

Destaque

Il medico di famiglia e lo straniero
Il medico  di famiglia e lo stranieroIl medico  di famiglia e lo straniero
Il medico di famiglia e lo stranieroGiovanni Pagana
 
La fibrillazione atriale e il medico di famiglia
La fibrillazione atriale e il medico di famigliaLa fibrillazione atriale e il medico di famiglia
La fibrillazione atriale e il medico di famigliaGiovanni Pagana
 
Chapter1.5
Chapter1.5Chapter1.5
Chapter1.5nglaze10
 
2012 Ford Mustang For Sale NE | Ford Dealer Nebraska
2012 Ford Mustang For Sale NE | Ford Dealer Nebraska2012 Ford Mustang For Sale NE | Ford Dealer Nebraska
2012 Ford Mustang For Sale NE | Ford Dealer NebraskaSidDillon Crete
 
Chapter4.9
Chapter4.9Chapter4.9
Chapter4.9nglaze10
 
NAFCU Executive Compensation & Benefits Survey – Initial 2011 Findings (Credi...
NAFCU Executive Compensation & Benefits Survey – Initial 2011 Findings (Credi...NAFCU Executive Compensation & Benefits Survey – Initial 2011 Findings (Credi...
NAFCU Executive Compensation & Benefits Survey – Initial 2011 Findings (Credi...NAFCU Services Corporation
 
Mmg e tromboembolismo nel malato oncologico
Mmg e tromboembolismo nel malato oncologicoMmg e tromboembolismo nel malato oncologico
Mmg e tromboembolismo nel malato oncologicoGiovanni Pagana
 
Medico di famiglia e stroke
Medico di famiglia e strokeMedico di famiglia e stroke
Medico di famiglia e strokeGiovanni Pagana
 
Menjars tradicionals de diferents països
Menjars tradicionals de diferents païsosMenjars tradicionals de diferents països
Menjars tradicionals de diferents païsosestelpastor
 
Youtubers in Czech Republic
Youtubers in Czech RepublicYoutubers in Czech Republic
Youtubers in Czech RepublicTomas Pflanzer
 
Chirurgia refrattiva in medicina generale
Chirurgia refrattiva in medicina generaleChirurgia refrattiva in medicina generale
Chirurgia refrattiva in medicina generaleGiovanni Pagana
 
Project description rus
Project description rusProject description rus
Project description rusZeng Wang
 
Clube dos desportos radicais 2
Clube dos desportos radicais 2Clube dos desportos radicais 2
Clube dos desportos radicais 2Idalecio
 
Eksamen1
Eksamen1Eksamen1
Eksamen1mvaage
 
'Where next for care?' ILC-UK and the Actuarial Profession Day Conference sup...
'Where next for care?' ILC-UK and the Actuarial Profession Day Conference sup...'Where next for care?' ILC-UK and the Actuarial Profession Day Conference sup...
'Where next for care?' ILC-UK and the Actuarial Profession Day Conference sup...ILC- UK
 

Destaque (20)

Recent newsletter sample
Recent newsletter sampleRecent newsletter sample
Recent newsletter sample
 
Gravity
GravityGravity
Gravity
 
Il medico di famiglia e lo straniero
Il medico  di famiglia e lo stranieroIl medico  di famiglia e lo straniero
Il medico di famiglia e lo straniero
 
La fibrillazione atriale e il medico di famiglia
La fibrillazione atriale e il medico di famigliaLa fibrillazione atriale e il medico di famiglia
La fibrillazione atriale e il medico di famiglia
 
9.7
9.79.7
9.7
 
Chapter1.5
Chapter1.5Chapter1.5
Chapter1.5
 
2012 Ford Mustang For Sale NE | Ford Dealer Nebraska
2012 Ford Mustang For Sale NE | Ford Dealer Nebraska2012 Ford Mustang For Sale NE | Ford Dealer Nebraska
2012 Ford Mustang For Sale NE | Ford Dealer Nebraska
 
Chapter4.9
Chapter4.9Chapter4.9
Chapter4.9
 
NAFCU Executive Compensation & Benefits Survey – Initial 2011 Findings (Credi...
NAFCU Executive Compensation & Benefits Survey – Initial 2011 Findings (Credi...NAFCU Executive Compensation & Benefits Survey – Initial 2011 Findings (Credi...
NAFCU Executive Compensation & Benefits Survey – Initial 2011 Findings (Credi...
 
Mmg e tromboembolismo nel malato oncologico
Mmg e tromboembolismo nel malato oncologicoMmg e tromboembolismo nel malato oncologico
Mmg e tromboembolismo nel malato oncologico
 
Medico di famiglia e stroke
Medico di famiglia e strokeMedico di famiglia e stroke
Medico di famiglia e stroke
 
Menjars tradicionals de diferents països
Menjars tradicionals de diferents païsosMenjars tradicionals de diferents països
Menjars tradicionals de diferents països
 
Youtubers in Czech Republic
Youtubers in Czech RepublicYoutubers in Czech Republic
Youtubers in Czech Republic
 
Denobia olegba
Denobia olegbaDenobia olegba
Denobia olegba
 
Chirurgia refrattiva in medicina generale
Chirurgia refrattiva in medicina generaleChirurgia refrattiva in medicina generale
Chirurgia refrattiva in medicina generale
 
Aji amarillo
Aji amarilloAji amarillo
Aji amarillo
 
Project description rus
Project description rusProject description rus
Project description rus
 
Clube dos desportos radicais 2
Clube dos desportos radicais 2Clube dos desportos radicais 2
Clube dos desportos radicais 2
 
Eksamen1
Eksamen1Eksamen1
Eksamen1
 
'Where next for care?' ILC-UK and the Actuarial Profession Day Conference sup...
'Where next for care?' ILC-UK and the Actuarial Profession Day Conference sup...'Where next for care?' ILC-UK and the Actuarial Profession Day Conference sup...
'Where next for care?' ILC-UK and the Actuarial Profession Day Conference sup...
 

Semelhante a Ch3rerevised

The Road to U-SQL: Experiences in Language Design (SQL Konferenz 2017 Keynote)
The Road to U-SQL: Experiences in Language Design (SQL Konferenz 2017 Keynote)The Road to U-SQL: Experiences in Language Design (SQL Konferenz 2017 Keynote)
The Road to U-SQL: Experiences in Language Design (SQL Konferenz 2017 Keynote)Michael Rys
 
Text tagging with finite state transducers
Text tagging with finite state transducersText tagging with finite state transducers
Text tagging with finite state transducerslucenerevolution
 
Database Management Lab -SQL Queries
Database Management Lab -SQL Queries Database Management Lab -SQL Queries
Database Management Lab -SQL Queries shamim hossain
 
SQL (Basic to Intermediate Customized 8 Hours)
SQL (Basic to Intermediate Customized 8 Hours)SQL (Basic to Intermediate Customized 8 Hours)
SQL (Basic to Intermediate Customized 8 Hours)Edu4Sure
 
Unit 3 rdbms study_materials-converted
Unit 3  rdbms study_materials-convertedUnit 3  rdbms study_materials-converted
Unit 3 rdbms study_materials-convertedgayaramesh
 
MIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome MeasuresMIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome MeasuresSteven Johnson
 
DATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEMDATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEMSonia Pahuja
 
SQL Query Interview Questions
SQL Query Interview QuestionsSQL Query Interview Questions
SQL Query Interview Questionssoniajessica2
 
SQL is Dead; Long Live SQL: Lightweight Query Services for Long Tail Science
SQL is Dead; Long Live SQL: Lightweight Query Services for Long Tail ScienceSQL is Dead; Long Live SQL: Lightweight Query Services for Long Tail Science
SQL is Dead; Long Live SQL: Lightweight Query Services for Long Tail ScienceUniversity of Washington
 

Semelhante a Ch3rerevised (20)

Ch1revised
Ch1revisedCh1revised
Ch1revised
 
Spatial Databases
Spatial DatabasesSpatial Databases
Spatial Databases
 
The Road to U-SQL: Experiences in Language Design (SQL Konferenz 2017 Keynote)
The Road to U-SQL: Experiences in Language Design (SQL Konferenz 2017 Keynote)The Road to U-SQL: Experiences in Language Design (SQL Konferenz 2017 Keynote)
The Road to U-SQL: Experiences in Language Design (SQL Konferenz 2017 Keynote)
 
Text tagging with finite state transducers
Text tagging with finite state transducersText tagging with finite state transducers
Text tagging with finite state transducers
 
Database Management Lab -SQL Queries
Database Management Lab -SQL Queries Database Management Lab -SQL Queries
Database Management Lab -SQL Queries
 
2 rel-algebra
2 rel-algebra2 rel-algebra
2 rel-algebra
 
Session 6#
Session 6#Session 6#
Session 6#
 
Lect11
Lect11Lect11
Lect11
 
SQL (Basic to Intermediate Customized 8 Hours)
SQL (Basic to Intermediate Customized 8 Hours)SQL (Basic to Intermediate Customized 8 Hours)
SQL (Basic to Intermediate Customized 8 Hours)
 
ch3
ch3ch3
ch3
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
PT- Oracle session01
PT- Oracle session01 PT- Oracle session01
PT- Oracle session01
 
Unit 3 rdbms study_materials-converted
Unit 3  rdbms study_materials-convertedUnit 3  rdbms study_materials-converted
Unit 3 rdbms study_materials-converted
 
12 SQL
12 SQL12 SQL
12 SQL
 
12 SQL
12 SQL12 SQL
12 SQL
 
MIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome MeasuresMIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome Measures
 
DATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEMDATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEM
 
SQL Query Interview Questions
SQL Query Interview QuestionsSQL Query Interview Questions
SQL Query Interview Questions
 
Project Report
Project ReportProject Report
Project Report
 
SQL is Dead; Long Live SQL: Lightweight Query Services for Long Tail Science
SQL is Dead; Long Live SQL: Lightweight Query Services for Long Tail ScienceSQL is Dead; Long Live SQL: Lightweight Query Services for Long Tail Science
SQL is Dead; Long Live SQL: Lightweight Query Services for Long Tail Science
 

Ch3rerevised

  • 1. Chapter 3:Spatial Query Languages 3.1 Standard Database Query Languages 3.2 Relational Algebra 3.3 Basic SQL Primer 3.4 Extending SQL for Spatial Data 3.5 Example Queries that emphasize spatial aspects 3.6 Trends: Object-Relational SQL
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17. Query Example…Where clause Query: List the attributes of countries in the Country relation where the life-expectancy is less than seventy years. SELECT Co.Name,Co.Life-Exp FROM Country Co WHERE Co.Life-Exp <70 Note: use of alias ‘Co’ for Table ‘Country’ Result 
  • 18. Multi-table Query Examples Query: List the capital cities and populations of countries whose GDP exceeds one trillion dollars. Note:Tables City and Country are joined by matching “City.Country = Country.Name”. This simulates relational operator “join” discussed in 3.2 SELECT Ci.Name,Co.Pop FROM City Ci,Country Co WHERE Ci.Country =Co.Name AND Co.GDP >1000.0 AND Ci.Capital=‘Y ’
  • 19. Multi-table Query Example Query: What is the name and population of the capital city in the country where the St. Lawrence River originates? SELECT Ci.Name, Ci.Pop FROM City Ci, Country Co, River R WHERE R.Origin =Co.Name AND Co.Name =Ci.Country AND R.Name =‘St.Lawrence ’ AND Ci.Capital=‘Y ’ 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.
  • 20. Query Examples…Aggregate Staistics 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 ’ Query: For each continent, find the average GDP. SELECT Co.Cont,Avg(Co.GDP)AS Continent-GDP FROM Country Co GROUP BY Co.Cont
  • 21. Query Example..Having clause, Nested queries Query: For each country in which at least two rivers originate, find the length of the smallest river. SELECT R.Origin, MIN (R.length) AS Min-length FROM River GROUP BY R.Origin HAVING COUNT (*) > 1 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 ’)
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27. Using spatial operation in SELECT clause Query: List the name, population, and area of each country listed in the Country table. SELECT C.Name,C.Pop, Area (C.Shape)AS &quot;Area&quot; FROM Country C Note: This query uses spatial operation, Area().Note the use of spatial operation in place of a column in SELECT clause.
  • 28. Using spatial operator Distance Query: List the GDP and the distance of a country’s capital city to the equator for all countries. SELECT Co.GDP, Distance (Point(0,Ci.Shape.y),Ci.Shape) AS &quot;Distance&quot; FROM Country Co,City Ci WHERE Co.Name = Ci.Country AND Ci.Capital =‘Y ’
  • 29. Using Spatial Operation in WHERE clause Query: Find the names of all countries which are neighbors of the United States (USA) in the Country table. SELECT C1.Name AS &quot;Neighbors of USA&quot; FROM Country C1,Country C2 WHERE Touch (C1.Shape,C2.Shape)=1 AND C2.Name =‘USA ’ 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.
  • 30. Spatial Query with multiple tables Query: For all the rivers listed in the River table, find the countries through which they pass. SELECT R.Name, C.Name FROM River R, Country C WHERE Cross (R.Shape,C.Shape)=1 Note: Spatial operation “Cross” is used to join River and Country tables. This query represents a spatial join operation. Exercise: Modify above query to report length of river in each country. Hint: Q6, pp. 69
  • 31. Example Spatial Query…Buffer and Overlap 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 ’ Note: This query uses spatial operation of Buffer, which is illustrated in Figure 3.2 (pp. 69).
  • 32.
  • 33. Using spatial operation in an aggregate query Query: List all countries, ordered by number of neighboring countries. SELECT Co.Name, Count(Co1.Name) FROM Country Co, Country Co1 WHERE Touch (Co.Shape,Co1.Shape) GROUP BY Co.Name ORDER BY Count (Co1.Name) Notes: This query can be used to differentiate querying capabilities of simple GIS software (e.g. Arc/View) and a spatial database. It is quite tedious to carry out this query in GIS. Earlier version of OGIS did not provide spatial aggregate operation to support GIS operations like reclassify.
  • 34. Using Spatial Operation in Nested Queries Query: For each river, identify the closest city. SELECT C1.Name, R1.Name FROM City C1, River R1 WHERE Distance (C1.Shape,R1.Shape) <= ALL ( SELECT Distance (C2.Shape) FROM City C2 WHERE C1.Name <> C2.Name ) Note: Spatial operation Distance used in context of a nested query. Exercise: It is interesting to note that SQL query expression to find smallest distance from each river to nearest city is much simpler and does not require nested query. Audience is encouraged to write a SQL expression for this query.
  • 35. Nested Spatial Query Query: List the countries with only one neighboring country. A country is a neighbor of another country if their land masses share a boundary. According to this definition, island countries, like Iceland, have no neighbors. 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.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.