Search code examples
sparqlwikidata

Query Wikidata to get all properties that are subclasses of entity (all types of images associated with an entity)


My goal is to get all images related to a wikidata entity. I can get the image property (of e.g. the statue of liberty) with the following query:

SELECT ?image WHERE
{
    wd:Q9202 wdt:P18 ?image.
}

This gets me the associated image but there are many types of image properties (P14, P15, P18, P41,...). They are all types of "Wikidata property for linking to a representative image (Q26940804)".

Is there an elegant way to get all properties that are subclasses of Q26940804 (getting all images)?


Solution

  • Truthy predicates, which start with wdt:P, are connected to wd:Q26940804 not directly, but through their prototypic properties, which start with wd:P.

    SELECT ?predicate ?propertyLabel ?image WHERE
    {
        wd:Q9202 ?predicate ?image .
        ?property wikibase:directClaim ?predicate .
        ?property wdt:P31 wd:Q26940804
        SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    

    Try it!

    More info:

    Also, those wd:P18 etc. are instances of wd:Q26940804, not subclasses.