I have the following SPARQL query to get the list of countries with the smallest density of population per km and their presidents (leaders):
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?country_name ?populationdensity ?leader
WHERE {
?country a dbpedia-owl:Country ;
rdfs:label ?country_name ;
prop:populationDensityKm ?populationdensity ;
dbpedia-owl:leader ?leader .
FILTER (?populationdensity < 10 && langMatches(lang(?country_name), "en")) .
}
GROUP BY ?populationdensity
ORDER BY ASC(?populationdensity)
limit 10
As you can see, I am grouping results by population density, yet I am getting results which include numerous population densities duplicates: SPARQL Query
Can someone tell me what am I doing wrong?
I assume it has something to do with list of leaders, where for each country more than one is return.
Is there a way to limit that to 1 leader per country somehow?
The first thing is that you should put all variables you use in the group by clause.
Virtuoso currently is loose in its parsing of queries and allows things it should not.
The second is you need to select just one leader, if you don't care which one then you should use SAMPLE. If you want all of them then use a group_concat variation.
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?country_name ?populationdensity (sample(?leader) as ?ls)
WHERE {
?country a dbpedia-owl:Country ;
rdfs:label ?country_name ;
prop:populationDensityKm ?populationdensity ;
dbpedia-owl:leader ?leader .
FILTER (?populationdensity < 10 && langMatches(lang(?country_name), "en")) .
}
GROUP BY ?country_name ?populationdensity
ORDER BY ASC(?populationdensity)
limit 10
If you want the current leader you need to replace the line
dbpedia-owl:leader ?leader .
With this
dbpprop:leaderTitle/dbpprop:incumbent ?leader .
Related
Im a using Virtuoso and DBpedia as an endpoint.
My purpose is to retrieve all movies which have a greater amount of actor than the mean number of actors for all movies.
I thought the following query would work:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT
DISTINCT ?film
COUNT(?actor) AS ?numActors
WHERE{
?film rdf:type dbp:Film .
?film dbp:starring ?actor .
{
SELECT
AVG(?numActors) AS ?avgNumActors
WHERE{
SELECT
?Sfilm
COUNT(?Sactor) AS ?numActors
WHERE{
?Sfilm rdf:type dbp:Film .
?Sfilm dbp:starring ?Sactor
}
}
}
}
GROUP BY ?film
HAVING (COUNT(?actor) > ?avgNumActors)
LIMIT 20
but I receveice the following error
Variable ?avgNumActors is used in the result set outside aggregate and not mentioned in GROUP BY clause
What am I doing wrong?
I'm new to SPARQL and I'm a bit stuck on a part of an assignment I have. I'm querying dbpedia for all the countries in the European Union which have a total population >= 3000000. For each country I'd like to show their corresponding government type. I would like the final result set to contain 3 columns:
the English label for each country
the English label for each type of government
the total population value
and then sorted descending on the total population.
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbrc: <http://dbpedia.org/resource/Category:>
SELECT DISTINCT ?country xsd:integer(?populationTotal) ?government_type ?engName ?gov_type
WHERE {
?country dct:subject dbrc:Member_states_of_the_European_Union ;
a dbo:Country ;
rdfs:label ?engName .
OPTIONAL { ?country dbo:governmentType ?government_type . ?government_type rdfs:label ?gov_type . }
OPTIONAL { ?country dbo:populationTotal ?populationTotal . }
FILTER (xsd:integer(?populationTotal) >= 3000000 && langMatches(lang(?engName), "en"))
FILTER (langMatches(lang(?gov_type), "en"))
} ORDER BY DESC(?populationTotal)
I've managed to get dbpedia to return all the info I need, however I'd like to only keep the columns above in bulletpoints and not the country column. I know there should be a way to return that country directly with the label however I'm having troubles getting any closer to a solution...
I am currently trying to run my query but I keep getting the error that in line 0 the parentheses are not balanced at '}'
I have checked my whole code multiple times, but I don't seem to get it fixed. I am currently using the dbpedia endpoint.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX yago: <http://dbpedia.org/class/yago/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?country ?government ?population
WHERE{ ?country dct:subject <http://dbpedia.org/resource>/Category:Countries_in_Europe> ;
rdfs:label ?country;
dbo:government ?government.
?government rdfs:label ?government.
?population rdfs:subClassOf* dbo:PopulatedPlace
rdf:type dbpedia-owl:Country;
rdfs:label ?country ;
prop:populationEstimate ?population .
FILTER (?population < 3000000) .
FILTER ( lang(?country) AND (lang(?(government = 'en')
}
Three rows in the graph should be shown, First with the country as a title, second with the governmenttypes of the countries as a title and the 3rd should be a row with the population descending from the total of 3000000.
Thanks alot in advance for helping me out!
You have multiple errors in this query.
Several things that pop out at me.
Thing 1 --
?government rdfs:label ?government.
You've got several similar ?subject ?predicate ?subject constructions.
Thing 2 --
?population rdfs:subClassOf* dbo:PopulatedPlace
rdf:type dbpedia-owl:Country;
I think you need a semicolon after dbo:PopulatedPlace
Thing 3 --
FILTER ( lang(?country) AND (lang(?(government = 'en')
That FILTER breaks syntax several ways. I think this will do what you intend --
FILTER ( lang(?country) = 'en') .
FILTER ( lang(?government) = 'en') .
Thing 4 --
<http://dbpedia.org/resource>/Category:Countries_in_Europe>
You've got an extra > in mid-string.
Thing 5 --
dbpedia-owl:Country
I think that should be dbo:Country
Thing 6 --
prop:populationEstimate
I think that should be dbp:populationEstimate
There are MANY more issues... I am not sure you're really trying.
This is my query below, querying the country names with a certain minimum population, executing on http://dbpedia.org/sparql.
even though i change the population variable to a tiny amount. there are no rows being returned. why?
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?country_name ?population
WHERE {
?country a type:LandlockedCountries ;
rdfs:label ?country_name ;
prop:populationEstimate ?population .
FILTER (?population > 15000000 && langMatches(lang(?country_name), "en")) .
} ORDER BY DESC(?population)
Because there is no class http://dbpedia.org/class/yago/LandlockedCountries in DBpedia - I don't know why you think that there is such a class?
There is a Wikipedia category Landlocked_countries, thus, the URI would be http://dbpedia.org/resource/Category:Landlocked_countries and the property that relates resources to a category is http://purl.org/dc/terms/subject:
PREFIX prop: <http://dbpedia.org/property/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dct: <http://purl.org/dc/terms/>
SELECT *
WHERE {
?country dct:subject dbc:Landlocked_countries ;
rdfs:label ?country_name ;
prop:populationEstimate ?population .
FILTER (?population > 15000000 && langMatches(lang(?country_name), "en")) .
} ORDER BY DESC(?population)
In general, "debugging" a SPARQL query can be done by starting with just a single triple pattern and checking if this returns the expected resp. any result.
Note: This question is different from SPARQL query to retrieve countries population from DBpedia. This question is about population density as understood by DBPedia itself.
How can I retrieve country population density from DBPedia?
I have tried the following, but Virtuoso endpoint returns an empty result set:
PREFIX p: <http://dbpedia.org/property/>
SELECT DISTINCT ?name ?populationDensity
WHERE {
?country a dbpedia-owl:Country .
?country rdfs:label ?name .
?country p:populationDensity ?populationDensity . }
Your current query returns an empty table because there is no ?country that fulfills your query that has the rdf:type dbpedia-owl:Country (represented by 'a'). Check that with this query.
To find the list of rdf:type's that the set of data that does use your populationDensity you could use this query. Following that lead you can just check all properties for Portugal and find that it does have populationDensity, but not the one you used.
This works:
PREFIX dbpedia-ont-PP: <http://dbpedia.org/ontology/PopulatedPlace/>
SELECT DISTINCT ?country ?populationDensity
WHERE {
?country a dbpedia-owl:Country .
?country dbpedia-ont-PP:populationDensity ?populationDensity .
}