1 of 18

Wikidata

Data Formats tutorial

2 of 18

Initial information

We are going to query Wikidata using the Wikidata Query Service (WDQS).

Typical workflow:

2-3 windows

  1. one with the Wikidata Query Service, where query is written
  2. one with Wikidata - browsing examples of intended results, looking at relevant Q and P numbers
  3. one with Turtle representation of the Item, e.g. https://www.wikidata.org/wiki/Special:EntityData/Q14960.ttl to see the RDF prefixes and datatypes

2

Tooltips on items and properties provide names

There is auto-complete support (CTRL+SPACE)

3 of 18

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

4 of 18

Task 2

10 most populous cities in the Czech Republic, ordered by population

4

5 of 18

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.

6 of 18

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

7 of 18

Task 3

10 most populous cities in the Czech Republic, ordered by population in 1980

7

8 of 18

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

9 of 18

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.

10 of 18

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

11 of 18

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)

12 of 18

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

13 of 18

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

14 of 18

Task 4

Try the query from Task 3 in Yasgui

  • https://query.wikidata.org/sparql
  • you will be missing the prefixes
  • but it will still work
  • but if you’ll try to autofill them using Yasgui, p: will not work (too short) and ps: will be autofilled wrong
    • It is always better to include the prefixes explicitly when sharing SPARQL queries

#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

15 of 18

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

16 of 18

Task 5

Get RDF representation instead of the tabular results of:�10 most populous cities in the Czech Republic, ordered by population�

16

17 of 18

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

18 of 18

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