Commons:SPARQL query service/queries/examples

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
This page contains changes which are not marked for translation.

This page is parsed by the web interface of the query service to fill the query example dialog. Many of the examples also work in template:Wikidata list template calls parsed by the Listerbot, which however requires the ?item field variable to be selected.


Simple Queries[edit]

These basic queries help to understand SPARQL and the Wikibase RDF format.

Depictions of Douglas Adams[edit]

#shows M-entities that depict Douglas Adams
SELECT ?file WHERE {
  ?file wdt:P180 wd:Q42 .
}

Try it!

Depictions of Douglas Adams shown as image grid[edit]

The ImageGrid display mode looks for file-urls of the form used as values by Wikidata properties such as image (P18) to identify files that can be displayed.

At present the query database does not yet contain triples that relate M-entities to urls in this form; so this workaround constructs such urls, which then enable the ImageGrid mode to work if they are output.

According to mw:Wikibase/Indexing/RDF_Dump_Format#MediaInfo, such connections might some time get built in as ?image schema:about ?file

#workaround to show the images in an image grid
#defaultView:ImageGrid
SELECT ?file ?image WHERE {
  ?file wdt:P180 wd:Q42 .
  ?file schema:contentUrl ?url .
  bind(iri(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(substr(str(?url),53)))) AS ?image)
}

Try it!

Digital Representations of "David" by Michelangelo[edit]

Files with digital representation of (P6243) set to David (Q179900)

# Digital depictions of "David" by Michelangelo
#defaultView:ImageGrid
SELECT ?file ?image WHERE {
  ?file wdt:P6243 wd:Q179900 . 
  ?file schema:contentUrl ?url .
  # workaround to show the images in an image grid
  bind(iri(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(substr(str(?url),53)))) AS ?image)
}

Try it!

Number of files with "Wikimedia OTRS ticket number" statement[edit]

Number of files with Wikimedia OTRS ticket number (P6305 ) statements

SELECT (COUNT(?file) AS ?count)  {
  ?file wdt:P6305 ?value .
}

Try it!

Files with multiple "Digital Representations of" statements[edit]

digital representation of (P6243) statements has "single value constraint", or in other words each file can be a digital representation of only one Wikidata item, (use picture (P180) if there are more objects in the image). The query below finds constraint violations.

SELECT ?file (COUNT(?value) AS ?count)  {
  ?file wdt:P6243 ?value .
} 
GROUP BY ?file 
HAVING ( ?count > 1 ) 
ORDER BY DESC(?count)
LIMIT 100

Try it!

Image grid showing images which are exactly 100×100 px[edit]

#defaultView:ImageGrid
select ?file ?image {
  ?file schema:contentUrl ?url ;
        schema:height 100 ;
        schema:width  100 .
  # workaround to show the images in an image grid
  bind(iri(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(substr(str(?url),53)))) AS ?image)
} limit 2000

Try it!

Number of files by file type[edit]

select ?encoding (count(*) as ?total) {
  ?file schema:encodingFormat ?encoding .
} 
group by ?encoding 
order by desc(?total)

Try it!

Scatter chart showing height/width of a sample of 10,000 images[edit]

#defaultView:ScatterChart
select * {
  ?file schema:height ?h ;
        schema:width ?w .
} limit 10000

Try it!

Map of some images tagged with "depicts" (P180) = "bridge" (Q12280)[edit]

# Map of some images tagged with "depicts" (P180) = "bridge" (Q12280)
#defaultView:Map
SELECT ?file ?pov_coords ?image WHERE {
  ?file wdt:P180 wd:Q12280 ;
        wdt:P1259 ?pov_coords ;
        schema:contentUrl ?url ;
  bind(iri(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(substr(str(?url),53)))) AS ?image)
}

Try it!

Map of images found in the medieval Atlas De Wit[edit]

