SPARQL Query Basics

This will run through how to create a SPARQL query from scratch. SPARQL is a programming language for querying linked data stored on the web. It is essentially a set of commands that allow you to find exactly the data you want.

Watch – an Intro to SPARQL queries by Dr. Chris Langley, Newman University.

Learning SPARQL will allow you to query the information stored in Wikidata but also the countless other data sources offering a SPARQL query service.

e.g. Show me a list of French female writers along with their date of birth, place of birth, and the map co-ordinates of their place of birth.

How long would it take to do this using Google?! In Wikidata it takes seconds.

Building a SPARQL query

To do this go to https://query.wikidata.org/

To show how the Query Service works, we will create a query for Women Writers.

Step 1 – Use a notable example to get you started. For women writers, we will go to Virginia Woolf.

Step 2 – Scroll down to see how occupations (P106) are described on Virginia Woolf’s page as this will be the crux of our query. A writer is another item on Wikidata with a unique identifier of Q36180

Step 3 – Go to https://query.wikidata.org/ and type the following.

SELECT   ?item

WHERE {

?item

}

This will select an item of data on Wikidata. SELECT is telling you what you want to see back in your query in terms of the columns of information it will return in its results. WHERE is where we define what the relationships are that are going to define our query.

But we need to define what properties we are interested in but let’s change the item name to something more relevant to our query (ie. a person) and add in the unique identifiers for occupation and the value of writer.

SELECT   ?person

WHERE {

?person wdt:P106 wd:Q36180 .

}

Holding down CTRL and pressing SPACE is a Wikidata Query Service trick that helps you find properties and values without having to look up the P number and Q number each time.

If we hit the RUN symbol in the bottom left of the page then the query will return a list of results. 158,000 plus results. But it just returns it as a list of Q numbers. Wikidata has a label service so that we can display the list of English language labels. So we can add the label service and make sure we add ?personLabel to the SELECT line too.

SELECT ?person ?personLabel

WHERE {

?person wdt:P106 wd:Q36180 .

SERVICE wikibase:label { bd:serviceParam wikibase:language “[AUTO_LANGUAGE],en”. }
}

RUN the query to display the results now. We should have two columns now.

Tip: We can add in different language labels too by adding the two letter code for that language e.g. ar for arabic .

e.g. SERVICE wikibase:label { bd:serviceParam wikibase:language “ar,en”. }

We can filter the query further. To add the property of gender (P21) with a value of female (Q6581072)

SELECT ?person ?personLabel

WHERE {

?person wdt:P106 wd:Q36180 .

                       ?person wdt:P21   wd:Q6581072 .

SERVICE wikibase:label { bd:serviceParam wikibase:language “en”. }
}

We can display the results with images so that the query comes to life. Again making sure we add ?image to the SELECT line at the top. The difference this time is we are not specifying what the value for the image should be.

SELECT ?person ?personLabel ?image

WHERE {

?person wdt:P106 wd:Q36180 .

?person wdt:P21   wd:Q6581072 .

                        ?person wdt:P18 ?image .

SERVICE wikibase:label { bd:serviceParam wikibase:language “en”. }
}

We can click RUN to display the results but we should change the display results from Table to Image Grid (using the dropdown menu in the bottom left of the screen) in order to get the images displayed.

We can also add in their date of birth (P569) and place of birth (P19).

NB: Place of birth requires you to add an extra line if you wish to pull in the co-ordinates of their place of birth so it can be visualised on a map.

SELECT ?person ?personLabel ?image ?birthDate ?birthPlace ?coords

WHERE {

?person wdt:P106 wd:Q36180 .
?person wdt:P21 wd:Q6581072 .
?person wdt:P18 ?image .
?person wdt:P569 ?birthDate .
?person wdt:P19 ?birthPlace .
?birthPlace wdt:P625 ?coords .

SERVICE wikibase:label { bd:serviceParam wikibase:language “ar,en”. }
}

Clicking RUN and setting the display results to MAP (again accessed from the bottom left dropdown menu) will show the co-ordinate locations of the places of birth of female writers in Wikidata.

However, each line of the query further the filters the results to only those results that have a) an occupation b) gender c) an image, a date of birth AND a place of birth. Perhaps we don’t want to exclude some items of data that don’t have an image, date of birth or place of birth from our results. In that case, we can use the OPTIONAL command.

SELECT ?person ?personLabel ?image ?birthDate ?birthPlace ?coords

WHERE {

?person wdt:P106 wd:Q36180 .
?person wdt:P21 wd:Q6581072 .
OPTIONAL {?person wdt:P18 ?image .}
OPTIONAL {?person wdt:P569 ?birthDate .}
OPTIONAL {?person wdt:P19 ?birthPlace .
?birthPlace wdt:P625 ?coords .}

SERVICE wikibase:label { bd:serviceParam wikibase:language “ar,en”. }
}

Play around with more SPARQL queries

  1. You can access more SPARQL queries from the Examples dropdown at the top left of the Wikidata Query Service.
  2. https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries
  3. Pdf handout explaining the Query Service (in brief).
  4. Wikidata Sparql Query Service – video tutorial.
  5. Learning SPARQL (book).