Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return null results in SPARQL?

I’m using the Wikidata SPARQL Query Service with the following query:

SELECT ?item ?itemLabel ?class ?classLabel ?projectLabel WHERE {
  VALUES ?item { wd:Q1 wd:Q367204 }
  ?item wdt:P31 ?class;
        wdt:P18 ?project.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en,en". } }

link to the query

When running the query there is a result for Q1 because there is an image, but there is no result for Q367204 because there isn’t an image.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My question: How can I get results for both Q1 and Q367204 regardless if there is an image available or not?

>Solution :

wd:Q367204 is missing from the results because "there isn’t an image", but also because there isn’t an "instance of" statement (P31). Therefore, you can get results for both instances by wrapping both of these in an OPTIONAL block, with just ?item and ?itemLabel for Q367204, and all variables for Q1:

SELECT ?item ?itemLabel ?class ?classLabel ?projectLabel WHERE {
  VALUES ?item { wd:Q1 wd:Q367204 }
  OPTIONAL {
    ?item wdt:P31 ?class;
      wdt:P18 ?project.
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en,en". }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading