BIND
Create computed variables from expressions. BIND assigns the result of an expression to a new variable, enabling calculations, transformations, and conditional logic.
Understanding BIND
BIND takes an expression and assigns its result to a variable. This is useful for computing values from existing data, formatting output, creating conditional values, or extracting parts of complex data types like dates or URIs.
Basic BIND Examples
SELECT ?person ?personLabel ?birthYear ?age
WHERE {
?person wdt:P31 wd:Q5 ;
wdt:P106 wd:Q937857 ;
wdt:P569 ?birth .
FILTER NOT EXISTS { ?person wdt:P570 ?death . }
BIND(YEAR(?birth) AS ?birthYear)
BIND(YEAR(NOW()) - ?birthYear AS ?age)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 15
This query finds living football players and calculates their approximate age. The first BIND extracts the year from the birth date, and the second BIND subtracts it from the current year to compute age.
SELECT ?mountain ?mountainLabel ?heightM ?heightFt
WHERE {
?mountain wdt:P31 wd:Q8502 ;
wdt:P2044 ?heightM .
FILTER(?heightM > 7000)
BIND(?heightM * 3.28084 AS ?heightFt)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?heightM)
BIND can perform arithmetic on existing values. This query converts mountain elevation from meters to feet by multiplying by the conversion factor, showing both values in the results.
Conditional BIND with IF
SELECT ?country ?countryLabel ?population ?size
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P1082 ?population .
BIND(IF(?population > 100000000, "Large",
IF(?population > 10000000, "Medium", "Small")) AS ?size)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?population)
LIMIT 30
The IF function enables conditional logic in BIND. This query classifies countries as "Large", "Medium", or "Small" based on population thresholds. Nested IF statements create multiple categories.
String Operations with BIND
SELECT ?person ?personLabel ?wikipediaUrl
WHERE {
?person wdt:P31 wd:Q5 ;
wdt:P106 wd:Q1028181 .
?article schema:about ?person ;
schema:isPartOf <https://en.wikipedia.org/> ;
schema:name ?title .
BIND(CONCAT("https://en.wikipedia.org/wiki/", ENCODE_FOR_URI(?title)) AS ?wikipediaUrl)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 10
BIND can use string functions like CONCAT to build new values from existing data. This query constructs Wikipedia article URLs by combining the base URL with the URL-encoded article title.
Key Entities Used in Examples
| Entity | ID | Description |
|---|---|---|
| human | wd:Q5 | Any human being |
| association football player | wd:Q937857 | Person who plays football |
| mountain | wd:Q8502 | Large landform rising above terrain |
| country | wd:Q6256 | Sovereign state |
| painter | wd:Q1028181 | Artist who paints |
| date of birth | wdt:P569 | Date when person was born |
| elevation | wdt:P2044 | Height above sea level |
| population | wdt:P1082 | Number of inhabitants |