Federation in the #Wikimedia #Commons Query Service is impressively powerful: You can use the wealth of information contained in #Wikidata, combine it with structured metadata on Commons and retrieve media from Commons you'd hardly find otherwise.
For example:
- Find images for all monuments with a Bavarian monument authority ID in a certain administrative district (here Passau): https://w.wiki/9dba
- images of churches in Rome: https://w.wiki/9dbf
The query structure is rather straightforward and looks more or less like this:
SELECT DISTINCT * WITH {
SELECT DISTINCT * {
SERVICE <https://query.wikidata.org/sparql> {
WIKIDATA QUERY yielding a ?variableFromWikidataQuery
}
}} AS %results WHERE {
INCLUDE %results .
?image wdt:P180 ?variableFromWikidataQuery .
}