2

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.

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

logi-kal
  • 7,107
  • 6
  • 31
  • 43
Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101

1 Answers1

2

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". }
}