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
  ?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 a ?director variable (which we see in the WHERE section) but it isn’t presented in the query results.

Skip to content