Wikidata:WikidataCon 2017/Notes/Introduction to the Wikidata Query Service and SPARQL

Title: Wikidata Query Service and SPARQL - Advanced Workshop

Speaker(s) edit

Name or username: Lucas Werkmeister Contact (email, Twitter, etc.): @WikidataFacts Useful links: slides: https://www.wikidata.org/wiki/User:Mhl20/SPARQL_Prefixes

Abstract edit

This will be a more advanced introduction to the Wikidata Query Service than the submission Query Wikidata - Basics Workshop, focusing on SPARQL. Attendees will learn what the underlying data model of the query service is, how SPARQL works and how to write their own queries.

Collaborative notes of the session edit

1. people with a spouse born in the same city as Goethe

SELECT ?person ?personLabel ?spouse ?spouseLabel {
 ?person wdt:P26 ?spouse.
 ?spouse wdt:P19 ?placeOfBirth.
 wd:Q5879 wdt:P19 ?placeOfBirth.
 SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 10
SELECT ?other_confLabel ?cityLabel WHERE {
 SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
 wd:Q37807682 wdt:P276 ?city.
 ?other_conf  wdt:P276 ?city.
}
LIMIT 100

2. anything which has an organizer and is in the same city as WikidataCon 2017

SELECT ?item ?itemLabel ?cityLabel WHERE {
 ?item wdt:P664 ?organizer.
 ?item wdt:P276 ?city.
 wd:Q37807682 wdt:P276 ?city.
 SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 100

3. conferences in the same city as WikidataCon 2017

SELECT ?item ?itemLabel ?cityLabel WHERE {
 ?item wdt:P31/wdt:P279* wd:Q625994.
 ?item wdt:P276 ?city.
 wd:Q37807682 wdt:P276 ?city.
 SERVICE wikibase:label { bd:serviceParam wikibase:language "en,de". }
}
LIMIT 100

4. population of Berlin at different times

SELECT ?population ?pointInTime WHERE {
 wd:Q64 p:P1082 [
   ps:P1082 ?population;
   pq:P585 ?pointInTime;
   wikibase:rank ?rank
 ].
 FILTER(?rank != wikibase:DeprecatedRank)
}
SELECT ?workLabel ?performanceLabel ?date ?location WHERE {
 ?work wdt:P31 wd:Q2743.
 ?performance wdt:P1191 ?date.
 ?performance wdt:P625 ?location.
              
                SERVICE wikibase:label {
               bd:serviceParam wikibase:language "en" .
   }
 }

5. items with a “first performance” with the “location” qualifier

SELECT ?workLabel ?date ?locationLabel WHERE {
 ?work p:P1191 ?statement.
 ?statement ps:P1191 ?date.
 ?statement pq:P276 ?location.
 SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

6. locations with most first performances

SELECT ?locationLabel (COUNT(DISTINCT ?work) AS ?count) WHERE {
 ?work p:P1191 [
   ps:P1191 ?date;
   pq:P276 ?location
 ].
 SERVICE wikibase:label { bd:serviceParam wikibase:language "en,de,fr,it,hr". }
}
GROUP BY ?locationLabel
ORDER BY DESC(?count)

7. items with more than one first performance

SELECT ?item ?itemLabel ?itemDescription (COUNT(?statement) AS ?count) WHERE {
 ?item p:P1191 ?statement.
 SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?item ?itemLabel ?itemDescription
HAVING(?count > 1)
ORDER BY DESC(?count)

8. elements with shortest half-life (in seconds)

SELECT ?item ?itemLabel ?halfLife ?unit ?unitLabel WHERE {
 ?item p:P2114 ?statement.
 ?statement psn:P2114 ?value.
 ?value wikibase:quantityAmount ?halfLife.
 ?value wikibase:quantityUnit ?unit.
 SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ASC(?halfLife)