User:TweetsFactsAndQueries/Queries/most common class combinations of items linked via “different from” statements

Originally posted on Twitter and on Mastodon.

# most common class combinations of items linked via “different from” statements
SELECT ?class1 ?class2 ?class1Label ?class2Label ?count WITH {
  SELECT ?class1 ?class2 (COUNT(*) AS ?count) WHERE {
    ?item1 wdt:P1889 ?item2.
    OPTIONAL { ?item1 wdt:P31 ?class1. }
    OPTIONAL { ?item2 wdt:P31 ?class2. }
    FILTER(!BOUND(?class1) || !BOUND(?class2) || STR(?class1) <= STR(?class2))
  }
  GROUP BY ?class1 ?class2
  ORDER BY DESC(?count)
  LIMIT 50
} AS %results WHERE {
  INCLUDE %results.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY DESC(?count)
Try it!