6 ms·
Wikidata's new SPARQL service is probably the most useful topic in this tutorial for software developers and anyone interested in the Semantic Web. It allows o
by emw 11y ago
Wikidata's new SPARQL service is probably the most useful topic in this tutorial for software developers and anyone interested in the Semantic Web. It allows one to query the vast, free knowledgebase that backs Wikipedia -- almost 15 million entities and over 70 million statements.
Example queries:
* Politicians who died of cancer (of any type): https://query.wikidata.org/#PREFIX%20wikibase%3A%20%3Chttp%3A%2F%2Fwikiba.se%2Fontology%23%3E%0APREFIX%20wd%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E%20%0APREFIX%20wdt%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E%0APREFIX%20rdfs%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0A%0ASELECT%20%3Fpolitician%20%3Fcause%20%3Fpolitician_label%20%3Fcause_of_death_label%20WHERE%20{%0A%20%20%20%3Fpolitician%20wdt%3AP106%20wd%3AQ82955%20.%20%20%20%20%23%20find%20items%20that%20have%20%22occupation%20%28P106%29%3A%20politician%20%28Q82955%29%22%0A%20%20%20%3Fpolitician%20wdt%3AP509%20%3Fcause%20.%20%20%20%20%20%20%20%23%20with%20a%20P509%20%28cause%20of%20death%29%20claim%0A%20%20%20%3Fcause%20wdt%3AP279*%20wd%3AQ12078%20.%20%20%20%20%20%20%20%20%23%20...%20where%20the%20cause%20is%20a%20subclass%20of%20%28P279*%29%20cancer%20%28Q12078%29%0A%20%20%20%23%20%3Fpolitician%20wdt%3AP39%20wd%3AQ11696%20.%20%20%20%23%20Uncomment%20this%20line%20to%20include%20only%20U.S.%20Presidents%0A%20%20%0A%20%20%20OPTIONAL%20{%3Fpolitician%20rdfs%3Alabel%20%3Fpolitician_label%20filter%20%28lang%28%3Fpolitician_label%29%20%3D%20%22en%22%29%20.}%20%0A%20%20%20OPTIONAL%20{%3Fcause%20rdfs%3Alabel%20%3Fcause_of_death_label%20filter%20%28lang%28%3Fcause_of_death_label%29%20%3D%20%22en%22%29.}%0A}%0AORDER%20BY%20ASC%20%28%3Fpolitician%29 https://query.wikidata.org/#PREFIX%20wikibase%3A%20%3Chttp%3...
* Who discovered the most planets? https://query.wikidata.org/#PREFIX%20wikibase%3A%20%3Chttp%3A%2F%2Fwikiba.se%2Fontology%23%3E%0APREFIX%20wd%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E%20%0APREFIX%20wdt%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E%0APREFIX%20rdfs%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0A%0ASELECT%20%3Fdiscoverer%20%3Fname%20%28COUNT%28DISTINCT%20%3Fplanet%29%20as%20%3Fcount%29%0A%20WHERE%20{%0A%20%20%20%3Fppart%20wdt%3AP279*%20wd%3AQ634%20.%0A%20%20%20%3Fplanet%20wdt%3AP31%20%3Fppart%20.%0A%20%20%20%3Fplanet%20wdt%3AP61%20%3Fdiscoverer%20.%0A%20%20SERVICE%20wikibase%3Alabel%20{%0A%20%20%20%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20.%0A%20%20%20%20%3Fdiscoverer%20rdfs%3Alabel%20%3Fname%0A%20%20%20}%0A%20}%0A%20GROUP%20BY%20%3Fdiscoverer%20%3Fname%0A%20ORDER%20BY%20DESC%28%3Fcount%29%0A%20LIMIT%2010 https://query.wikidata.org/#PREFIX%20wikibase%3A%20%3Chttp%3...
* Largest cities with a female mayor: http://query.wikidata.org/#PREFIX%20wikibase%3A%20%3Chttp%3A%2F%2Fwikiba.se%2Fontology%23%3E%0APREFIX%20wd%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E%20%0APREFIX%20wdt%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E%0APREFIX%20rdfs%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0APREFIX%20p%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2F%3E%0APREFIX%20q%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fqualifier%2F%3E%0APREFIX%20v%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fstatement%2F%3E%0A%0ASELECT%20DISTINCT%20%3Fcity%20%3FcityLabel%20%3Fmayor%20%3FmayorLabel%20WHERE%20%7B%0A%20%20%3Fcity%20wdt%3AP31%2Fwdt%3AP279%2A%20wd%3AQ515%20.%20%20%23%20find%20instances%20of%20subclasses%20of%20city%0A%20%20%3Fcity%20p%3AP6%20%3Fstatement%20.%20%20%20%20%20%20%20%20%20%20%20%20%23%20with%20a%20P6%20%28head%20of%20goverment%29%20statement%0A%20%20%3Fstatement%20v%3AP6%20%3Fmayor%20.%20%20%20%20%20%20%20%20%20%20%20%23%20...%20that%20has%20the%20value%20%3Fmayor%0A%20%20%3Fmayor%20wdt%3AP21%20wd%3AQ6581072%20.%20%20%20%20%20%20%20%23%20...%20where%20the%20%3Fmayor%20has%20P21%20%28sex%20or%20gender%29%20female%0A%20%20FILTER%20NOT%20EXISTS%20%7B%20%3Fstatement%20q%3AP582%20%3Fx%20%7D%20%20%23%20...%20but%20the%20statement%20has%20no%20P582%20%28end%20date%29%20qualifier%0A%20%0A%20%20%23%20Now%20select%20the%20population%20value%20of%20the%20%3Fcity%0A%20%20%23%20%28wdt%3A%20properties%20use%20only%20statements%20of%20%22preferred%22%20rank%20if%20any%2C%20usually%20meaning%20%22current%20population%22%29%0A%20%20%3Fcity%20wdt%3AP1082%20%3Fpopulation%20.%0A%20%20%23%20Optionally%2C%20find%20English%20labels%20for%20city%20and%20mayor%3A%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%0A%20%20%20%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20.%0A%20%20%7D%0A%20%7D%20ORDER%20BY%20DESC%28%3Fpopulation%29%20LIMIT%2010 http://query.wikidata.org/#PREFIX%20wikibase%3A%20%3Chttp%3A...
More Wikidata SPARQL query examples: https://www.mediawiki.org/wiki/Wikibase/Indexing/SPARQL_Query_Examples https://www.mediawiki.org/wiki/Wikibase/Indexing/SPARQL_Quer....