Query with more than one variable

Queries with more than one variable

Until now our queries essentially had just one variable, even if additional variables were declared for the label and/or description of the item. Now we will look at queries with more variables.
The following query retrieves items that are part of the Star Wars film series, and the director of each film.

#Star Wars films

SELECT ?item  ?itemLabel ?director
WHERE 
{ 
  ?item wdt:P179 wd:Q22092344. # item is part of the series Star Wars (film series)
  ?item wdt:P57 ?director.     # item’s director property’s value is collected by the director variable
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}

Query explanation

In the SELECT section we have the variables ?item and ?director, as well as ?itemLabel which retrieves the label for ?item.

In the WHERE section, on lines 6 and 7, we see the pattern matching conditions:
?item wdt:P179 wd:Q22092344.
?item wdt:P57 ?director.

SPARQL seeks Wikidata items with statements that match the pattern defined in the WHERE section. So, as before, our WHERE section’s first line says, “Find me items that have a statement with a P179 property (part of a series) with the value Q22092344 (Star Wars (film series))”.
The second line says, “Then for each of those items, find me their P57 property (director) and put its value in the variable ?director.”
As we’ve seen, there is an implicit AND between each statement in the WHERE section, i.e. only patterns that match all statements will be returned by this query. 

Now let’s run the query:

What needs to be added to see the names of the directors? Add it and run the query again.

Show solution

# A little bit of syntax

If we wanted the query to show the name of each director, but not the Q number, we could omit the ?director from the SELECT section and only declare ?directorLabel. This implies there is a ?director variable (which we see in the WHERE section) but it isn’t presented in the query results.

Exercise: more than one variable

| Exercise: Write a query to show the director and screenwriter (P58) of each film in the Star Wars film series

show solution

or, using a different syntax (see the explanation here):

The previous query which listed the director of each Star Wars film returned 9 results. This query returns 16 results. Why?

Because there are more than one screenwriter for some of the films. So we get one line for each screenwriter.

Multiple values

Properties with multiple values

Multiple values for a property is not a problem for a database like Wikidata. There is nothing wrong with having several values for the same property. For certain statements – such as the children of a person or the official languages of a country – it is perfectly reasonable to have multiple values. Essentially these are additional property-value pairs. 

For example for item Star Wars Episode V: The Empire Strikes Back (Q181795) the screenwriter property has these values:

A graphic representation would be:

A graphic representation of the director and screenwriter properties and their values for Wikidata item Q181795

Another exercise

| Exercise: Show director and cost (P2130) of each film in the Star Wars series

show solution

or using a different syntax (see the explanation here):

previous query which retrieved each Star Wars film and its director returned 9 results. This current query only returns 8 results. Why?

Because some items don’t have cost (P2130) as a property, and are therefore ignored by the query. 

The OPTIONAL clause

Missing values and the OPTIONAL clause

As we’ve seen, when there is more than one pattern to match in the WHERE clause, there is an implicit AND between the statements, such that only patterns that match all statements will be returned by the query. For example, in the last exercise, there were three matching patterns:

#Star Wars films

SELECT ?item  ?itemLabel ?directorLabel ?cost
WHERE 
{ 
  ?item wdt:P179 wd:Q22092344.       # item is part of the series Star Wars (film series)
  ?item wdt:P57 ?director.           # item’s director property’s value is collected by the director variable
  ?item wdt:P2130 ?cost.  			 # item's cost property's value is collected by the cost variable
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}

The statements on line 6, 7, and 8 have an AND relation between them. Therefore, an item will be considered a match only if it has a P179 property (part of a series) with a value of Q22092344 (Star Wars film series), as well as a P57 (director) property and a P2130 (cost) property. If an item doesn’t match one of these statements (e.g., doesn’t have the property cost) it is ignored by the query.  

SPARQL is a pattern-matching query language. SPARQL queries will only return data when the pattern in the WHERE clause exactly matches the pattern in the data you’re querying. But many datasets have missing values, and data is only a match for the query if there is valid data in each piece of the statement declared within WHERE. This means SPARQL will not return an item that is missing property name or value requested in WHERE.

The OPTIONAL keyword within the WHERE clause denotes optional patterns you’d like to match in the data. OPTIONAL allows searching for data that may or may not be there.

Run the query again with cost as an optional pattern.

Image Grid View

Image Grid View

WDQS can display the results of our queries in different formats. So far we have always seen the results in table format. Let’s look again at the previous exercise:

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 “Image grid” from the menu, and WDQS will display each item’s logo, with each item’s results underneath.

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

You don’t need to remember the exact code for the image grid: 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.

Skip to content