#defaultView:Map
SELECT ?file ?image ?depictionLabel ?coord WHERE {
  ?file wdt:P6243 wd:Q2520345 .
  ?file wdt:P180 ?depiction .
  ?file schema:contentUrl ?url .
  SERVICE <https://query.wikidata.org/sparql> {
    ?depiction wdt:P625 ?coord.
    SERVICE wikibase:label {
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
        ?depiction rdfs:label ?depictionLabel .
    }
  }
  bind(iri(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", replace(substr(str(?url),53),"_","%20"))) AS ?image)
}

Try it!

Uses of federation[edit]

Looking up labels and descriptions of Wikidata items[edit]

Most common values for "source of image" (P7482)[edit]

  • This query uses federation to find Wikidata labels for Wikidata items discovered in the first part
# Most common values for "source of image" (P7482)
SELECT ?count ?source ?source_label

WITH { 
  SELECT (COUNT(DISTINCT(?file)) AS ?count) ?source WHERE {
     ?file wdt:P7482 ?source . 
  } GROUP BY ?source
} AS %sources
 
WHERE {
  INCLUDE %sources .
  service <https://query.wikidata.org/sparql> {
    OPTIONAL {?source rdfs:label ?source_label FILTER (lang(?source_label) = 'en') } .
  }
}
ORDER BY DESC(?count) ?source_label

Try it!

Most common "expression, gesture or body pose" (P6022) qualifier values for "depicts" (P180)[edit]

# Most common "expression, gesture or body pose" (P6022) qualifier values for "depicts" (P180)
SELECT ?count ?value ?value_label ?example

WITH { 
  SELECT (COUNT(DISTINCT(?file)) AS ?count) ?value (SAMPLE(?file) AS ?example) WHERE {
     ?file p:P180/pq:P6022 ?value . 
  } GROUP BY ?value
  ORDER BY DESC(?count)
  LIMIT 2000           
} AS %values
 
WHERE {
  INCLUDE %values .
  service <https://query.wikidata.org/sparql> {
    OPTIONAL {?value rdfs:label ?value_label FILTER (lang(?value_label) = 'en') } .
  }
}
ORDER BY DESC(?count) ?value_label

Try it!

  • restricted to humans: tinyurl.com/y53kzn83 ; non-humans: tinyurl.com/y35b5w3l

colors of roses[edit]

  • This uses federation to WDQS in order to get the labels of the color items the roses depicted on the images have.
#defaultView:ImageGrid

prefix commons: <http://commons.wikimedia.org/wiki/Special:FilePath/>

select ?colorName ?image with {
  select ?color (iri(replace(str(sample(?photo)), "^.*/", str(commons:))) as ?image) where {
    [a schema:ImageObject] schema:contentUrl ?photo;
                           p:P180 [
                             ps:P180 wd:Q102231;
                             pq:P462 ?color
                           ].
  }
  group by ?color
} as %roses where {
  include %roses.

  service <https://query.wikidata.org/sparql> {
    service wikibase:label {
      bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
      ?color rdfs:label ?colorName.
    }
  }
}

Try it!


Retrieving a set of Wikidata items of interest[edit]

All images depicting Van Gogh artworks[edit]

  • Using federation to find all Wikidata items that have 'creator' (P170) as Vincent van Gogh (Q5582), then finding all media items that depict those paintings, using a hack to display the images in a grid.
#defaultView:ImageGrid
select ?image ?painting ?paintingLabel ?paintingDescription ?thumb with {
  select * {
    service <https://query.wikidata.org/sparql> {
      ?painting wdt:P170 wd:Q5582;
                rdfs:label ?paintingLabel;
                schema:description ?paintingDescription.
      filter (lang(?paintingLabel) = "en")
      filter (lang(?paintingDescription) = "en")
    }
  } 
} as %paintings where {
  include %paintings. 
  ?image wdt:P180 ?painting.
  
  # This is a hack to make ImageGrid work
  ?image schema:contentUrl ?url. 
  bind(iri(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(substr(str(?url),53)))) AS ?thumb)
}

Try it!

