Basic

Multiple Patterns

Combine patterns with UNION and joins. UNION lets you match alternative patterns, while joining patterns together requires matching all conditions.

Understanding Pattern Combination

By default, patterns in SPARQL are joined together (AND logic)—all must match. UNION provides OR logic—either pattern can match. Understanding when to use each is key to writing effective queries.

UNION for Alternatives

Writers OR Poets
Run ↗
SELECT ?person ?personLabel ?occupationLabel
WHERE {
  {
    ?person wdt:P31 wd:Q5 ;
            wdt:P106 wd:Q36180 .
    BIND(wd:Q36180 AS ?occupation)
  }
  UNION
  {
    ?person wdt:P31 wd:Q5 ;
            wdt:P106 wd:Q49757 .
    BIND(wd:Q49757 AS ?occupation)
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 20

UNION combines results from two patterns. A person matching either pattern (writer Q36180 or poet Q49757) appears in results. BIND captures which pattern matched for display. For simpler cases, consider VALUES instead.

Simpler Alternative with VALUES
Run ↗
SELECT ?person ?personLabel ?occupationLabel
WHERE {
  VALUES ?occupation { wd:Q36180 wd:Q49757 }
  ?person wdt:P31 wd:Q5 ;
          wdt:P106 ?occupation .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 20

When alternatives only differ in one value, VALUES is cleaner than UNION. Use UNION when the alternative patterns have different structures or involve different variables.

Complex UNION Patterns

Different Structures in UNION
Run ↗
SELECT ?item ?itemLabel ?type
WHERE {
  {
    ?item wdt:P31 wd:Q5 ;
          wdt:P27 wd:Q142 .
    BIND("French person" AS ?type)
  }
  UNION
  {
    ?item wdt:P31 wd:Q515 ;
          wdt:P17 wd:Q142 .
    BIND("French city" AS ?type)
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 20

UNION excels when patterns have completely different structures. Here we find both French people (humans with French citizenship) and French cities (cities in France) in a single query, labeling which type each result is.

Joining Multiple Patterns

Multiple Required Conditions
Run ↗
SELECT ?person ?personLabel ?birthPlaceLabel ?deathPlaceLabel
WHERE {
  ?person wdt:P31 wd:Q5 ;
          wdt:P106 wd:Q1930187 ;
          wdt:P19 ?birthPlace ;
          wdt:P20 ?deathPlace .
  ?birthPlace wdt:P17 wd:Q30 .
  ?deathPlace wdt:P17 wd:Q145 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 10

Without UNION, patterns are joined with AND logic. All patterns must match for a result to appear. This finds journalists who were born in the USA (Q30) AND died in the UK (Q145)—both conditions must be true.

Key Entities Used in Examples

Entity ID Description
human wd:Q5 Any human being
writer wd:Q36180 Person who uses written words
poet wd:Q49757 Person who writes poetry
journalist wd:Q1930187 Person who writes news
city wd:Q515 Large human settlement
France wd:Q142 Country in Western Europe
United States wd:Q30 Country in North America
United Kingdom wd:Q145 Country in Northwestern Europe
place of birth wdt:P19 Location where born
place of death wdt:P20 Location where died