SPARQL - Unable to filter by country - sparql

I know this is an easy question to answer, but I have been googling this for a few hours without success. Why does the following SPARQL being entered in http://dbpedia.org/snorql/ not return anything?
prefix dbo: <http://dbpedia.org/ontology/>
SELECT ?country ?city ?city_name ?country_name
WHERE {
?city rdf:type dbo:City .
?city foaf:name ?city_name .
?city dbo:country ?country .
FILTER(?country=dbpedia:Canada).
?country foaf:name ?country_name .
}
LIMIT 200
I also tried -with the same results (i.e. none)
prefix dbo: <http://dbpedia.org/ontology/>
SELECT ?country ?city ?city_name
WHERE {
?city rdf:type dbo:City .
?city foaf:name ?city_name .
?city dbo:country ?country .
?country foaf:name ?country_name .
FILTER(langMatches(lang(?country_name),"EN") && ?country_name="Canada")
}
LIMIT 200

No need for filters for an equality.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?country ?city ?city_name
WHERE {
?city rdf:type dbo:City ;
foaf:name ?city_name ;
dbo:country ?country .
?country foaf:name "Canada"#en .
FILTER(langMatches(lang(?city_name), "en"))
}
ORDER BY ?city_name
LIMIT 100
Test : http://linkedwiki.com/query/Filter_cities_by_country_with_DBpedia

Figured it out with some further digging.
To get the first query digging, I was using the wrong prefix. I needed to use <http://dbpedia.org/Resource> not the ontology one. This comes pre-defined in snorql - but it didn't actually have a prefix. But with some fiddling the following worked.
prefix dbo: <http://dbpedia.org/ontology/>
SELECT ?country ?city ?city_name ?country_name
WHERE {
?city rdf:type dbo:City .
?city foaf:name ?city_name .
?city dbo:country ?country .
FILTER(?country=:Canada).
?country foaf:name ?country_name .
}
LIMIT 200
As for matching on name, apparently you can't just match on a literal when using foaf. You need to add the language tag:
prefix dbo: <http://dbpedia.org/ontology/>
SELECT ?country ?city ?city_name
WHERE {
?city rdf:type dbo:City .
?city foaf:name ?city_name .
?city dbo:country ?country .
?country foaf:name ?country_name .
FILTER(langMatches(lang(?country_name),"EN") && ?country_name="Canada"#en)
}
LIMIT 200

Related

List countries from DBpedia

Trying to query DBpedia for a list of all countries with the dbo:longName property and the capital of each country listed but get 0 results returned. Can't see whats wrong with the query.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?country a dbo:longName ;
dbo:capital ?capital .
}
What's missing?
You are missing that ?country has a rdf:type of dbo:Country and not dbo:longName. The right query should be:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?x a dbo:Country.
?x dbo:longName ?country.
?x dbp:capital ?capital
}
Update
Based on your comment you want the URI's of the country and their capital. Therefore, you don't need dbo:longName, because you don't want the country label name. You will select the instances:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?country a dbo:Country.
?country dbo:capital ?capital
}
Note that results will bring countries that are extinct. If you want to filter countries that have ended, you should get this result with:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?country a dbo:Country.
?country dbo:capital ?capital.
FILTER NOT EXISTS { ?country dbo:dissolutionYear ?yearEnd }
}

SPARQL Query for all Books by George Orwell

I created this query to return all books that are notable works by George Orwell but it returns no result.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?title where {
?person foaf:name ?name .
?title dbo:author ?person .
?title dbo:notableWork dbp:George_Orwell .
}
I cannot seem to figure out why there is no result.
I am running the query in http://dbpedia.org/snorql
Don't you have the triples about notable works in the wrong order?
Try rewriting based on this working query
SELECT *
WHERE {
:George_Orwell dbo:notableWork ?title
}
.
title
:Nineteen_Eighty-Four
:Animal_Farm
You can also bind :George_Orwell to a variable and ask more about that:
SELECT *
WHERE {
values ?author { :George_Orwell } .
?author rdfs:label ?l .
?title ?p ?author .
?title rdf:type dbo:Book .
filter (lang(?l) = "en")
}
and DESCRIBE things
describe :Animal_Farm

Query SPARQL doesn't work with "dct:subject"

SELECT ?name ?birth ?person ?subject
WHERE { ?person dbo:birthPlace :London . ?person
dbo:birthDate ?birth . ?person foaf:name ?name .
?person dct:subject :English_rock_singers. ?person dct:subject ?
subject. } ORDER BY ?name
This query works only if I delete the "subject".
Is there a way to query dbpedia with a category?
Though your question is not clear, I think your problem is with formatting the query and defining the prefixes. This should work:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
SELECT ?name ?birth ?person ?subject WHERE {
?person dbo:birthPlace dbr:London.
?person dbo:birthDate ?birth.
?person foaf:name ?name.
?person dct:subject dbc:English_rock_singers.
?person dct:subject ?subject.
}
ORDER BY ?name

sparql query is not returning desired output

This is my sparql query to get all the country name with some property of that country.
SELECT distinct ?country ?capital ?currency ?lat ?long
WHERE {
?country rdf:type dbo:Country .
?country dbo:capital ?capital .
?country dbo:currency ?currency .
?country geo:lat ?lat.
?country geo:long ?long.
}
ORDER BY ?country
But the problem is, Some country is missing, like "Switzerland". You go to http://dbpedia.org/page/Switzerland this page you will see that it's type is country. you will also do not find exactly "Austria" rather "Austrian_Empire". Why? There is a entity called "Austria" and it is dbo:Country type.
Switzerland does not seem to have dbo:capital, which is why it's not included in the results of your query.
If you want to get even results that do not have some of the properties, use OPTIONAL:
SELECT distinct ?country ?capital ?currency ?lat ?long
WHERE {
?country rdf:type dbo:Country .
OPTIONAL
{
?country dbo:capital ?capital .
?country dbo:currency ?currency .
?country geo:lat ?lat.
?country geo:long ?long.
}
}
ORDER BY ?country
Though this query returns even entities that are not countries (but for some reason are dbo:Country), like Cinema of Switzerland.

How do get city population by name with SPARQL

I need to get city population by its name. I'm sure SPARQL could provide this, but I'm not sure how to write the query. I have a query which provides COuntry's capital and coordinates by it's name, so I assume it should be something similar. HELP!
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?country ?population ?capital ?lat ?long WHERE {
?country a o:Country ; foaf:name "Germany"#en; o:capital [ geo:lat ?lat ; geo:long ?long ; p:name ?capital ]
}
All you need is:
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?pop WHERE {
?country a o:Country ;
foaf:name ?name ;
p:populationEstimate ?pop .
}
The only tricky part was finding out the name of the property that links Countries and Populations.