getting the list of countries which have more than one official language - sparql

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)
}

Related

Why this wikidata SPARQL query is missing country information?

This SPARQL query on Wikidata is missing the form of government for a lot of entries. My query:
SELECT DISTINCT ?country ?countryLabel
(group_concat(DISTINCT ?bfogLabel;separator=", ") as ?Government)
WHERE
{
?country wdt:P31 wd:Q3624078.
OPTIONAL {?country wdt:P122 ?bfog } . # basic form of government
SERVICE wikibase:label
{ bd:serviceParam wikibase:language "en" .
?country rdfs:label ?countryLabel .
?bfog rdfs:label ?bfogLabel .
}
}
GROUP BY ?country ?countryLabel
ORDER BY ?countryLabel
Angola is in Wikipedia's infobox: "Unitary dominant-party presidential constitutional republic". But it is empty in this query.
Why is that? More important: is there any fix for this? I saw in this question that wikidata is not as reliable as possible when it comes to data categorization.
Try it out here

How to get information like capital, currency, language, population about a country in simple way using SPARQL from DBPEDIA

How to get information like capital, currency, language, population about a country in simple way using SPARQL from DBPEDIA.
Is there a simple way to do it?
I found a solution for this problem,
I figured out a query that returns all the required fields,
SELECT DISTINCT ?country ?population ?capital ?currency WHERE {
{?country rdf:type
<http://dbpedia.org/class/yago/WikicatMemberStatesOfTheUnitedNations> .
?country <http://dbpedia.org/ontology/populationTotal> ?population .
?country <http://dbpedia.org/ontology/capital> ?capital .
?country <http://dbpedia.org/ontology/currency> ?currency .}
UNION
{?country rdf:type <http://dbpedia.org/ontology/Country> .
?country <http://dbpedia.org/ontology/populationTotal> ?population .
?country <http://dbpedia.org/ontology/capital> ?capital .
?country <http://dbpedia.org/ontology/currency> ?currency .}
}
SPARQL Result

executing sparql query on all the result of another sparql query

Suppose I have a list of names returned from a sparql query. And another query that uses a name from this list and returns something else. How can I execute this query on all names in the list returned by the other query? Thank you
You use a subquery. E.g.,
select ?country ?capital {
{ select ?country {
#-- criteria for selecting country
}
?country :hasCapital ?country
}
Note that in trivial cases, you wouldn't need a separate subquery, you could just do:
select ?country ?capital {
#-- criteria for selecting country
?country :hasCapital ?country
}

How to ASK if there is any country in the United Nations called “Paris” using SPARQL

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.

SPARQL query to retrieve countries population density from DBPedia

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 .
}