Sparql queries Sparql queries - sparql

I need to query dbpedia using sparql:
input: name of a person
output:
the surname, sex , age, occupation
input: an organization name
output:
the founder, if it is public or private
input: a date
output:
if it is an important date for Example: christians, or Easter
input: a city name
output:
the state, region of the city.

Here is an example for #4
First you need to look at the Ontology Class of City in DbPedia.
city is described as city
Select distinct ?city ?location
Where
{
?city a dbo:City.
?city dbo:location ?location.
}
LIMIT 100
Then you can the filter clause to get the city you want.

i think i need to know the resource first but for example
for the 1. i can do ...
SELECT *
where
{
<http://dbpedia.org/resource/Name_x> dc:description ?p.
<http://dbpedia.org/resource/Name_x> foaf:surname ?y
<http://dbpedia.org/resource/Name_x> foaf:name ?y
<http://dbpedia.org/resource/Name_x> foaf:office ?y
}
im checking the others

Related

Count column values in SPARQL

I am writing a query in SPARQL for a knowledge graph.
I have a column having Country and names of countries repeating.
For example: Japan, India, US, India, India, US, US, Romania etc. and so on.
I need to write a SPARQL query that gives me the count of each country in that column.
Example: Japan 1, India 3, US 3 etc. and so on.
SELECT ?countryName COUNT(?CountryName)
WHERE
{
...
}
GROUP BY ?countryName
HAVING (?countryName = "Germany")
Try something like this:
SELECT ?countryName COUNT(*) AS ?count
WHERE
{ ?iri a OBJECT .
?iri rdfs:label ?countryName .
}
GROUP BY ?countryName
Keep in mind that you will need to replace OBJECT with the entity type, and I have assumed you are using the rdfs:label predicate for the labels.

Wikidata Query misses results

I am trying to query all the countries from Wikidata (query link):
SELECT ?item WHERE { ?item wdt:P31 wd:Q6256. }
Unfortunately, the results are missing for example Switzerland (Q39):
https://www.wikidata.org/wiki/Q39
Looking at the Switzerland data, it has the triple: instance of (P31) country (Q6256).
Could you help me understand why Q39 is not present in the results then?
Thanks!
In WikiData, often you have what are called statements. These allow for qualifications to those statements.
For instance, in the case of Switzerland being a country, the qualification is that the rank of this statement should be 'normal' instead of preferred.
The preferred way to refer to Switzerland is sovereign state (wd:Q3624078), and it looks like WikiData will only have a wdt:P31 relationship between an entity and its 'preferred rank' classification only, as this query shows.
I think this could be because 'country' is a more generic concept, e.g. Wales is a country but not a sovereign state.
Fear not however, as this query:
SELECT DISTINCT ?item
WHERE {
?item p:P31/ps:P31 wd:Q6256.
}
returns wd:Q39 as well. What this query is doing is to navigate from Switzerland to Country via the statement.
That is, we have in our data:
wd:Q39 p:P31 wds:Q39-fbe1ac75-4a8a-93c4-6009-81055d79f9cb .
wds:Q39-fbe1ac75-4a8a-93c4-6009-81055d79f9cb ps:P31 wd:Q6256 .
but not:
wd:Q39 wdt:P31 wd:Q6256 .
Try this:
SELECT DISTINCT *
WHERE {
?item p:P31 ?y .
?y ps:P31 wd:Q6256 ;
?p ?o .
VALUES ?item {wd:Q39}
}
to see for yourself.

Wikidata SPARQL query is missing part of expected results

On Wikidatas SPARQL endpoint i tried to find all cities worldwide with a population greater than 100000.
I'm getting lots of correct results with my query. But when i checked for some particular cities they didn't show up in the list.
My query:
SELECT DISTINCT ?cityLabel ?population ?coord ?countryLabel ?shortCountry ?city WHERE {
?city (wdt:P31/(wdt:P279*)) wd:Q515;
wdt:P1082 ?population;
wdt:P625 ?coord.
FILTER(?population > 100000 )
?city wdt:P17 ?country.
?country wdt:P298 ?shortCountry.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ASC (?shortCountry)
I looked for the following cities in the results:
Berlin
Beirut
Lyon
But they didn't show up.
Try it out here
Ok i figured something out for myself:
I dug a little deeper into the wikidata object of the cities i mentioned and found out, that they are no instance of "city" (Q515).
Berlin belongs to e.g. "capital", "city with millions of inhabitants", "metropolis"
Lyon belongs to "commune of france" and "big city"
Beirut belongs to e.g. "capital" and "big city"
But none of them is listed as a "city". That's for sure not intentionally because other bigger cities are also an instance of "city" e.g. Paris
So in conclusion:
Check all the categories your filtering for and check your expected results to match them.

How to extract persons on a wikipedia list using dbpedia/sparql

I'm trying to extract all persons that won a (Gold) medal at the Olympics and ideally their birth location using the dbpedia SPARQL query. Basically it's this list I'm aiming at: https://de.wikipedia.org/wiki/Liste_der_olympischen_Medaillengewinner_aus_Spanien
I guess it must somehow work with this piece of code:
yago-res:wikicategory_Olympic_bronze_medalists_for_Spain
This doesn't work:
SELECT ?res
WHERE {
?res yago-res:wikicategory_Olympic_bronze_medalists_for_Spain .
}
any ideas?
To get all the spanish persons that have won the gold medal in olympic
select ?person where
{
?person a <http://dbpedia.org/class/yago/OlympicGoldMedalistsForSpain>
}
If you look at what dbpedia has, there is no class:
http://dbpedia.org/class/yago/OlympicGoldMedalists
but there is
http://dbpedia.org/class/yago/OlympicGoldMedalistsForItaly
and
http://dbpedia.org/class/yago/OlympicGoldMedalistsForFrance
and
http://dbpedia.org/class/yago/OlympicGoldMedalistsForGermany
so a work around could be:
select distinct ?person ?birthPlace where
{
?goldForCountry rdfs:subClassOf yago:Medalist110305062 .
?person a ?goldForCountry .
optional{
?person dbo:birthPlace ?birthPlace
}
filter (contains(str(?goldForCountry), "http://dbpedia.org/class/yago/OlympicGoldMedalistsFor"))
}
The birthPlace should be optional because there are 3994 persons that dbpedia doesn't have their birth place

Query DBpedia to get abstract for different inputs

I have a question I need to build a single query to DBpedia such that, If I give any one of these as input like a City name or a person name or a Institute name or a Instrument name can I get its abstract as a output???
For instance,
New York- New York is a state in the Northeastern and Mid-Atlantic regions of the United States......
Mars- Mars is the fourth planet from the Sun and the second smallest planet in the Solar System....
Michael Jackson- Michael Joseph Jackson was an American singer, songwriter, dancer, and actor......
I have tried but its not working for all.
SELECT ?abstract WHERE {
<http://dbpedia.org/resource/New_York>
<http://dbpedia.org/ontology/abstract>
?abstract
FILTER langMatches(lang(?abstract), "en")
}
If you intend to get the abstract for multiple things, supply those multiple things within a VALUES block. I found that matching by ?name worked sufficiently well for name-based searches.
SELECT DISTINCT ?abstract WHERE {
[ rdfs:label ?name
; dbpedia-owl:abstract ?abstract
] .
FILTER langMatches(lang(?abstract),"en")
VALUES ?name { "New York"#en }
}
LIMIT 10