Jdouglas has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/205995

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, 196 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikidata/query/rdf 
refs/changes/95/205995/1

diff --git a/docs/sparql-query-examples.md b/docs/sparql-query-examples.md
new file mode 100644
index 0000000..675f46e
--- /dev/null
+++ b/docs/sparql-query-examples.md
@@ -0,0 +1,196 @@
+# 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 wd: <http://www.wikidata.org/entity/>
+select ?name where {
+  wd:Q39246 wd:P108s/wd:P108v ?employer .
+  ?employer wd:P373s/wd:P373v ?name     .
+} LIMIT 10
+```
+
+| name                               |
+| ---------------------------------- |
+| Cornell University                 |
+| California Institute of Technology |
+
+## Who are Feynman's colleagues?
+
+```sparql
+prefix wd: <http://www.wikidata.org/entity/>
+select ?employerName ?colleagueName where {
+  wd:Q39246  wd:P108s/wd:P108v ?employer      .
+  ?colleague wd:P108s/wd:P108v ?employer      .
+  ?employer  wd:P373s/wd:P373v ?employerName  .
+  ?colleague wd:P373s/wd:P373v ?colleagueName .
+} LIMIT 10
+```
+
+| employerName       | colleagueName              |
+| ------------------ | -------------------------- |
+| Cornell University | Charles De Garmo           |
+| Cornell University | Jeremiah Jenks             |
+| Cornell University | Deborah Estrin             |
+| Cornell University | John Cleese                |
+| Cornell University | Simon Ungers               |
+| Cornell University | Gerhard Herzberg           |
+| Cornell University | Otto F. Kernberg           |
+| Cornell University | William Thurston           |
+| Cornell University | Andy Goldsworthy           |
+| Cornell University | Adolf Meyer (psychiatrist) |
+
+## What are the fields of Feynman's colleagues?
+
+* field of work: [P101](https://www.wikidata.org/wiki/Property:P101)
+
+```sparql
+prefix wd: <http://www.wikidata.org/entity/>
+select ?colleagueName ?fieldName where {
+  wd:Q39246  wd:P108s/wd:P108v ?employer      .
+  ?colleague wd:P108s/wd:P108v ?employer      .
+  ?colleague wd:P373s/wd:P373v ?colleagueName .
+  ?colleague wd:P101s/wd:P101v ?field         .
+  ?field     wd:P373s/wd:P373v ?fieldName     .
+} LIMIT 10
+```
+
+| colleagueName         | fieldName           |
+| --------------------- | ------------------- |
+| William Thurston      | Topology            |
+| Carl Sagan            | Astrobiology        |
+| Richard Feynman       | Particle physics    |
+| Carl Sagan            | Planetary science   |
+| Jorge Cham            | Webcomics           |
+| J. Robert Oppenheimer | Theoretical physics |
+| Richard Feynman       | Particle physics    |
+| J. Robert Oppenheimer | Nuclear physics     |
+
+## What are the fields of Feynman's colleagues who are physicists?
+
+* occupation: [P106](https://www.wikidata.org/wiki/Property:P106)
+* physicist: [Q169470](https://www.wikidata.org/wiki/Q169470)
+
+```sparql
+prefix wd: <http://www.wikidata.org/entity/>
+select ?colleagueName ?fieldName ?employerName where {
+  wd:Q39246  wd:P108s/wd:P108v ?employer      .
+  ?colleague wd:P108s/wd:P108v ?employer      .
+  ?colleague wd:P106s/wd:P106v wd:Q169470     .
+  ?employer  wd:P373s/wd:P373v ?employerName  .
+  ?colleague wd:P373s/wd:P373v ?colleagueName .
+  ?colleague wd:P101s/wd:P101v ?field         .
+  ?field     wd:P373s/wd:P373v ?fieldName     .
+} LIMIT 10
+```
+
+| colleagueName         | fieldName           | employerName                   
    |
+| --------------------- | ------------------- | 
---------------------------------- |
+| Richard Feynman       | Particle physics    | Cornell University             
    |
+| Richard Feynman       | Particle physics    | California Institute of 
Technology |
+| J. Robert Oppenheimer | Theoretical physics | California Institute of 
Technology |
+| J. Robert Oppenheimer | Nuclear physics     | California Institute of 
Technology |
+
+## Whose birthday is it?
+
+```sparql
+prefix wd: <http://www.wikidata.org/entity/>
+prefix wdo: <http://www.wikidata.org/ontology#>
+prefix xsd: <http://www.w3.org/2001/XMLSchema#>
+select ?entityLabel ?date where {
+  ?entity wd:P569s/wd:P569v     ?dateV           .
+  ?dateV  wdo:preferredCalendar wd:Q1985727      .
+  ?dateV  wdo:time              ?date            .
+  ?entity wd:P373s/wd:P373v     ?entityLabel     .
+  filter ( regex(str(?date), "\\d{4}-\\d{2}-\\d{2}") )
+  filter ( xsd:integer(substr(str(?date), 6, 2)) = month(now()) )
+  filter ( xsd:integer(substr(str(?date), 9, 2)) =   day(now()) )
+} limit 10
+```
+
+| entityLabel             | date       |
+| ----------------------- | ---------- |
+| Catherine Bott          | 1995-04-22 |
+| Pavel Horváth           | 1975-04-22 |
+| Carlos Sastre           | 1975-04-22 |
+| Salvatore Coco          | 1975-04-22 |
+| Mark van Bommel         | 1977-04-22 |
+| Andy Baio               | 1977-04-22 |
+| Robert Hunter (cyclist) | 1977-04-22 |
+| Ambra Angiolini         | 1977-04-22 |
+| Anna Eriksson           | 1977-04-22 |
+| Pavel Churavý           | 1977-04-22 |
+
+## What happened on this day in history?
+
+```sparql
+prefix wd: <http://www.wikidata.org/entity/>
+prefix wdo: <http://www.wikidata.org/ontology#>
+prefix xsd: <http://www.w3.org/2001/XMLSchema#>
+select ?entityLabel ?date where {
+  ?entity ?x                    ?dateS           .
+  ?dateS  ?y                    ?dateV           .
+  ?dateV  wdo:preferredCalendar wd:Q1985727      .
+  ?dateV  wdo:time              ?date            .
+  ?entity wd:P373s/wd:P373v     ?entityLabel     .
+  filter ( regex(str(?date), "\\d{4}-\\d{2}-\\d{2}") )
+  filter ( xsd:integer(substr(str(?date), 6, 2)) = month(now()) )
+  filter ( xsd:integer(substr(str(?date), 9, 2)) =   day(now()) )
+} limit 10
+```
+
+| entityLabel             | date       |
+| ----------------------- | ---------- |
+| The Social Network      | 1960-04-22 |
+| Ayerbe                  | 1960-04-22 |
+| Peter Higgs             | 1960-04-22 |
+| Molinicos               | 1960-04-22 |
+| Zevenhuizen-Moerkapelle | 1960-04-22 |
+| Isòvol                  | 1960-04-22 |
+| Centelles               | 1960-04-22 |
+| Folgueroles             | 1960-04-22 |
+| Oristà                  | 1960-04-22 |
+| Montesquiu              | 1960-04-22 |
+
+## What are the top most populous cities in the world with female mayors?
+
+```sparql
+PREFIX wd: <http://www.wikidata.org/entity/>
+PREFIX wdo: <http://www.wikidata.org/ontology#>
+SELECT ?city (MAX(?population) AS ?max_population) ?citylabel ?mayorlabel 
WHERE {
+  ?city      wd:P31s/wd:P31v wd:Q515     .      # find instances of subclasses 
of city
+  ?city      wd:P6s          ?statement  .      # with a P6 (head of 
goverment) statement
+  ?statement wd:P6v          ?mayor      .      # ... that has the value ?mayor
+  ?mayor     wd:P21s/wd:P21v wd:Q6581072 .      # ... where the ?mayor has P21 
(sex or gender) female
+  FILTER NOT EXISTS { ?statement wd:P582q ?x }  # ... but the statement has no 
P582 (end date) qualifier
+
+  # Now select the population value of the ?city
+  # (the number is reached through a chain of three properties)
+  ?city wd:P1082s/wd:P1082v/wdo:numericValue ?population .
+
+  # Optionally, find English labels for city and mayor:
+  OPTIONAL {
+    ?city wd:P373s/wd:P373v ?citylabel .
+  }
+  OPTIONAL {
+    ?mayor wd:P373s/wd:P373v ?mayorlabel .
+  }
+
+}
+GROUP BY ?city ?citylabel ?mayorlabel
+ORDER BY DESC(?max_population) LIMIT 10
+```
+
+| city                                    | max_population | citylabel | 
mayorlabel        |
+| --------------------------------------- | -------------- | --------- | 
----------------- |
+| <http://www.wikidata.org/entity/Q2807>  | 3273049        | Madrid    | Ana 
Botella       |
+| <http://www.wikidata.org/entity/Q1085>  | 1246780        | Prague    | 
Adriana Krnáčová  |
+| <http://www.wikidata.org/entity/Q8818>  | 814208         | Valencia  | Rita 
Barberà      |
+| <http://www.wikidata.org/entity/Q72>    | 440180         | Zürich    | 
Corine Mauch      |
+| <http://www.wikidata.org/entity/Q11959> | 335052         | Alicante  | Sonia 
Castedo     |
+| <http://www.wikidata.org/entity/Q25804> | 181513         | Trondheim | Rita 
Ottervik     |
+| <http://www.wikidata.org/entity/Q15682> | 137971         | Cádiz     | 
Teófila Martínez  |

-- 
To view, visit https://gerrit.wikimedia.org/r/205995
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie57fcf67ad69bfa6a9636b130e1d429c8d9234db
Gerrit-PatchSet: 1
Gerrit-Project: wikidata/query/rdf
Gerrit-Branch: master
Gerrit-Owner: Jdouglas <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to