Using Double Optional to Select Preferred Value in SPARQL. Alexiev, V. Blog post, October, 2025.
Blog abstract bibtex A common SPARQL need is to fetch one of several possible values in some preference order. The "most natural" way to approach such a query is to try the different alternatives and select into different variables, then combine them with COALESCE. I've seen people struggle with this (and I've been guilty myself) by using complex constructs like UNION, FILTER NOT EXISTS, FILTER(!BOUND), COALESCE. It is even worse to use GROUP BY and some aggregation function like SAMPLE, MAX, MAX(LENGTH). Using these constructs complicates the query and makes it more brittle in the face of multiple values or other data problems/deformities. If you are not extra careful, it's easy to fall into the "Cartesian Explosion" trap, i.e. produce a resultset that's the Cartesian Product of multivalued variables. A simpler way is to use a sequence of OPTIONAL clauses that all select into the same variable. This is a beautiful trick that makes sense when you grok it. I'll give several examples of real-world queries that can be optimized using this "Double OPTIONAL" trick. The only "gotcha" is if the query optimizer decides to reorder OPTIONAL clauses: but I don't know of any optimizer to do that, at least if you put more complicated clauses later in the query.
@Misc{Alexiev-double-OPTIONAL-2025,
author = {Vladimir Alexiev},
title = {Using Double Optional to Select Preferred Value in SPARQL},
howpublished = {Blog post},
month = oct,
year = 2025,
url_Blog = {https://github.com/statnett/Talk2PowerSystem/wiki/Blog-Using-Double-Optional-to-Select-Preferred-Value-in-SPARQL},
keywords = {SPARQL, OPTIONAL, UNION, NOT EXISTS, BOUND, COALESCE, preference order, SELECT, Wikidata, Lang String, DCAT, Talk2PowerSystem chatbot, Talk2PowerSystem components, Electrical CIM, CimSparql},
abstract = {A common SPARQL need is to fetch one of several possible values in some preference order. The "most natural" way to approach such a query is to try the different alternatives and select into different variables, then combine them with COALESCE. I've seen people struggle with this (and I've been guilty myself) by using complex constructs like UNION, FILTER NOT EXISTS, FILTER(!BOUND), COALESCE. It is even worse to use GROUP BY and some aggregation function like SAMPLE, MAX, MAX(LENGTH). Using these constructs complicates the query and makes it more brittle in the face of multiple values or other data problems/deformities. If you are not extra careful, it's easy to fall into the "Cartesian Explosion" trap, i.e. produce a resultset that's the Cartesian Product of multivalued variables. A simpler way is to use a sequence of OPTIONAL clauses that all select into the same variable. This is a beautiful trick that makes sense when you grok it. I'll give several examples of real-world queries that can be optimized using this "Double OPTIONAL" trick. The only "gotcha" is if the query optimizer decides to reorder OPTIONAL clauses: but I don't know of any optimizer to do that, at least if you put more complicated clauses later in the query.},
}
Downloads: 0
{"_id":"txSCSSw4TLcWbBBWn","bibbaseid":"alexiev-usingdoubleoptionaltoselectpreferredvalueinsparql-2025","author_short":["Alexiev, V."],"bibdata":{"bibtype":"misc","type":"misc","author":[{"firstnames":["Vladimir"],"propositions":[],"lastnames":["Alexiev"],"suffixes":[]}],"title":"Using Double Optional to Select Preferred Value in SPARQL","howpublished":"Blog post","month":"October","year":"2025","url_blog":"https://github.com/statnett/Talk2PowerSystem/wiki/Blog-Using-Double-Optional-to-Select-Preferred-Value-in-SPARQL","keywords":"SPARQL, OPTIONAL, UNION, NOT EXISTS, BOUND, COALESCE, preference order, SELECT, Wikidata, Lang String, DCAT, Talk2PowerSystem chatbot, Talk2PowerSystem components, Electrical CIM, CimSparql","abstract":"A common SPARQL need is to fetch one of several possible values in some preference order. The \"most natural\" way to approach such a query is to try the different alternatives and select into different variables, then combine them with COALESCE. I've seen people struggle with this (and I've been guilty myself) by using complex constructs like UNION, FILTER NOT EXISTS, FILTER(!BOUND), COALESCE. It is even worse to use GROUP BY and some aggregation function like SAMPLE, MAX, MAX(LENGTH). Using these constructs complicates the query and makes it more brittle in the face of multiple values or other data problems/deformities. If you are not extra careful, it's easy to fall into the \"Cartesian Explosion\" trap, i.e. produce a resultset that's the Cartesian Product of multivalued variables. A simpler way is to use a sequence of OPTIONAL clauses that all select into the same variable. This is a beautiful trick that makes sense when you grok it. I'll give several examples of real-world queries that can be optimized using this \"Double OPTIONAL\" trick. The only \"gotcha\" is if the query optimizer decides to reorder OPTIONAL clauses: but I don't know of any optimizer to do that, at least if you put more complicated clauses later in the query.","bibtex":"@Misc{Alexiev-double-OPTIONAL-2025,\n author = {Vladimir Alexiev},\n title = {Using Double Optional to Select Preferred Value in SPARQL},\n howpublished = {Blog post},\n month = oct,\n year = 2025,\n url_Blog = {https://github.com/statnett/Talk2PowerSystem/wiki/Blog-Using-Double-Optional-to-Select-Preferred-Value-in-SPARQL},\n keywords = {SPARQL, OPTIONAL, UNION, NOT EXISTS, BOUND, COALESCE, preference order, SELECT, Wikidata, Lang String, DCAT, Talk2PowerSystem chatbot, Talk2PowerSystem components, Electrical CIM, CimSparql},\n abstract = {A common SPARQL need is to fetch one of several possible values in some preference order. The \"most natural\" way to approach such a query is to try the different alternatives and select into different variables, then combine them with COALESCE. I've seen people struggle with this (and I've been guilty myself) by using complex constructs like UNION, FILTER NOT EXISTS, FILTER(!BOUND), COALESCE. It is even worse to use GROUP BY and some aggregation function like SAMPLE, MAX, MAX(LENGTH). Using these constructs complicates the query and makes it more brittle in the face of multiple values or other data problems/deformities. If you are not extra careful, it's easy to fall into the \"Cartesian Explosion\" trap, i.e. produce a resultset that's the Cartesian Product of multivalued variables. A simpler way is to use a sequence of OPTIONAL clauses that all select into the same variable. This is a beautiful trick that makes sense when you grok it. I'll give several examples of real-world queries that can be optimized using this \"Double OPTIONAL\" trick. The only \"gotcha\" is if the query optimizer decides to reorder OPTIONAL clauses: but I don't know of any optimizer to do that, at least if you put more complicated clauses later in the query.},\n}\n\n","author_short":["Alexiev, V."],"key":"Alexiev-double-OPTIONAL-2025","id":"Alexiev-double-OPTIONAL-2025","bibbaseid":"alexiev-usingdoubleoptionaltoselectpreferredvalueinsparql-2025","role":"author","urls":{" blog":"https://github.com/statnett/Talk2PowerSystem/wiki/Blog-Using-Double-Optional-to-Select-Preferred-Value-in-SPARQL"},"keyword":["SPARQL","OPTIONAL","UNION","NOT EXISTS","BOUND","COALESCE","preference order","SELECT","Wikidata","Lang String","DCAT","Talk2PowerSystem chatbot","Talk2PowerSystem components","Electrical CIM","CimSparql"],"metadata":{"authorlinks":{}}},"bibtype":"misc","biburl":"https://vladimiralexiev.github.io/my/Alexiev-bibliography.bib","dataSources":["qQ4QyF9WbfwAyRcSb"],"keywords":["sparql","optional","union","not exists","bound","coalesce","preference order","select","wikidata","lang string","dcat","talk2powersystem chatbot","talk2powersystem components","electrical cim","cimsparql"],"search_terms":["using","double","optional","select","preferred","value","sparql","alexiev"],"title":"Using Double Optional to Select Preferred Value in SPARQL","year":2025}