Detect depiction of both a specific church and the generic "church building"[edit]

  • Detecting redundancy of a "depicted" concept with something more specific based on Wikidata. Federation is used to retrieve all items that are instances of "church building".
#500 images that have both a specific church building and redundantly depict "church building" (Q16970)   
select ?item ?church ?churchLabel ?churchDescription ?thumb with {
  select * {
    service <https://query.wikidata.org/sparql> {
      ?church wdt:P31 wd:Q16970;
                rdfs:label ?churchLabel;
                schema:description ?churchDescription.
      filter (lang(?churchLabel) = "en")
      filter (lang(?churchDescription) = "en")
    }
  } 
} as %church where {
  include %church. 
  ?item wdt:P180 ?church.
  ?item wdt:P180 wd:Q16970.
}

LIMIT 500

Try it!


Illustration published in German magazine 'Die Gartenlaube' using Wikidata federation[edit]

  • This uses federation to find articles published in (P1433) the German magazine 'Die Gartenlaube', together with some information about those articles;
    then, having retrieved that set, finds Commons files with the Commons statements that they were published in (P1433) those articles
#defaultView:ImageGrid{"hide":["?img2"]}
select ?articleLabel ?articleDesc ?article ?img2 ?imgDesc
with {
  select * {
    # SELECT all articles published in German magazine 'Die Gartenlaube'
    service <https://query.wikidata.org/sparql> {
      ?article wdt:P1433 wd:Q655617;
               rdfs:label ?articleLabel;
               schema:description ?articleDesc.
      FILTER (LANG(?articleLabel) = "en")
      FILTER (LANG(?articleDesc) = "en")
    }
  }
} as %items
where {
  include %items .
  #select images published in any article.
  ?file wdt:P1433 ?article; 
    schema:contentUrl ?img ;
    optional {?file rdfs:label ?imgDesc. FILTER(LANG(?desc) = "de") } .
  # workaround to show the images in an image grid
  BIND(IRI(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(substr(str(?img),53)))) AS ?img2)
}

Try it!

Find depicted artworks authors and copyright status[edit]

select ?image ?item ?itemLabel ?itemDescription ?thumb (?p170Label as ?author) (YEAR(?p570) as ?death) ?copyright_statusLabel with {
  select * {
    service <https://query.wikidata.org/sparql> {
      ?item wdt:P131* wd:Q1757 .            
      ?item wdt:P170 ?p170 .
      OPTIONAL { ?item rdfs:label ?itemLabel filter (lang(?itemLabel) = "en") }
      OPTIONAL { ?item schema:description ?itemDescription filter (lang(?itemDescription) = "en") }    
      OPTIONAL { ?p170 rdfs:label ?p170Label filter (lang(?p170Label) = "en") } 
      OPTIONAL { ?p170 wdt:P570 ?p570 } .
      OPTIONAL { ?p170 wdt:P7763 ?copyright_status. 
                 ?copyright_status rdfs:label ?copyright_statusLabel
                 filter (lang(?copyright_statusLabel) = "en")
               }
    }
  }
} as %works where {
  include %works.
  ?image wdt:P180 ?item.
  ?image schema:contentUrl ?url. 
  bind(iri(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(substr(str(?url),53)))) AS ?thumb)
}

Try it!

Exploring Commons Categories[edit]

Service wikibase:mwapi provides ability to combine SPARQL queries with Commons categories. Unfortunately only 10,000 files can be returned by that service, however that number can be extended as we can control which 10,00 by adding additional commands like

   bd:serviceParam mwapi:gcmsort "timestamp" .
   bd:serviceParam mwapi:gcmdir "descending" .

Wikidata items of files in Category:Artworks with structured data with redirected P6243 property[edit]

Maintenance category Category:Artworks with structured data with redirected P6243 property collects files with digital representation of (P6243) statement where Wikidata item linked by that statement is a redirect to a different Wikidata item. The following query will show the filenames, the items linked by P6243 property and the items they redirect to. Be aware that the category is often empty, and even if it is not the redirects might have been already corrected.

