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 .
}
Related
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 need to find the names of similar types from DBpedia so I'm trying to figure out a query which can return me the names of entities which have same subject type in its dct:subject (example I want to find similar types of white house so i want to write a query for same . I'm considering the dct:subject to find them ). If there is any other approach please mention it
Previously I tried it for rdf:type but the result are not so good and some time it shows time out
I have done my problem by the query mentioned below and now i want to consider dct:subject instead of rdf:type
select distinct ?label ?resource count(distinct ?type) as ?score where {
values ?type { dbo:Thing dbo:Organization yago:WikicatIslam-relatedControversies yago:WikicatIslamistGroups yago:WikicatRussianFederalSecurityServiceDesignatedTerroristOrganizations yago:Abstraction100002137 yago:Act100030358 yago:Cabal108241798 yago:Group100031264 yago:Movement108464601 yago:PoliticalMovement108472335
}
?resource rdfs:label ?label ;
foaf:name ?name ;
a ?type .
FILTER (lang(?label) = 'en').
}
ORDER BY DESC(?score)
I tried to find the all the country from dbpedia which have more than one official language. i tried the following sparql query but did not work.
SELECT distinct ?country ?officialLanguage
WHERE {
?country rdf:type dbo:Country .
?country dbo:officialLanguage ?officialLanguage.
FILTER (COUNT(?officialLanguage) >1)
}
and got the following error-
Virtuoso 37000 Error SP030: SPARQL compiler, line 8: Aggregates are allowed only in result sets at ')' before '>'
I am very new to sparql. I think I am missing something.
As an alternative query to the answer of #svick you could try
SELECT ?country (COUNT(?officialLanguage) AS ?nrOfLanguages)
WHERE {
?country rdf:type dbo:Country .
?country dbo:officialLanguage ?officialLanguage.
}
GROUP BY ?country
HAVING(COUNT(?officialLanguage) > 1)
SPARQL doesn't work like that, it can't deduce that you mean the count of distinct ?officialLanguage for each ?country. You will need to be more explicit than that, for example:
SELECT distinct ?country ?officialLanguage
WHERE {
?country rdf:type dbo:Country .
?country dbo:officialLanguage ?officialLanguage.
{
SELECT ?country COUNT(*) AS ?languages
WHERE {
?country dbo:officialLanguage [].
}
}
FILTER (?languages > 1)
}
I have been trying to do this query but im not sure about how is the syntax I need. I have seen a lot of examples of how to ask in a query to check if something is larger or shorter than other thing, but I have no idea of how to ask if something is IN other thing.
This is my last (failed) try:
PREFIX : <http:/dbpedia.org/resource/>
ASK
{
FILTER(<http:/dbpedia.org/resource/Paris> IN <http:/dbpedia.org/resource/Member_states_of_the_United_Nations>)
}
Part of the issue is that there's a DBpedia resource called
http:/dbpedia.org/resource/Member_states_of_the_United_Nations
but what you actually want, since its the value of the dct:subject property, is the category,
http://dbpedia.org/page/Category:Member_states_of_the_United_Nations
For instance, you can get a list of countries that are member states with a query like:
select ?country {
?country a dbo:Country ;
dct:subject dbc:Member_states_of_the_United_Nations
}
SPARQL results
However, Paris isn't a country that's a member. It's a city in France, which is a member. You can check whether something is a member with an ask query like:
ask {
dbr:France a dbo:Country ;
dct:subject dbc:Member_states_of_the_United_Nations
}
SPARQL results (true)
You could get a list of populated places in countries that are member with a query like:
select ?city ?country {
?city a dbo:PopulatedPlace ;
dbo:country ?country .
?country a dbo:Country ;
dct:subject dbc:Member_states_of_the_United_Nations .
}
SPARQL results (limited to 100)
and you can modify that to be an ask query that checks for cities with specific names. E.g.:
ask {
?city rdfs:label "Paris"#en ;
a dbo:PopulatedPlace ;
dbo:country ?country .
?country a dbo:Country ;
dct:subject dbc:Member_states_of_the_United_Nations .
}
SPARQL results (true)
Using the dc:hasPart term :
<http:/dbpedia.org/resource/Member_states_of_the_United_Nations> <http://dublincore.org/documents/dcmi-terms/#terms-hasPart> <http:/dbpedia.org/resource/Paris>
This is described in this w3 document.
I am trying to select all people born in a specific country (e.g. Portugal) from DBPedia.
I could use this query:
SELECT DISTINCT ?person
WHERE {
?person dbpedia-owl:birthPlace dbpedia:Portugal.
}
But the problem is that not all people have dbpedia:Portugal as birthPlace. About 30% of people have just a town name as birthPlace, e.g.
dbpedia:Lisbon
I could add all Portugal cities in a FILTER clause but it's a big list.
May be it's possible to infer Portugal from Lisbon in the SPARQL query somehow?
(to not to add all Portugal cities in FILTER to get ALL persons)
If we assume all the cities in a specific country are defined as part of that country in dbpedia, you could have a query that first looks for the people that have dbpedia:Portugal as a country and then cities within dbpedia:Portugal.
SELECT DISTINCT ?person
WHERE {
?person a dbpedia-owl:Person.
Optional{
?person dbpedia-owl:birthPlace ?country.
}
Optional{
?person dbpedia-owl:birthPlace ?place.
?place dbpedia-owl:country ?country
}
filter(?country= dbpedia:Portugal)
}
The query that you have written identifies 1723 distinct URIs, and this finds 2563 URIs.
Artemis' answer works, but it's very verbose for what's a pretty simple query. It can be simplified to:
select distinct ?person where {
?person a dbpedia-owl:Person ;
dbpedia-owl:birthPlace/dbpedia-owl:country? dbpedia:Portugal
}
SPARQL results (2449)
Full results may be achieved by this http://answers.semanticweb.com/questions/22450/sparql-selecting-people-by-country
- 2730 persons
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT ?person
WHERE
{
{?person a <http://dbpedia.org/ontology/Person>;
<http://dbpedia.org/ontology/birthPlace> ?place.
?place <http://dbpedia.org/ontology/country> ?birthCountry.
?birthCountry a <http://dbpedia.org/ontology/Country>.
FILTER (?birthCountry = dbpedia:Portugal).
}
UNION
{ ?person a <http://dbpedia.org/ontology/Person>;
<http://dbpedia.org/ontology/birthPlace> ?birthCountry.
?birthCountry a <http://dbpedia.org/ontology/Country>.
FILTER (?birthCountry = dbpedia:Portugal).
}
}
GROUP BY ?person
ORDER BY ?person