jenkins-bot has submitted this change and it was merged. Change subject: Document various interesting SPARQL query examples ......................................................................
Document various interesting SPARQL query examples Fixes #T96693 Fixes #T96925 This starts a new document for collecting interesting and demonstrative query examples. The final entry is from a thread on Wikidata[1] that shows the top most populous cities in the world that currently have female mayors. 1. https://lists.wikimedia.org/pipermail/wikidata-l/2015-April/005852.html Change-Id: Ie57fcf67ad69bfa6a9636b130e1d429c8d9234db --- A docs/sparql-query-examples.md 1 file changed, 222 insertions(+), 0 deletions(-) Approvals: Smalyshev: Looks good to me, approved jenkins-bot: Verified diff --git a/docs/sparql-query-examples.md b/docs/sparql-query-examples.md new file mode 100644 index 0000000..74bda69 --- /dev/null +++ b/docs/sparql-query-examples.md @@ -0,0 +1,222 @@ +# SPARQL query examples + +## Who were Feynman's employers? + +* Feynman: [Q39246](https://www.wikidata.org/wiki/Q39246) +* employer: [P108](https://www.wikidata.org/wiki/Property:P108) +* Commons category (a.k.a. label): [P373](https://www.wikidata.org/wiki/Property:P373) + +```sparql +PREFIX entity: <http://www.wikidata.org/entity/> +PREFIX v: <http://www.wikidata.org/entity/value/> +PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> + +SELECT ?employer WHERE { + entity:Q39246 entity:P108/v:P108/rdfs:label ?employer . +} LIMIT 10 +``` + +| employer | +| ---------------------------------- | +| Cornell University | +| California Institute of Technology | + +## Who are Feynman's colleagues? + +```sparql +PREFIX entity: <http://www.wikidata.org/entity/> +PREFIX v: <http://www.wikidata.org/entity/value/> +PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> + +SELECT ?employer ?colleague WHERE { + entity:Q39246 entity:P108/v:P108 ?employerS . + ?colleagueS entity:P108/v:P108 ?employerS . + ?employerS rdfs:label ?employer . + ?colleagueS rdfs:label ?colleague . +} LIMIT 10 +``` + +| employer | colleague | +| ---------------------------------- | -------------------- | +| California Institute of Technology | Eric Temple Bell | +| California Institute of Technology | John Gamble Kirkwood | +| California Institute of Technology | Joseph Grinnell | +| California Institute of Technology | Jesse L. Greenstein | +| California Institute of Technology | Charles C. Steidel | +| California Institute of Technology | Robert Bacher | +| California Institute of Technology | Michael Aschbacher | +| California Institute of Technology | Gerald J. Wasserburg | +| California Institute of Technology | H. Richard Crane | +| California Institute of Technology | Steven E. Koonin | + +## What are the fields of Feynman's colleagues? + +* field of work: [P101](https://www.wikidata.org/wiki/Property:P101) + +```sparql +PREFIX entity: <http://www.wikidata.org/entity/> +PREFIX v: <http://www.wikidata.org/entity/value/> +PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> + +SELECT ?colleague ?field WHERE { + entity:Q39246 entity:P108/v:P108 ?employerS . + ?colleagueS entity:P108/v:P108 ?employerS . + ?colleagueS entity:P101/v:P101/rdfs:label ?field . + ?colleagueS rdfs:label ?colleague . +} LIMIT 10 +``` + +| colleague | field | +| ------------------ | ------------------ | +| Carl Sagan | astrobiology | +| Harry Vandiver | number theory | +| James Ax | model theory | +| Mark Kac | probability theory | +| Michael Aschbacher | group theory | +| Eric Temple Bell | combinatorics | +| Sossina M. Haile | Fuel cell | +| Paul Olum | topology | +| Elfriede Abbe | illustration | +| Rick Durrett | probability theory | + +## What are the fields of Feynman's colleagues who are mathematicians? +physicists? + +* occupation: [P106](https://www.wikidata.org/wiki/Property:P106) +* physicist: [Q169470](https://www.wikidata.org/wiki/Q169470) + +```sparql +PREFIX entity: <http://www.wikidata.org/entity/> +PREFIX v: <http://www.wikidata.org/entity/value/> +PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> + +SELECT ?colleague ?field WHERE { + entity:Q39246 entity:P108/v:P108 ?employerS . + ?colleagueS entity:P108/v:P108 ?employerS . + ?colleagueS entity:P106/v:P106 entity:Q170790 . + ?colleagueS entity:P101/v:P101/rdfs:label ?field . + ?colleagueS rdfs:label ?colleague . +} LIMIT 10 +``` +| colleague | field | +| ---------------- | --------------------- | +| Virgil Snyder | algebraic geometry | +| John H. Hubbard | mathematical analysis | +| William Thurston | topology | +| Karen Vogtmann | topology | +| Daina Taimina | topology | +| Eugene Dynkin | algebra | +| Karen Vogtmann | group theory | +| William Feller | probability theory | +| Kiyoshi Itō | probability theory | +| Harry Kesten | probability theory | + +## Whose birthday is it? + +```sparql +PREFIX entity: <http://www.wikidata.org/entity/> +PREFIX v: <http://www.wikidata.org/entity/value/> +PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> +PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> + +SELECT ?entity (year(?date) as ?year) WHERE { + ?entityS entity:P569/v:P569 ?date . + ?entityS rdfs:label ?entity . + + FILTER (datatype(?date) = xsd:dateTime) + FILTER (month(?date) = month(now())) + FILTER (day(?date) = day(now())) +} LIMIT 10 +``` + +| entity | year | +| ---------------------------- | ---- | +| Annie Easley | 1933 | +| James Buchanan | 1791 | +| Daniela Hantuchová | 1983 | +| Radek Pilař | 1931 | +| Hugh Seymour Davies | 1943 | +| Reinhart Koselleck | 1923 | +| Friedrich von Hagedorn | 1708 | +| Dietrich Schwanitz | 1940 | +| Arthur Moeller van den Bruck | 1876 | +| Ruth Leuwerik | 1924 | + +## What happened on this day in history? + +```sparql +PREFIX entity: <http://www.wikidata.org/entity/> +PREFIX v: <http://www.wikidata.org/entity/value/> +PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> +PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> + +SELECT ?entity ?event (year(?date) as ?year) WHERE { + ?entityS ?eventS ?propS . + ?propS ?eventV ?date . + ?entityS rdfs:label ?entity . + ?eventS rdfs:label ?event . + + FILTER (datatype(?date) = xsd:dateTime) + FILTER (month(?date) = month(now())) + FILTER (day(?date) = day(now())) +} LIMIT 10 +``` + +| entity | event | year | +| ---------------------- | ------------- | ---- | +| Israel | head of state | 1963 | +| Annie Easley | date of birth | 1933 | +| James Buchanan | date of birth | 1791 | +| Daniela Hantuchová | date of birth | 1983 | +| Radek Pilař | date of birth | 1931 | +| Tsakhiagiin Elbegdorj | position held | 1998 | +| Albert of Saxony | date of birth | 1828 | +| Hugh Seymour Davies | date of birth | 1943 | +| Reinhart Koselleck | date of birth | 1923 | +| Friedrich von Hagedorn | date of birth | 1708 | + +## What are the top most populous cities in the world with a female head of government? + +```sparql +PREFIX entity: <http://www.wikidata.org/entity/> +PREFIX v: <http://www.wikidata.org/entity/value/> +PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> +PREFIX q: <http://www.wikidata.org/entity/qualifier/> + +SELECT (MAX(?population) AS ?max_population) ?city ?head WHERE { + ?cityS entity:P31/v:P31 entity:Q515 . # find instances of subclasses of city + ?cityS entity:P6 ?x . # with a P6 (head of goverment) statement + ?x v:P6 ?headS . # ... that has the value ?headS + ?headS entity:P21/v:P21 entity:Q6581072 . # ... where the ?headS has P21 (sex or gender) female + FILTER NOT EXISTS { ?x q:P582 ?y } # ... but the statement has no P582 (end date) qualifier + + ?cityS entity:P1082/v:P1082 ?population . + + # Optionally, find English labels for city and head: + OPTIONAL { + ?cityS rdfs:label ?city . + FILTER ( lang(?city) = "en" ) + } + OPTIONAL { + ?headS rdfs:label ?head . + FILTER ( lang(?head) = "en" ) + } + +} +GROUP BY ?city ?head +ORDER BY DESC(?max_population) +LIMIT 10 +``` + +| max_population | city | head | +| -------------- | ------------- | --------------------------- | +| 2240621 | Paris | Anne Hidalgo | +| 2195914 | Houston | Annise Parker | +| 1388308 | Munich | Christine Strobl | +| 209860 | Rennes | Nathalie Appéré | +| 181513 | Trondheim | Rita Ottervik | +| 172693 | Albacete | Maria Carmen Bayod Guinalio | +| 153402 | Logroño | Cuca Gamarra | +| 137971 | Cádiz | Teófila Martínez | +| 102301 | Liberec | Martina Rosenbergová | +| 101319 | Ancona | Valeria Mancinelli | -- To view, visit https://gerrit.wikimedia.org/r/205995 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ie57fcf67ad69bfa6a9636b130e1d429c8d9234db Gerrit-PatchSet: 6 Gerrit-Project: wikidata/query/rdf Gerrit-Branch: master Gerrit-Owner: Jdouglas <[email protected]> Gerrit-Reviewer: Jdouglas <[email protected]> Gerrit-Reviewer: Manybubbles <[email protected]> Gerrit-Reviewer: Smalyshev <[email protected]> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