SELECT ?file ?title ?redirect_from ?redirect_to
WITH
{
  SELECT ?file ?title
  WHERE
  {
    SERVICE wikibase:mwapi
    {
      bd:serviceParam wikibase:api "Generator" .
      bd:serviceParam wikibase:endpoint "commons.wikimedia.org" .
      bd:serviceParam mwapi:gcmtitle "Category:Artworks with structured data with redirected P6243 property" .
      bd:serviceParam mwapi:generator "categorymembers" .
      bd:serviceParam mwapi:gcmtype "file" .
      bd:serviceParam mwapi:gcmlimit "max" .
      ?title wikibase:apiOutput mwapi:title .
      ?pageid wikibase:apiOutput "@pageid" .
    }
    BIND (URI(CONCAT('https://commons.wikimedia.org/entity/M', ?pageid)) AS ?file)
  }
} AS %get_files
WHERE
{
  INCLUDE %get_files
  ?file wdt:P6243 ?redirect_from .
  SERVICE <https://query.wikidata.org/sparql> {
      ?redirect_from owl:sameAs ?redirect_to.
  }
}

Try it!

More Queries[edit]

Some queries investigating how particular properties are being used

Objects with the largest number of Digital Representations (P6243)[edit]

Wikidata items with largest number of files on Commons linking to them through digital representation of (P6243) statement.

SELECT ?count ?item ?item_label ?commonscat_link

WITH {
  SELECT (COUNT(DISTINCT(?file)) AS ?count) ?item WHERE {
     ?file wdt:P6243 ?item . 
  } GROUP BY ?item
  ORDER BY DESC(?count)
  LIMIT 200
} AS %items

WHERE {
  INCLUDE %items . 
  service <https://query.wikidata.org/sparql> {
    OPTIONAL {?item rdfs:label ?item_label FILTER (lang(?item_label) = 'en') } .
    OPTIONAL {
      ?item wdt:P373 ?commonscat .
      BIND(IRI(CONCAT("https://commons.wikimedia.org/wiki/Category:", ?commonscat)) AS ?commonscat_link) .
    }
  }
} ORDER BY DESC(?count) ?item_label

Try it!

Count of number of Digital Representations (P6243) by type of object represented[edit]

Note -- limited to a sample of 50,000 files, although we (currently) have 225124 files with digital representation of (P6243) statements, as 50,000 seems to be as large a number as can be sent by federation to WDQS for analysis without timeout

SELECT (COUNT(DISTINCT(?file)) AS ?count) ?class ?class_label

WITH {
  SELECT ?file ?item WHERE {
     ?file wdt:P6243 ?item . 
  } LIMIT 50000
} AS %files

WHERE {
  INCLUDE %files . 
  service <https://query.wikidata.org/sparql> {
    ?item wdt:P31 ?class .
    OPTIONAL {?class rdfs:label ?class_label FILTER (lang(?class_label) = 'en') } .
  }
} GROUP BY ?class ?class_label
ORDER BY DESC(?count) ?class_label

Try it!

  • More efficient version: tinyurl.com/y2dtkz9l

Most frequent qualifiers when "source of image" (P7482) = "file available on the internet" (Q74228490)[edit]

# # Most frequent qualifier properties when "source of image" (P7482) = "file available on the internet" (Q74228490) 
#  limited to first 1,000,000 such images
SELECT ?count ?qualifier ?qualifier_label ?example ?example_value ?example_value_label

WITH {
  SELECT ?file ?source_stmt WHERE {
      ?file p:P7482 ?source_stmt . 
      ?source_stmt ps:P7482 wd:Q74228490 .
    } LIMIT 1000000
  }  AS %files

WITH {
  SELECT (COUNT(DISTINCT(?file)) AS ?count) ?qualifier (SAMPLE(?file) AS ?example) (SAMPLE(?value) AS ?example_value) WHERE {
     INCLUDE %files .
     ?file p:P7482 ?source_stmt . 
     ?source_stmt ?qualifier ?value .
  } GROUP BY ?qualifier
  ORDER BY DESC(?count)
  LIMIT 200                      
} AS %source_info
 
