Something like
SELECT ?item ?type ?typeLabel WHERE {
?type (a | wdt:P279) wd:Q12280. # Item's type is subclass of bridge
?item wdt:P31 ?type.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
order by ?item
In this specific case it seems that all instances have a more specific type but may have multiple types. So you may want to `SELECT DISTINCT ?item.
Correct query
SELECT DISTINCT ?item WHERE {
{ ?type wdt:P279 wd:Q12280. # Item's type is subclass of bridge
?item wdt:P31 ?type. }
UNION
{ ?item wdt:P31 wd:Q12280.} # item is an instance of bridge
}
DISTINCT required as entities may be instances of a bridge and one of its subclasses. Logically they all should be but apparently that's not how it's done in Wikidata.
p:P31/ps:P31/(p:P279/ps:P279)*. Unfortunately, it's very slow and there is no way to optimize it. One could writep:P31/ps:P31/(p:P279|ps:P279)*instead, relying on the Wikidata data model. – Stanislav Kralin Feb 07 '19 at 10:09