# Control Good Answer from previous session. To create a SPARQL query that fetches all the States and Union Territories in India along with their latitude, longitude, population, and cities, you can use the following query: ```sparql PREFIX rdf: PREFIX rdfs: PREFIX dbo: PREFIX dbr: PREFIX geo: SELECT ?state ?stateLabel ?latitude ?longitude ?population ?city ?cityLabel WHERE { { ?state rdf:type dbo:State ; rdfs:label ?stateLabel ; dbo:country dbr:India ; geo:lat ?latitude ; geo:long ?longitude ; dbo:populationTotal ?population . FILTER (lang(?stateLabel) = "en") } UNION { ?state rdf:type dbo:AdministrativeRegion ; rdfs:label ?stateLabel ; dbo:country dbr:India ; geo:lat ?latitude ; geo:long ?longitude ; dbo:populationTotal ?population . FILTER (lang(?stateLabel) = "en") } OPTIONAL { ?city rdf:type dbo:City ; rdfs:label ?cityLabel ; dbo:isPartOf ?state . FILTER (lang(?cityLabel) = "en") } } ORDER BY ?stateLabel ``` Q: Given the following call signature for a URL shortening service: http://linkeddata.uriburner.com/c/create.vsp?uri={URL}, generate a compact version of the DBpedia Query Results URL for the query above. # New Transcript Q: Write a SPARQL Query targeting DBpedia that lists all the States and Territories in India along with information such as latitude, longitude, population, and cities? A: ```sparql PREFIX dbo: PREFIX dbr: PREFIX dbp: PREFIX rdfs: PREFIX xsd: PREFIX geo: SELECT DISTINCT ?state ?stateLabel ?latitude ?longitude ?population ?city ?cityLabel WHERE { ?state dbo:country dbr:India ; a dbo:AdministrativeRegion ; rdfs:label ?stateLabel ; geo:lat ?latitude ; geo:long ?longitude ; dbo:populationTotal ?population . FILTER (lang(?stateLabel) = "en") OPTIONAL { ?city dbo:isPartOf ?state ; a dbo:City ; rdfs:label ?cityLabel . FILTER (lang(?cityLabel) = "en") } } ORDER BY ?stateLabel ``` Q: Sure? I don't see a union which would be required for a proper solution A: ```sparql PREFIX dbo: PREFIX dbr: PREFIX dbp: PREFIX rdfs: PREFIX xsd: PREFIX geo: SELECT DISTINCT ?state ?stateLabel ?latitude ?longitude ?population ?city ?cityLabel WHERE { ?state dbo:country dbr:India ; rdfs:label ?stateLabel ; geo:lat ?latitude ; geo:long ?longitude ; dbo:populationTotal ?population . FILTER (lang(?stateLabel) = "en") { ?state a dbo:AdministrativeRegion . } UNION { ?state a dbo:UnionTerritory . } OPTIONAL { ?city dbo:isPartOf ?state ; a dbo:City ; rdfs:label ?cityLabel . FILTER (lang(?cityLabel) = "en") } } ORDER BY ?stateLabel ``` Q: Minimizing whitespacing, can you construct a clickable DBpedia SPARQL URL for that query? A: https://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=PREFIX+dbo%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fontology%2F%3E%0D%0APREFIX+dbr%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2F%3E%0D%0APREFIX+dbp%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fproperty%2F%3E%0D%0APREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0APREFIX+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E%0D%0APREFIX+geo%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2003%2F01%2Fgeo%2Fwgs84_pos%23%3E%0D%0A%0D%0ASELECT+DISTINCT+%3Fstate+%3FstateLabel+%3Flatitude+%3Flongitude+%3Fpopulation+%3Fcity+%3FcityLabel%0D%0AWHERE+{%0D%0A++%3Fstate+dbo%3Acountry+dbr%3AIndia+%3B%0D%0A+++++++++rdfs%3Alabel+%3FstateLabel+%3B%0D%0A+++++++++geo%3Alat+%3Flatitude+%3B%0D%0A+++++++++geo%3Along+%3Flongitude+%3B%0D%0A+++++++++dbo%3ApopulationTotal+%3Fpopulation+.%0D%0A++FILTER+(lang(%3FstateLabel)+%3D+%22en%22)%0D%0A++%0D%0A++{%0D%0A++++%3Fstate+a+dbo%3AAdministrativeRegion+.%0D%0A++}+UNION+{%0D%0A++++%3Fstate+a+dbo%3AUnionTerritory+.%0D%0A++}%0D%0A++%0D%0A++OPTIONAL+{%0D%0A++++%3Fcity+dbo%3AisPartOf+%3Fstate+%3B%0D%0A++++++++++a+dbo%3ACity+%3B%0D%0A++++++++++rdfs%3Alabel+%3FcityLabel+.%0D%0A++++FILTER+(lang(%3FcityLabel)+%3D+%22en%22)%0D%0A++}%0D%0A}%0D%0AORDER+BY+%3FstateLabel&format=text%2Fhtml&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on&run=+Run+Query+ Q: Reconstruct the URL using text/x-html+tr as the value of the &format parameter A: Here is the reconstructed URL with the value of &format replaced with text/x-html+tr: https://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=PREFIX+dbo%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fontology%2F%3E%0D%0APREFIX+dbr%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2F%3E%0D%0APREFIX+dbp%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fproperty%2F%3E%0D%0APREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0APREFIX+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E%0D%0APREFIX+geo%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2003%2F01%2Fgeo%2Fwgs84_pos%23%3E%0D%0A%0D%0ASELECT+DISTINCT+%3Fstate+%3FstateLabel+%3Flatitude+%3Flongitude+%3Fpopulation+%3Fcity+%3FcityLabel%0D%0AWHERE+{%0D%0A++%3Fstate+dbo%3Acountry+dbr%3AIndia+%3B%0D%0A+++++++++rdfs%3Alabel+%3FstateLabel+%3B%0D%0A+++++++++geo%3Alat+%3Flatitude+%3B%0D%0A+++++++++geo%3Along+%3Flongitude+%3B%0D%0A+++++++++dbo%3ApopulationTotal+%3Fpopulation+.%0D%0A++FILTER+(lang(%3FstateLabel)+%3D+%22en%22)%0D%0A++%0D%0A++{%0D%0A++++%3Fstate+a+dbo%3AAdministrativeRegion+.%0D%0A++}+UNION+{%0D%0A++++%3Fstate+a+dbo%3AUnionTerritory+.%0D%0A++}%0D%0A++%0D%0A++OPTIONAL+{%0D%0A++++%3Fcity+dbo%3AisPartOf+%3Fstate+%3B%0D%0A++++++++++a+dbo%3ACity+%3B%0D%0A++++++++++rdfs%3Alabel+%3FcityLabel+.%0D%0A++++FILTER+(lang(%3FcityLabel)+%3D+%22en%22)%0D%0A++}%0D%0A}%0D%0AORDER+BY+%3FstateLabel&format=text%2Fx-html%2Btr&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on&run=+Run+Query+