Another exercise

| Exercise: Write a query to show where the actors in each film in the Star Wars film series were born.

Tip: use DISTINCT to remove duplicates from the results

show solution

Edit the query to add the image (P18) of each actor to the results

show solution

If you’re wondering why there are less results than in the previous query, see the section about the OPTIONAL clause.

Dates

When queries: Dates

These queries retrieve date information making it possible to present this information in the form of a timeline.

We recommend going over the introduction to Wikidata and learning about the structure of Wikidata, as well as the section regarding the simplest query before proceeding.

Let’s start with a simple example: The following query shows when the albums of the singer Madonna (Q1744) were released:

After running the query, scroll to the results. Above the table, click on the arrow next to the eye. A drop-down menu will pop up. Choose “Timeline” from the menu, and WDQS will display the results on a timeline.

Another way to display the results in the timeline view is to specify it in the query itself. The code on line 2, after the hashtag sign, tells the query that the query results should be shown not as a table, but as a timeline.

You don’t need to remember the exact code for the map view: thanks to the autocompletion function of WDQS, once you type a hashtag in the query window, a drop-down menu will suggest the different display options.

Linked dates

Retrieving dates linked to values

In the previous query, dates were directly linked to the items we selected. But dates are not always directly linked to the items of interest to us. Once you have learned to retrieve data linked to values, it is quite easy to retrieve date information related to items. For example, the following query shows the date of birth of the directors of the Star Wars movies on a time scale:

Note the query uses the DISTINCT modifier to remove duplicates from the results.

Exercise: dates

| Exercise: Write a query that shows when women who are heads of government of sovereign states were born

Hint: select sovereign states (Q3624078) that have a head of government (P6) who is a woman (i.e., sex (P21) has the value female (Q6581072)), and retrieve their dates of birth.

show solution

Dates and qualifiers

Dates and qualifiers

If you have learned how to query statements with qualifiers, try this exercise:

| Exercise: Write a query to show on a timeline the date of birth of actors that played James Bond in the James Bond film series (Q2484680)

Hint: the cast member property (P161) has P453 (character role) property as a qualifier with the value Q2009573 (James Bond)

Tip: use DISTINCT to remove duplicates from the results

show solution

Exercise: date qualifers

Exercise: Write a query showing women Nobel prize winners in Literature and when they received the award.

Hint: the award received property (P166) has P585 (point in time) as qualifier

show solution

If you have already learned about instances and classes, try editing the query so it shows all women who have won a Nobel prize, which award it was, and when they received it.

Hint: the value of the award received property (P166) is a subclass (P279) of Nobel Prize (Q7191).

show solution

Modifying your results

Modifying your results

In all the queries we have seen until now, all items that matched certain patterns were selected and shown in the results. But quite often we are only interested in certain results, or some aspect of them. In this section you will learn a few keywords and functions that will help you get more out of your data.

Our queries so far had two clauses: the SELECT clause where we stated the variables that will be shown in the results, and the WHERE clause in which the patterns for matching were given.

#Star Wars Films
SELECT ?item  
WHERE 
{
  ?item wdt:P179 wd:Q22092344.
}

In the next section you will learn to modify your query results by adding keywords and functions to the SELECT clause, to the WHERE clause, or after the WHERE clause. In fact, you may have already encountered such an element in previous sections – the DISTINCT modifier which can be added to the SELECT clause to remove duplicates from the results.

COUNT

COUNT

The aggregate function COUNT can be used to write queries to answer how many items there are that match a certain pattern. In such cases, we are not interested in the items that match our query pattern themselves, but just want to know the number of the items that match.

Suppose we wanted to know how many Wikidata items there are about women chemists. In principle, we could run a query as follows:

Above the table with the results, WDQS shows how many items were found in how many milliseconds of running the query.

This strategy won’t always work, however. Suppose we wanted to know how many Wikidata items there are about actresses. In principle, we could edit the query as follows:

#Actresses on Wikidata
SELECT ?item ?itemLabel
WHERE {
  ?item wdt:P31 wd:Q5.            # Item is instance of human
  ?item wdt:P21 wd:Q6581072.      # Item is a woman
  ?item wdt:P106 wd:Q33999.       # Occupation is actor.     
  
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }  
  }

In principle, WDQS would return all matching items and show us how many items were found that match the pattern. However, because the number of matching items is very large, the query will time out. This is due to a query deadline which is set to 60 seconds. Every query that takes more time to execute than this configured deadline will time out:

To answer how many actresses there are on Wikidata we declare a variable in the SELECT clause, in this example we call it ?actresscount. The variable is defined as the COUNT of items with patterns matching those in the WHERE clause:

This query has only one result: the variable actresscount with the value of the count of all items matching the search pattern (human and female and actress)

Skip to content