VALUES
Define inline data for queries. VALUES lets you specify a set of values for variables, constraining results to match only those specific values.
Understanding VALUES
VALUES provides inline data to a query, acting like a small embedded table. It's more efficient than multiple UNION clauses or long FILTER IN lists, and it makes queries easier to read when working with specific sets of entities.
Basic VALUES Examples
SELECT ?country ?countryLabel ?population ?capital ?capitalLabel
WHERE {
VALUES ?country { wd:Q142 wd:Q183 wd:Q38 wd:Q29 wd:Q145 }
?country wdt:P1082 ?population ;
wdt:P36 ?capital .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
VALUES with a single variable specifies a list of values to query. This is more efficient than using FILTER(?country IN (wd:Q142, ...)) because the query optimizer can use the VALUES directly when matching patterns.
SELECT ?person ?personLabel ?occupationLabel
WHERE {
VALUES ?occupation { wd:Q36180 wd:Q49757 wd:Q1930187 }
?person wdt:P31 wd:Q5 ;
wdt:P27 wd:Q30 ;
wdt:P106 ?occupation .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 20
VALUES can constrain any variable in your pattern. Here it limits occupations to writers, poets, and journalists, effectively creating an OR condition for the occupation property.
Multiple Variables
SELECT ?country ?countryLabel ?language ?languageLabel
WHERE {
VALUES (?country ?language) {
(wd:Q142 wd:Q150)
(wd:Q183 wd:Q188)
(wd:Q38 wd:Q652)
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
VALUES can bind multiple variables together. Each row in the VALUES block represents a set of values that must match together. This is useful for querying related pairs or tuples of data.
VALUES with UNDEF
SELECT ?item ?itemLabel ?itemDescription
WHERE {
VALUES (?item ?expectedType) {
(wd:Q42 wd:Q5)
(wd:Q90 UNDEF)
(wd:Q243 wd:Q6256)
}
OPTIONAL { ?item wdt:P31 ?expectedType . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Use UNDEF in VALUES tuples when a variable should not be constrained for that particular row. Here, Paris (Q90) can match any type or no type, while Douglas Adams must be human and Eiffel Tower must be a country (which won't match, demonstrating type checking).
Key Entities Used in Examples
| Entity | ID | Description |
|---|---|---|
| France | wd:Q142 | Country in Western Europe |
| Germany | wd:Q183 | Country in Central Europe |
| Italy | wd:Q38 | Country in Southern Europe |
| Spain | wd:Q29 | Country in Southwestern Europe |
| United Kingdom | wd:Q145 | Country in Northwestern Europe |
| writer | wd:Q36180 | Person who uses written words |
| poet | wd:Q49757 | Person who writes poetry |
| journalist | wd:Q1930187 | Person who writes news |
| French | wd:Q150 | Romance language |
| German | wd:Q188 | West Germanic language |
| Italian | wd:Q652 | Romance language |