User:TweetsFactsAndQueries/Queries/“has part” statements with “has part” qualifiers

Originally posted on Twitter and on Mastodon.

SELECT ?subject ?subjectLabel ?outerPartLabel ?innerPartLabel ?outerHasPartInner WHERE {
  ?subject p:P527 [
    ps:P527 ?outerPart;
    pq:P527 ?innerPart
  ].
  BIND(EXISTS { ?outerPart wdt:P527 ?innerPart. } AS ?outerHasPartInner)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?subjectLabel ?outerPartLabel ?innerPartLabel
Try it!