WHERE {
  INCLUDE %source_info .
  service <https://query.wikidata.org/sparql> {
    ?qualifier ^wikibase:qualifier ?qual_item . 
    OPTIONAL {?qual_item rdfs:label ?qualifier_label FILTER (lang(?qualifier_label) = 'en') } .
    OPTIONAL {?example_value rdfs:label ?example_value_label FILTER (lang(?example_value_label) = 'en') } .
  }
}
ORDER BY DESC(?count)

Try it!

Most frequent providers of images when "source of image" (P7482) = "file available on the internet" (Q74228490)[edit]

# Most frequent providers of images when "source of image" (P7482) = "file available on the internet" (Q74228490) 
SELECT ?count ?provider ?provider_label ?example 

WITH {
  SELECT (COUNT(DISTINCT(?file)) AS ?count) ?provider (SAMPLE(?file) AS ?example) WHERE {
     ?file p:P7482 ?source_stmt . 
     ?source_stmt ps:P7482 wd:Q74228490 .
     ?source_stmt pq:P137 ?provider .
  } GROUP BY ?provider
  ORDER BY DESC(?count)
  LIMIT 200                      
} AS %source_info
 
WHERE {
  INCLUDE %source_info .
  service <https://query.wikidata.org/sparql> {
    OPTIONAL {?provider rdfs:label ?provider_label FILTER (lang(?provider_label) = 'en') } .
  }
}
ORDER BY DESC(?count)

Try it!

Images with largest number of "depicts" (P180) values, but no preferred value(s)[edit]

(from a sample of 500,000 images)

# Images with largest number of depicts values

#defaultView:ImageGrid
SELECT ?count ?image (GROUP_CONCAT(?topicLabel; separator = ' / ') AS ?topicLabels)

WITH {
  SELECT ?file WHERE {
    ?file a wikibase:Mediainfo .
  } LIMIT 500000
} AS %files

WITH {
  SELECT ?file ?topic WHERE {
    INCLUDE %files .
    MINUS {?file p:P180/wikibase:rank wikibase:PreferredRank} .
    ?file wdt:P180 ?topic .
  }
} AS %file_topics 

WITH {
  SELECT (COUNT(DISTINCT(?topic)) AS ?count) ?file WHERE {
    INCLUDE %file_topics .
  } GROUP BY ?file
  ORDER BY DESC(?count)
  LIMIT 200
} AS %top_files

WITH {
  SELECT ?count ?file ?topic WHERE {
     INCLUDE %file_topics .
     INCLUDE %top_files .
  }
} AS %top_file_topics

WITH {
  SELECT DISTINCT ?topic WHERE {
      INCLUDE %top_file_topics .
  }
} AS %top_topics

WITH{
  SELECT ?topic ?topicLabel WHERE {
    INCLUDE %top_topics .
    service <https://query.wikidata.org/sparql> {
       service wikibase:label {
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
        ?topic rdfs:label ?topicLabel.
      }
    }
  }
} AS %topicLabels

WHERE {
   INCLUDE %top_file_topics .
   INCLUDE %topicLabels .
   ?file schema:contentUrl ?url .
   bind(iri(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(substr(str(?url),53)))) AS ?image)
} GROUP BY ?count ?file ?image 
ORDER BY DESC(?count)

Try it!

Files with duplicate "depicts" (P180) statements[edit]

Find all the files which have the same value added twice to the same property. In this example we used picture (P180) statement, but it can be used with others as well. This query sometimes times out, but removing "DISTINCT" from it fixes the problem, but returns multiple rows per file

SELECT DISTINCT ?file {
  ?file p:P180 ?statement1.
  ?file p:P180 ?statement2.
  ?statement1 ps:P180 ?value .
  ?statement2 ps:P180 ?value .
  FILTER(?statement1 != ?statement2)
}
limit 50

Try it!

Things depicted with cats[edit]

