Returning only label columns & population from dbpedia query - sparql

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

Related

Sparql in combination with DBpedia

i want to display the countries with their net value/income or something similiar through accessing the data through DBPedia. and further insert them into an already created table.
Unfortunately i dont get any results with my code.
Try the following query to list the type of entities that has a dbp:income as a property. you will notice that dbo:Country is not the list, that's why the result you got was empty.
prefix dbp: <http://dbpedia.org/property/>
select distinct ?type
where {
?s a ?type.
?s dbp:income ?income
}
My suggestion is to use dbp:gdpNominal instead of the dbp:income property in your query :
prefix dbp: <http://dbpedia.org/property/>
prefix dbo: <http://dbpedia.org/ontology/>
select distinct ?country_name ?income
where {
?country a dbo:Country.
?country rdfs:label ?country_name. FILTER (lang(?country_name) = 'en')
?country dbp:gdpNominal ?income.
}

Find all artists who were born in a given city

The following snippet retrieves all artists (names) from around the world using the dbpedia.org database.
prefix dbo: <http://dbpedia.org/ontology/>
select distinct (str(?name_) as ?name) {
?artist a dbo:Artist ;
rdfs:label ?name_ .
}
What I would like to do next, is extend this snippet so that a) I filter by a certain city and b) present the following optional fields:
name (this works already)
city name
birth date
sub class (e.g. Actor, MusicalArtist etc.)
dbo:birthDate will give you the birth date of the artist.
dbo:birthPlace will give you the birth place of the artist.
rdf:type / dbc:subject will give you all the roles that the artist have. Be careful, this one can be tremendous. Perhaps should you refine your research (Yago, dbc, dbo, wikidata, ...).
Here is an example of artists born in Pinner and their birthDate :
prefix dbo: <http://dbpedia.org/ontology/>
select distinct (str(?name_) as ?name) ?birthPlace ?birthDate {
?artist a dbo:Artist ;
rdfs:label ?name_ .
?artist dbo:birthPlace ?birthPlace .
?artist dbo:birthDate ?birthDate .
filter(?birthPlace = dbr:Pinner)
}

Why is this sparql query not returning any rows on dbpedia?

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.

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

SPARQL query does not give all the results that are on dbpedia

I'm executing this query in http://dbpedia.org/snorql/:
Query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
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 .
}
The query looks for all land looked countries.
I don't understand why in the results there are not some countries which are classified on
"/dbpedia.org/class/yago/LandlockedCountries". For example, Paraguay (/dbpedia.org/page/ParaguAy) is classified but does not appear in the query result set. Can somebody explain me why?
Unfortunately, there are a small number of landlocked countries that do not have values for at least one of the country_name and populationEstimate properties. This is why they will not be returned in your query. If you run the following query, those countries will come up (those two attributes are set as OPTIONAL).
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?country ?country_name ?population
WHERE {
?country a type:LandlockedCountries .
OPTIONAL {?country rdfs:label ?country_name Filter(lang(?country_name) = 'en')} .
OPTIONAL {?country prop:populationEstimate ?population} .
}
run query
For (slightly) better results, since some countries seem to be duplicated with erroneous capitalization (e.g. ParaguAy and Paraguay), the following query uses ?country dcterms:subject category:Landlocked_countries instead of the yago class.
run query