0

I was learning the wikidata query language, and wanted to find the US president's name along with grand father, great grand father and so on...

I tried this to get a father, but how to find the father's father and so on...

SELECT  ?valLabel ?resLabel
WHERE {
  ?val wdt:P31 wd:Q5.
  ?val  wdt:P27 wd:Q30.
  ?val wdt:P106 wd:Q82955.
  ?val wdt:P22 wd:Q11806.
  OPTIONAL { ?val wdt:P22 ?res. }

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Abhishek Sengupta
  • 2,938
  • 1
  • 28
  • 35

3 Answers3

1

Your query is looking for humans who are US citizens who are politicians and whose father is John Adams.

Instead, you want a query like this (in pseudo-SPARQL):

SELECT ?presidentLabel ?fatherLabel ?gFatherLabel ?ggFatherLabel ...
WHERE {
?president position_held president_of_the_US .
OPTIONAL{?president has_father ?father .}
OPTIONAL{?father has_father ?gFather .}
OPTIONAL{?gFather has_father ?ggFather .} ...

SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  }

Notice that the optional is necessary, as there may be no information up to the great-grandfather of a president, in which case no information about the president would be returned at all.

In Wikidata, the following should work:

SELECT
  ?presidentLabel
  ?fatherLabel
  ?gFatherLabel
  ?ggFatherLabel
WHERE {
  ?president wdt:P39 wd:Q11696.

  OPTIONAL{?president wdt:P22 ?father}
  OPTIONAL{?father wdt:P22 ?gFather}
  OPTIONAL{?gFather wdt:P22 ?ggFather}

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

However the query seems to time out when you look beyond the president's grandfather.

Bonus: If you want the presidents in chronological order, use this query:

 SELECT  ?ord ?presidentLabel ?fatherLabel ?gFatherLabel
 WHERE {
    ?president wdt:P39 wd:Q11696 ;
               p:P39 ?presidency .
    ?presidency ps:P39 wd:Q11696 ;
                pq:P1545 ?ordString .

  OPTIONAL{?president wdt:P22 ?father .}
  OPTIONAL{?father wdt:P22 ?gFather .}

BIND(xsd:integer(?ordString) AS ?ord)      

      
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
      }
ORDER BY ?ord
Valerio Cocchi
  • 1,891
  • 1
  • 6
  • 18
  • How to find the n-th level till it finds no information? –  Jun 12 '20 at 15:36
  • You will need to use a property path, like this: ?president wdt:P22+ ?ancestor . This will be slow however, and you need to look at other posts on how to find out what level the ancestor is at. – Valerio Cocchi Jun 12 '20 at 15:45
  • See here for inspiration: https://stackoverflow.com/questions/31614043/find-superclasses-and-sort-by-distance-in-sparql/31657312#31657312 – Valerio Cocchi Jun 12 '20 at 15:46
0

Try this query

#Father - GrandFather and so on ...
SELECT  ?childLabel ?fatherLabel ?grandFatherLabel ?greatGrandFather1Label ?greatGrandFather2Label
WHERE {
  ?child wdt:P31 wd:Q5.
  ?child  wdt:P27 wd:Q30.
  ?child wdt:P106 wd:Q82955.
  ?child wdt:P22 wd:Q11806.
  OPTIONAL { ?child wdt:P22 ?father. }
  OPTIONAL { ?father wdt:P22 ?grandFather. }
  OPTIONAL { ?grandFather wdt:P22 ?greatGrandFather1. }
  OPTIONAL { ?greatGrandFather1 wdt:P22 ?greatGrandFather2. }

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Abhishek Sengupta
  • 2,938
  • 1
  • 28
  • 35
  • How to find the n-th level till it finds no information? –  Jun 12 '20 at 15:36
  • 1
    I don't think we will be able to do that ,as the query will become expensive after some point and it will timeout. What do you want to do with this query ? – Abhishek Sengupta Jun 12 '20 at 15:41
0

Here is a query with all fathers (and their fathers, as far back as there is data in Wikidata) of all US presidents:

SELECT ?presidentLabel ?fatherLabel 
WHERE 
{
  ?president wdt:P39 wd:Q11696;
             wdt:P31 wd:Q5;
             wdt:P22+ ?father .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Try it!

Ainali
  • 1,613
  • 13
  • 23
  • Thanks for the query, learned something new from this, but I wanted to know nth level of father , grandfather and so on for each presidents in a single row. –  Jun 13 '20 at 15:27
  • 1
    As far as I know, that is not possible to do in SPARQL. To get a new column, you need to name the variable, which means that one has to make a decision on the n before querying. (But I must confess this is on the edge of my knowledge and would be happy to be proven wrong.) – Ainali Jun 13 '20 at 16:39
  • Cool ok, even I am new to this , any way thanks for sharing that query, much helpful :) –  Jun 13 '20 at 20:17