# Things depicted with cats

#defaultView:ImageGrid
SELECT ?count ?depicted ?depictedLabel ?sampleImg

WITH {
  SELECT ?target WHERE {
    SERVICE <https://query.wikidata.org/sparql> {   
        { ?target wdt:P31?/wdt:P279* wd:Q146 }     ## include specific sorts of cats and individual cats
        UNION
        { ?target wdt:P31? wd:Q43577 }              ## and also breeds of cat
    }
  }
} AS %targets
    
WITH {
  SELECT (COUNT(DISTINCT(?file)) AS ?count) ?depicted (SAMPLE(?file) AS ?sample) WHERE {
    INCLUDE %targets .
    ?file p:P180 ?stmt .
    ?stmt ps:P180 ?target .
 #   ?stmt wikibase:rank wikibase:PreferredRank .       ##  Make sure the cat is marked as prominent -- **commented out for now**
    
    ?file p:P180 ?stmt2.  FILTER (?stmt2 != ?stmt) .    ##  now get another depicts statement
    ?stmt2 ps:P180 ?depicted . 
  } GROUP BY ?depicted
} AS %things 

WITH {
  SELECT ?count ?depicted ?sample WHERE {
    INCLUDE %things .
    MINUS {                                           ## exclude anything that was an original target
      SELECT (?target AS ?depicted) WHERE {
        INCLUDE %targets .
      }
    }
  }
} AS %things1

WHERE {
  INCLUDE %things1 .
  SERVICE <https://query.wikidata.org/sparql> {
    SERVICE wikibase:label {
      bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
      ?depicted rdfs:label ?depictedLabel .
    }
  }
  ?sample schema:contentUrl ?url ;
  bind(iri(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(substr(str(?url),53)))) AS ?sampleImg)
} ORDER BY DESC(?count)

Try it!

  • with a more random set of sampled images: tinyurl.com/y35wjrz8 (choice will be different each time the query is modified, or if it is no longer in cache)

Frequently used properties on Commons[edit]

Most common predicates on Commons items that depict Douglas Adams[edit]

#most common predictates on Commons items that depict Douglas Adams
SELECT (COUNT(DISTINCT(CONCAT(str(?file),str(?value)))) AS ?count) ?prop (SAMPLE(?file) AS ?sample_file)  (SAMPLE(?value) AS ?sample_value) 

WHERE {
  ?file wdt:P180 wd:Q42 .
  ?file ?prop ?value .
} GROUP BY ?prop
ORDER BY DESC(?count)

Try it!

Most common predicates on a sample of 5,000 Commons files[edit]

  • This query uses federation to look up the properties' names on Wikidata (a task which is a little more involved for properties than for regular Wikidata items).

A series of named subqueries (i.e. the WITH blocks) are used to force the sequence of execution -- see on Wikidata for a little more about this.

#most common predictates on a sample of 5,000 Commons files
SELECT (COUNT(DISTINCT ?file) AS ?file_count) (COUNT(DISTINCT(CONCAT(str(?file),str(?val)))) AS ?stmt_count) 
   ?prop ?prop_label (SAMPLE(?file) AS ?sample_file)  (SAMPLE(?val) AS ?sample_val) 

WITH {
  SELECT ?file WHERE {
    ?file a wikibase:Mediainfo .
  } LIMIT 5000
} AS %files

WITH {
  SELECT ?file ?prop ?val WHERE {
    INCLUDE %files .
    ?file ?prop ?val .
  } 
} AS %triples

WITH {
  SELECT DISTINCT ?prop WHERE {
     INCLUDE %triples .
  }
} AS %props

WITH {
   SELECT ?prop ?prop_label WHERE {
     INCLUDE %props .
     service <https://query.wikidata.org/sparql> {
        OPTIONAL {
           ?prop ^wikibase:directClaim ?prop_item .
           ?prop_item rdfs:label ?prop_label FILTER (lang(?prop_label) = 'en') .
        }
        OPTIONAL {
           ?prop ^wikibase:claim ?prop_item .
           ?prop_item rdfs:label ?prop_label FILTER (lang(?prop_label) = 'en') .
        }
     }
  }
} AS %prop_labels
     
