Two or more match patterns

Adding match patterns

Until now our queries had only one pattern matching condition. For example, they selected items that have a P179 property (part of a series) with a value of Q22092344 (Star Wars film series). In the following query, there are two pattern matching conditions:

The query retrieves four items. These are items that satisfy both conditions. In other words, the patterns have an AND relation between them:
?item wdt:P179 wd:Q22092344.
AND
?item wdt:P57 Q38222.

# A little bit of syntax

Another way of formulating the above query, using a semicolon to join the two statements:

#Star Wars films

SELECT ?item  ?itemLabel
WHERE 
{ 
  ?item wdt:P179 wd:Q22092344; 	# item is part of the series Star Wars (film series)
  	   	wdt:P57 wd:Q38222. 		# item has director property with value George Lucas.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}

Because there is a semicolon on line 6, which joins the two patterns to match, the ?item variable is omitted on line 7.

UNION operator

Pattern matching with an OR relationship

In the previous section we saw that if there are two pattern matching conditions, they have an AND relation – the query will return only those items that match both patterns. If we want to have an OR relation between the conditions, we use the UNION operator. The following query retrieves items that are either part of the Star Wars film series or part of the Star Trek film series.

Skip to content