Wikidata queries

Let’s play with Wikidata (query.wikidata.org)

Return 20 items which are instance of human which have a twitter account:
SELECT ?item ?twitter
WHERE {
?item wdt:P31 wd:Q5 .
?item wdt:P2002 ?twitter .
} LIMIT 20

Screen Shot 2016-06-12 at 11.22.35

Or all Nobel Peace laureates:


SELECT DISTINCT ?item ?itemLabel
WHERE {
?item p:P166 ?awardStat . # ... with an awarded(P166) statement
?awardStat ps:P166 wd:Q35637 . # ... that has the value Nobel Peace Prize (Q35637)
}

Screen Shot 2016-06-12 at 10.55.56

Now we'd like to add the labels:


SELECT DISTINCT ?item ?itemLabel
WHERE {
?item p:P166 ?awardStat . # ... with an awarded(P166) statement
?awardStat ps:P166 wd:Q35637 . # ... that has the value Nobel Peace Prize (Q35637)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}

Screen Shot 2016-06-12 at 11.00.26

Return all Nobel Peace laureates and Nobel laureates in economics and include an image of them:

SELECT DISTINCT ?item ?itemLabel ?pic
WHERE {
?item p:P166 ?awardStat . # … with an awarded(P166) statement
{?awardStat ps:P166 wd:Q35637 .} # … that has the value Nobel Peace Prize (Q35637)
UNION
{?awardStat ps:P166 wd:Q47170 .} # … or Nobel Prize in economics
SERVICE wikibase:label { bd:serviceParam wikibase:language “en” . }
OPTIONAL { ?item wdt:P18 ?pic }
}
Screen Shot 2016-06-12 at 11.06.16

All Nobel laureates including the Wikipedia link and their twitter account:


SELECT DISTINCT ?item ?itemLabel ?pic ?article ?twitter
WHERE {

?item p:P166 ?awardStat . # … with an awarded(P166) statement
?awardStat ps:P166 ?aw .
?aw wdt:P31 wd:Q7191 .
SERVICE wikibase:label { # … include the labels
bd:serviceParam wikibase:language “en” .
}
OPTIONAL { ?item wdt:P2002 ?twitter . }
OPTIONAL { ?item wdt:P18 ?pic }
OPTIONAL {
?article schema:about ?item .
?article schema:inLanguage “en” .
?article schema:isPartOf .
}
}

Screen Shot 2016-06-12 at 11.11.28

There are many more examples on query.wikidata.org and in the user manual.



Categories: Ruby on Rails & other tech

Tags: , ,