WHERE {
  INCLUDE %triples .
  INCLUDE %prop_labels .
} GROUP BY ?prop ?prop_label
ORDER BY DESC(?stmt_count)

Try it!

  • A later 5000 : tinyurl.com/yyuywxqm

Most common qualifiers used on "depicts" statements[edit]

# Most common qualifiers used on "depicts" statements
SELECT DISTINCT ?count ?qual ?qual_label ?sample_file ('depicts' AS ?prop) ?sample_stmt_value ?sample_stmt_valueLabel (?qual_label AS ?qualLabel1) ?sample_value ?sample_valueLabel

WITH {
  SELECT ?qual  WHERE {
    service <https://query.wikidata.org/sparql> {
      ?prop wikibase:qualifier ?qual .
    }
  }
} AS %quals 

WITH {
  SELECT ?stmt ?qual WHERE {
    INCLUDE %quals .
    ?stmt ?qual [] .
    ?file p:P180 ?stmt .
  } LIMIT 30000
} AS %stmts 

WITH {
  SELECT (COUNT(DISTINCT(?stmt)) AS ?count) ?qual (SAMPLE(?stmt) AS ?example_stmt) WHERE {
     INCLUDE %stmts .
  } GROUP BY ?qual 
} AS %top_quals

WITH {
  SELECT ?count ?qual (SAMPLE(?example_file) AS ?sample_file) (SAMPLE(?example_stmt_value) AS ?sample_stmt_value) (SAMPLE(?example_value) AS ?sample_value) WHERE {
    INCLUDE %top_quals .
    ?example_stmt ^p:P180 ?example_file .
    ?example_stmt ps:P180 ?example_stmt_value .
    ?example_stmt ?qual ?example_value .
  } GROUP BY ?count ?qual
} AS %results
 
WHERE {
  INCLUDE %results .
  service <https://query.wikidata.org/sparql> {
    ?qual ^wikibase:qualifier ?qual_item
    service wikibase:label { 
      bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
      ?qual_item rdfs:label ?qual_label.
      ?sample_stmt_value rdfs:label ?sample_stmt_valueLabel .
      ?sample_value rdfs:label ?sample_valueLabel .
    }
  } 
}  ORDER BY DESC(?count)

Try it!

Most common property + qualifier combinations, other than on "depicts" (P180) statements[edit]

# most common property + qualifier combinations, other than on "depicts" (P180) statements
SELECT ?count ?pred ?pred_label ?qual ?qual_label ?example

WITH {
  SELECT ?qual  WHERE {
    service <https://query.wikidata.org/sparql> {
      ?prop wikibase:qualifier ?qual .
    }
  }
} AS %quals 

WITH {
  SELECT ?file ?stmt ?qual ?pred WHERE {
    INCLUDE %quals .
    ?stmt ?qual [] .
    OPTIONAL {?f1 p:P180 ?stmt} FILTER(!bound(?f1)) .
    ?file ?pred ?stmt .
  } LIMIT 100000
} AS %stmts 

WITH {
  SELECT (COUNT(DISTINCT(?stmt)) AS ?count) ?qual ?pred (SAMPLE(?file) AS ?example) WHERE {
     INCLUDE %stmts .
  } GROUP BY ?qual ?pred 
} AS %top_quals

WHERE {
  INCLUDE %top_quals .
  service <https://query.wikidata.org/sparql> {
        OPTIONAL {
           ?pred ^wikibase:claim ?pred_item .
           ?pred_item rdfs:label ?pred_label FILTER (lang(?pred_label) = 'en') .
        }
        OPTIONAL {
           ?qual ^wikibase:qualifier ?qual_item .
           ?qual_item rdfs:label ?qual_label FILTER (lang(?qual_label) = 'en') .
        }
  }
}  ORDER BY DESC(?count)

Try it!