Basic

OPTIONAL

Include results even when data is missing. OPTIONAL allows patterns that may or may not match, returning results with empty values for missing data instead of excluding them entirely.

Understanding OPTIONAL

Without OPTIONAL, SPARQL requires all patterns to match. If an entity lacks a property, it's excluded from results. OPTIONAL lets you request data that might not exist—if it does, you get it; if not, the result row still appears with that variable unbound.

Basic OPTIONAL Examples

Scientists with Optional Death Date
Run ↗
SELECT ?person ?personLabel ?birth ?death
WHERE {
  ?person wdt:P31 wd:Q5 ;
          wdt:P106 wd:Q901 ;
          wdt:P569 ?birth .
  OPTIONAL { ?person wdt:P570 ?death . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 20

This query finds scientists with their birth dates (required) and death dates (optional). Living scientists appear in results with an empty ?death value. Without OPTIONAL, only deceased scientists would be returned.

Cities with Optional Population
Run ↗
SELECT ?city ?cityLabel ?population
WHERE {
  ?city wdt:P31 wd:Q515 ;
        wdt:P17 wd:Q183 .
  OPTIONAL { ?city wdt:P1082 ?population . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 30

Not all cities in Wikidata have population data recorded. Using OPTIONAL ensures we get all German cities, showing population when available and leaving it empty when not recorded.

Multiple OPTIONAL Clauses

Authors with Optional Birth/Death Places
Run ↗
SELECT ?author ?authorLabel ?birthPlaceLabel ?deathPlaceLabel
WHERE {
  ?author wdt:P31 wd:Q5 ;
          wdt:P106 wd:Q36180 .
  OPTIONAL { ?author wdt:P19 ?birthPlace . }
  OPTIONAL { ?author wdt:P20 ?deathPlace . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 25

Multiple OPTIONAL clauses are independent—each is tried separately. An author might have a birth place but no death place, or vice versa, or neither, or both. All combinations appear in the results.

OPTIONAL with FILTER

Filtering on Optional Values
Run ↗
SELECT ?movie ?movieLabel ?budget
WHERE {
  ?movie wdt:P31 wd:Q11424 ;
         wdt:P577 ?date .
  FILTER(YEAR(?date) = 2020)
  OPTIONAL { ?movie wdt:P2130 ?budget . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 30

The FILTER applies to required patterns, while OPTIONAL values are still included when available. This gets all 2020 films, showing budget data only for films where it's recorded. Use BOUND() with FILTER to check if optional values exist.

Key Entities Used in Examples

Entity ID Description
human wd:Q5 Any human being
scientist wd:Q901 Person who conducts research
city wd:Q515 Large human settlement
Germany wd:Q183 Country in Central Europe
writer wd:Q36180 Person who uses written words
film wd:Q11424 Sequence of images for cinema
date of birth wdt:P569 Date when person was born
date of death wdt:P570 Date when person died
place of birth wdt:P19 Location where person was born
place of death wdt:P20 Location where person died
cost wdt:P2130 Production budget amount
publication date wdt:P577 Date of first publication