Wikidata
Data Formats tutorial
This work is licensed under a Creative Commons Attribution 4.0 International License.
Initial information
We are going to query Wikidata using the Wikidata Query Service (WDQS).
Typical workflow:
2-3 windows
2
Tooltips on items and properties provide names
There is auto-complete support (CTRL+SPACE)
Task 1 - example queries
There is a list of example queries in WDQS. Pick a few of them, make sure you understand what they are doing.
Compare the queries to the Wikidata RDF Model
3
Task 2
10 most populous cities in the Czech Republic, ordered by population
4
Task 2 - solution 1
#10 most populous cities in the Czech Republic, ordered
SELECT ?city ?cityLabel ?population
WHERE
{
?city wdt:P31 wd:Q515 ; #instance of: City
wdt:P17 wd:Q213 ; #country: Czech Republic
wdt:P1082 ?population . #population
SERVICE wikibase:label { bd:serviceParam wikibase:language "cs,en". }
}
ORDER BY DESC(?population)
LIMIT 10
5
🤔
Obviously, there is something missing… like Prague or Brno.
This is because they are instances of big city, which is a subclass of city.
Task 2 - solution 2
#10 most populous cities in the Czech Republic, ordered
SELECT DISTINCT ?city ?cityLabel ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 ; #instance of/(subclass of)*: City
wdt:P17 wd:Q213 ; #country: Czech Republic
wdt:P1082 ?population . #population
SERVICE wikibase:label { bd:serviceParam wikibase:language "cs,en". }
}
ORDER BY DESC(?population)
LIMIT 10
6
💡
wdt:P31/wdt:P279* - quite typical construct
Task 3
10 most populous cities in the Czech Republic, ordered by population in 1980
7
Task 3 - solution 1
#10 most populous cities in the Czech Republic, ordered
SELECT DISTINCT ?city ?cityLabel ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 ; #instance of: City
wdt:P17 wd:Q213 ; #country: Czech Republic
p:P1082 ?populationStatement . #population
?populationStatement ps:P1082 ?population ;
pq:P585 "1980-01-01T00:00:00Z"^^xsd:dateTime . #point in time
SERVICE wikibase:label { bd:serviceParam wikibase:language "cs,en". }
}
ORDER BY DESC(?population)
LIMIT 10
8
We need to use the “point in time” qualifier here
Task 3 - solution 1
#10 most populous cities in the Czech Republic, ordered
SELECT DISTINCT ?city ?cityLabel ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 ; #instance of: City
wdt:P17 wd:Q213 ; #country: Czech Republic
p:P1082 ?populationStatement . #population
?populationStatement ps:P1082 ?population ;
pq:P585 "1980-01-01T00:00:00Z"^^xsd:dateTime . #point in time
SERVICE wikibase:label { bd:serviceParam wikibase:language "cs,en". }
}
ORDER BY DESC(?population)
LIMIT 10
9
🤔
different numbers in Plzeň or Olomouc
This is quite normal in Wikidata - two sources say two different things.
Task 3 - solution 2
#10 most populous cities in Czechoslovakia, ordered
SELECT DISTINCT ?city ?cityLabel ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 ; #instance of: City
wdt:P17 wd:Q33946 ; #country: Czechoslovakia
p:P1082 ?populationStatement . #population
?populationStatement ps:P1082 ?population ;
pq:P585 "1980-01-01T00:00:00Z"^^xsd:dateTime .
SERVICE wikibase:label { bd:serviceParam wikibase:language "cs,en". }
}
ORDER BY DESC(?population)
LIMIT 10
10
In 1980, there was no Czech Republic.�It was Q33946 Czechoslovakia
Task 3 - solution 2
#10 most populous cities in Czechoslovakia, ordered
SELECT DISTINCT ?city ?cityLabel ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 ; #instance of: City
wdt:P17 wd:Q33946 ; #country: Czechoslovakia
p:P1082 ?populationStatement . #population
?populationStatement ps:P1082 ?population ;
pq:P585 "1980-01-01T00:00:00Z"^^xsd:dateTime .
SERVICE wikibase:label { bd:serviceParam wikibase:language "cs,en". }
}
ORDER BY DESC(?population)
LIMIT 10
11
🤔
wdt:P17 wd:Q33946 ; is not a truthy value - does not have best rank (see the previous screenshot)
Task 3 - solution 2
#10 most populous cities in Czechoslovakia, ordered
SELECT DISTINCT ?city ?cityLabel ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 ; #instance of: City
p:P17 ?countryStatement ; #country: Czechoslovakia
p:P1082 ?populationStatement . #population
?populationStatement ps:P1082 ?population ;
pq:P585 "1980-01-01T00:00:00Z"^^xsd:dateTime .
?countryStatement ps:P17 wd:Q33946 . #country: Czechoslovakia
SERVICE wikibase:label { bd:serviceParam wikibase:language "cs,en". }
}
ORDER BY DESC(?population)
LIMIT 10
12
🤔
still no Slovak cities?
… there is no “population in 1980” for them
Task 3 - solution 2
#10 most populous cities in Czechoslovakia, ordered
SELECT DISTINCT ?city ?cityLabel ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 ; #instance of: City
p:P17 ?countryStatement ; #country: Czechoslovakia
wdt:P1082 ?population . #population
?countryStatement ps:P17 wd:Q33946 . #country: Czechoslovakia
SERVICE wikibase:label { bd:serviceParam wikibase:language "cs,en". }
}
ORDER BY DESC(?population)
LIMIT 10
13
If this times out, remove the “cs” from the label service
Task 4
Try the query from Task 3 in Yasgui
#10 most populous cities in the Czech Republic, ordered
SELECT DISTINCT ?city ?cityLabel ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 ; #instance of/(subclass of)*: City
wdt:P17 wd:Q213 ; #country: Czech Republic
wdt:P1082 ?population . #population
SERVICE wikibase:label { bd:serviceParam wikibase:language "cs,en". }
}
ORDER BY DESC(?population)
LIMIT 10
14
Task 4 - solution
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
#10 most populous cities in Czech Republic, ordered
SELECT DISTINCT ?city ?cityLabel ?population
WHERE
{
?city wdt:P31/wdt:P279* wd:Q515 ; #instance of: City
wdt:P17 wd:Q213 ; #country: Czech Republic
p:P1082 ?populationStatement . #population
?populationStatement ps:P1082 ?population ;
pq:P585 "1980-01-01T00:00:00Z"^^xsd:dateTime . #point in time
SERVICE wikibase:label { bd:serviceParam wikibase:language "cs,en". }
}
ORDER BY DESC(?population)
LIMIT 10
15
Task 5
Get RDF representation instead of the tabular results of:�10 most populous cities in the Czech Republic, ordered by population�
16
Task 5 - solution
PREFIX schema: <http://schema.org/>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
#10 most populous cities in Czech Republic, ordered
CONSTRUCT {
?city wdt:P31 wd:Q515 ; #instance of/(subclass of)*: City
wdt:P17 wd:Q213 ; #country: Czech Republic
wdt:P1082 ?population ; #population
schema:name ?cityLabel .
}
WHERE {
?city wdt:P31/wdt:P279* wd:Q515 ; #instance of/(subclass of)*: City
wdt:P17 wd:Q213 ; #country: Czech Republic
wdt:P1082 ?population . #population
SERVICE wikibase:label { bd:serviceParam wikibase:language "cs,en". }
}
17
The label service works here as well
The label service works here as well
Task 6 - example query visualizations
See the “Destinations from Antwerp International airport” sample query and see the Map result and the Table result.
See Wikidata:SPARQL query service/Wikidata Query Help/Result Views for overview of which values are required for which result visualization.
#Destinations from Antwerp International airport
#defaultView:Map
SELECT ?connectsairport ?connectsairportLabel ?place_served ?place_servedLabel ?coor
WHERE
{
VALUES ?airport { wd:Q17480 } # Antwerp international airport wd:Q17480
?airport wdt:P81 ?connectsairport ;
wdt:P625 ?base_airport_coor .
?connectsairport wdt:P931 ?place_served ;
wdt:P625 ?coor .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
18
specifies default view in WDQS