Match where value is NULL in SPARQL

3.8k views Asked by At

I have the following SPARQL query:

SELECT ?label ?img 
WHERE
{
  ?uri rdfs:label ?label .
  ?uri vtio:hasImage ?img .
}

With results like the following:

label | img
-------------
label | link1
labe2 | link2
…

I also want the label without ?img also matched i.e. entries where ?img is NULL i.e. I want results like the following:

label  | img
--------------
label1 | link1
label2 |
label3 | link3
…

If I use my earlier query a result for label2 will not be shown?

How do I modify my query to also include rows like this?

1

There are 1 answers

1
Jeen Broekstra On BEST ANSWER

Use OPTIONAL:

select ?label ?img where {
?uri rdfs:label ?label.
 OPTIONAL { ?uri vtio:hasImage ?img. }
}