In this example, we search Wikidata for any items that are an “instance of” (P31) “film” (Q11424) that has the label “The Cabin in the Woods” (Q45394), and ask for the item’s genres (P136).
query_wikidata('SELECT DISTINCT
?genre ?genreLabel
WHERE {
?film wdt:P31 wd:Q11424.
?film rdfs:label "The Cabin in the Woods"@en.
?film wdt:P136 ?genre.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}')Which returns a tibble:
# A tibble: 6 x 2
genre genreLabel
<chr> <chr>
1 http://www.wikidata.org/entity/Q3072049 zombie film
2 http://www.wikidata.org/entity/Q471839 science fiction film
3 http://www.wikidata.org/entity/Q859369 comedy-drama
4 http://www.wikidata.org/entity/Q1342372 monster film
5 http://www.wikidata.org/entity/Q853630 slasher film
6 http://www.wikidata.org/entity/Q224700 comedy horror
For more example SPARQL queries, see this page on Wikidata.
Links for learning SPARQL
- A beginner-friendly course for SPARQL
- Building a SPARQL query: Museums on Instagram
- SPARQL Query Examples for WDQS
- Using SPARQL to access Linked Open Data by Matthew Lincoln
- Interesting or illustrative SPARQL queries for Wikidata
- Wikidata 2016 SPARQL Workshop
- Wikidata SPARQL Query video tutorial by Navino Evans
- Learning SPARQL by Bob DuCharme
- WDQS User Manual