2

I made a derivation of the cats with images example where I query for a specific cat, in particular its label and a possible image (in the web interface):

SELECT ?item ?itemLabel ?pic
WHERE {
    FILTER ( ?item = wd:Q17000472 ) .
    OPTIONAL { ?item wdt:P18 ?pic . }
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}

I do not care whether it's a cat as I just want to query basic info of any entity. Including such constraint (?item wdt:P31 wd:Q146 .) would actually give a result, but again the entity could just be anything.

Am I deducing correctly that a WHERE statement in SPARQL expects at least one 'unoptional' triple? What would be a good pattern to query optional properties of a specific entity without any knowledge about it?

1

First, use VALUES instead of FILTER to provide inline data, even if query optimizer is as smart as in Wikidata's Blazegraph:

SELECT ?item ?itemLabel ?pic WHERE {
    VALUES (?item) { (wd:Q17000472) }
    OPTIONAL { ?item wdt:P18 ?pic }
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

Try it!

Am I deducing correctly that a WHERE statement in SPARQL expects at least one 'unoptional' triple?

Rather, you shouldn't start with OPTIONAL. Evaluation starts from the so-called universal mapping, if the OPTIONAL block is not able to retrieve any results this step simply retains the universal mapping.

See also In SPARQL, Order Matters, example Q1b. DBpedia's Virtuoso also follows this convention.

Fortunately, VALUES decide this problem, at least on Wikidata's Blazegraph.

If you are still looking for properties that all Q-entities possess, then check wikibase:statements, wikibase:sitelinks, wikibase:identifiers, schema:version and schema:dateModified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.