Commons:SPARQL query service/queries/examples

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Translate this page; This page contains changes which are not marked for translation.
Other languages:

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.

The statement to retrieve an image is ?file schema:url ?image.

#defaultView:ImageGrid
select ?file ?image where {
  ?file wdt:P180 wd:Q42.
  ?file schema:url ?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
# Note how you can use a semicolon to group triples that have the same subject.
#defaultView:ImageGrid
select ?file ?image where {
  ?file wdt:P6243 wd:Q179900;
        schema:url ?image.
}

Try it!

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

Number of files with Wikimedia VRTS 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 depicts (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;
        schema:url ?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]

# Show a map of images depicting bridges AND having a coordinate for their point of view
#defaultView:Map
select ?file ?pov_coords ?image where {
  ?file wdt:P180 wd:Q12280;
        wdt:P1259 ?pov_coords;
        schema:url ?image.
}

Try it!

Map of images found in the 17th c. Atlas De Wit[edit]

#defaultView:Map
select ?file ?image ?depictionLabel ?coord where {
  ?file wdt:P6243 wd:Q2520345 .
  ?file wdt:P180 ?depiction .
  ?file schema:url ?image .
  service <https://query.wikidata.org/sparql> {
    ?depiction wdt:P625 ?coord.
    service wikibase:label {
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
        ?depiction rdfs:label ?depictionLabel .
    }
  }
}

Try it!

Captions of files depicting roses[edit]

#Captions of files depicting roses
SELECT ?file ?fileLabel WHERE {
  ?file wdt:P180 wd:Q102231 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

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
WHERE {
  { 
    SELECT (COUNT(DISTINCT(?file)) AS ?count) ?source WHERE {
      ?file wdt:P7482 ?source . 
    } GROUP BY ?source
  } 
  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]

subclasses of roses[edit]

  • This uses federation to WDQS in order to get the items and labels of the subclasses of roses
# Show all files depicting roses, and subclasses thereof
#defaultView:ImageGrid
select distinct ?item ?itemLabel ?image with {
  select ?item ?itemLabel where {
    service <https://query.wikidata.org/sparql> {
      ?item wdt:P31/wdt:P279* wd:Q34687. # subclasses of roses
      service wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". ?item rdfs:label ?itemLabel . }
    }
  }
} as %wikidataItems where {
  include %wikidataItems .
  ?file wdt:P180 ?item;
        schema:url ?image.
} limit 1000

Try it!

All images depicting Van Gogh artworks[edit]

  • Using federation to find all Wikidata items that have 'creator' (P170) as Vincent van Gogh (Q5582), then find all media items that depict those paintings and show 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;
         schema:url ?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"]}
#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:url ?img2.
  
  optional {?file rdfs:label ?imgDesc. filter(lang(?desc) = "de") } .
}

Try it!

Images of objects located in Helsinki, together with authors of those objects and copyright status[edit]

# Images of objects located in Helsinki, together with authors of those objects and copyright status
#defaultView:ImageGrid
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;
         schema:url ?thumb.
}

Try it!

Map of files participating in Wiki Loves Monuments in Sweden, color-coded by year of competition[edit]

# Map of files participating in Wiki Loves Monuments in Sweden, color-coded by year of competition
#defaultView:Map
select ?image ?wlm ?wlmLabel ?username ?coords ?thumb ?layer with {
  select * {
    service <https://query.wikidata.org/sparql> {
      ?wlm wdt:P179 wd:Q105194463;
                rdfs:label ?wlmLabel;
      filter (lang(?wlmLabel) = "en")
    }
  } 
} as %items where {
  include %items. 
  ?image wdt:P1344 ?wlm.
  OPTIONAL {?image wdt:P1259 ?coords}
  OPTIONAL {?image (p:P170/pq:P4174) ?username.}

  ?image schema:contentUrl ?url;
         schema:url ?thumb. 
  
     BIND(
          IF(?wlm = wd:Q105201214, "2011",
          IF(?wlm = wd:Q105201215, "2012",
          IF(?wlm = wd:Q105201216, "2013",
          IF(?wlm = wd:Q105201217, "2014",
          IF(?wlm = wd:Q105201218, "2015",
          IF(?wlm = wd:Q105201219, "2016",
          IF(?wlm = wd:Q105201220, "2017",
          IF(?wlm = wd:Q105201221, "2018",
          IF(?wlm = wd:Q105201222, "2019",
          IF(?wlm = wd:Q105201223, "2020",
          "")))))))))) AS ?layer).
}

Try it!

Media files depicting (former) heads of state that are still alive[edit]

# Media files depicting (former) heads of state that are still alive
#defaultView:ImageGrid
select ?file ?image ?leaderLabel with {
  select ?leader ?leaderLabel where {
    service <https://query.wikidata.org/sparql> {
      ?leader wdt:P31 wd:Q5;
            wdt:P39 ?posHeld. 
      minus { ?leader wdt:P570 []. } # Minus date of death, only living people
      ?posHeld wdt:P31/wdt:P279* wd:Q48352. # Position held = subclass or instance of 'head of state'
      service wikibase:label { 
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". 
        ?leader rdfs:label ?leaderLabel. 
      }
    }
  }
} as %wikidataItems where {
  include %wikidataItems .
  ?file wdt:P180 ?leader;
        schema:url ?image.
}

Try it!

Get information of Europeana item using federated query[edit]

#Get information of Europeana item using federated query
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX edm: <http://www.europeana.eu/schemas/edm/>
PREFIX ore: <http://www.openarchives.org/ore/terms/>

SELECT * WHERE {
  BIND(<http://data.europeana.eu/proxy/provider/91622/raa_kmb_16000200042758> as ?p854)  
  SERVICE <http://sparql.europeana.eu/> {
   {
         ?p854 <http://purl.org/dc/terms/created> ?created .
         ?p854 <http://purl.org/dc/elements/1.1/identifier> ?identifier .
         ?p854 <http://purl.org/dc/elements/1.1/publisher> ?publisher .
         ?p854 <http://purl.org/dc/elements/1.1/rights> ?rights .
         ?p854 <http://purl.org/dc/elements/1.1/title> ?title .
         ?p854 <http://purl.org/dc/elements/1.1/description> ?description .
     }
  }
}

Try it!

Read commons categories for Europeana subjects using federated query[edit]

# Find Commons category suggestions for a Commons file using subjects stored in Europeana
# 1.) Read collection and identifier for M68435429 (File:Tervahovi_Oulu_1898_02.jpg)
# 2.) Read subjects in Finnish of photo defined by identifier from Europeana
# 3.) Translate subjects to YSO ontology using Finto-service. 
# 3.b) Note: Query is routed through Sophox.org, because there is no direct access from WCQS to Finto.
# 4.) Translate YSO items to Wikidata items using Wikidata and read Commons categories

SELECT * WITH {
  SELECT ?mediafile ?collection ?identifier ?europeana ?subject WHERE {
    
     # 1.) Read collection and identifier for M68435429 (File:Tervahovi_Oulu_1898_02.jpg)
     BIND(sdc:M68435429 as ?mediafile)     
     ?mediafile p:P195 ?collection_prop .
     ?collection_prop ps:P195 ?collection .
     ?collection_prop pq:P217 ?identifier
     
     # 2.) Read subjects in Finnish of photo defined by identifier from Europeana
     SERVICE <http://sparql.europeana.eu/> {
       ?europeana <http://purl.org/dc/elements/1.1/identifier> ?identifier .
       ?europeana <http://purl.org/dc/elements/1.1/subject> ?subject .       
     }
  }
} AS %europeana
WHERE {
  INCLUDE %europeana . 
  
  # 3.) Translate subjects to YSO ontology using Finto-service. 
  # 3.b) Note: Query is routed through Sophox.org, because there is no direct access from WCQS to Finto.
  SERVICE <http://zbw.eu/beta/sparql/stw/query> {
    SERVICE <http://api.finto.fi/sparql> {
      ?yso skos:prefLabel ?subject ;
      skos:inScheme <http://www.yso.fi/onto/yso/>
    }
  } 
  # 4.) Translate YSO items to Wikidata items using Wikidata and read Commons categories
  BIND(REPLACE(STR(?yso), "http://www.yso.fi/onto/yso/p", "") as ?yso_number)
  SERVICE <https://query.wikidata.org/sparql>  {
    ?wikidata wdt:P2347 ?yso_number .
    ?wikidata wdt:P373 ?commonscat  
  }
}

Try it!


Exploring user uploads[edit]

World map of files authored by Wikimedia Commons user 'Coyau'[edit]

# World map of files authored by Wikimedia Commons user 'Coyau'
#defaultView:Map
select ?file ?image ?coordinates (?date AS ?layer) ?filename where {
  bind("Coyau" AS ?username)
  
  ?file (p:P170/pq:P4174) ?username;
        schema:url ?image;
        ?p ?coordinates. # any coordinate like wdt:P625,wdt:P1259,wdt:P9149
  
  filter(datatype(?coordinates) = geo:wktLiteral). # If this line is not here, the query will be very slow
  
  optional { ?file wdt:P571 ?date. }
  ?file schema:contentUrl ?url .
  BIND (wikibase:decodeUri(CONCAT("File:", substr(str(?url),53))) AS ?filename).
}
LIMIT 10000

Try it!

Timeline of files authored by a Wikimedia Commons user[edit]

# Timeline of files authored by a Wikimedia Commons user
#TEMPLATE={"template":"Timeline of files authored by ?username","variables":{"?username":{"query":" SELECT DISTINCT ?username WHERE { [] p:P170/pq:P4174 ?username .} LIMIT 100"} } }
#defaultView:Timeline
SELECT ?file ?date WHERE {
  BIND("Coyau" AS ?username)
  ?file (p:P170/pq:P4174) ?username;
    wdt:P571 ?date.
}
LIMIT 1000

Try it!

Maintenance queries[edit]

Files captured with a camera before that camera was released[edit]

select ?file ?file_date ?camera ?camera_date 
with {
  select * {
    # SELECT all camera models
    service <https://query.wikidata.org/sparql> {
      ?camera wdt:P31 wd:Q20741022;
               wdt:P571 ?camera_date.
    }
  }
} as %cameras
where {
  include %cameras .
  #select images taken with the camera
  ?file wdt:P4082 ?camera;
        wdt:P571 ?file_date.
  FILTER(?file_date < ?camera_date)

} LIMIT 1000

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!

Depicts statements with Dutch labels, of files in one Commons category[edit]

SELECT ?file ?title ?depicts ?depicts_label
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:Historia Naturalis van Rudolf II" .
      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:P180 ?depicts .
  service <https://query.wikidata.org/sparql> {
    OPTIONAL {?depicts rdfs:label ?depicts_label FILTER (lang(?depicts_label) = 'nl') } 
    }
}

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 depicts (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;
          schema:url ?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)

Images taken 1km around a center[edit]

#defaultView:Map{"hide":["?coor"]}
# query by Jura1, 2020-11-12
SELECT ?fileLabel ?fileDescription ?image ?coor
WHERE 
{
  hint:Query hint:optimizer "None".
  SERVICE <https://query.wikidata.org/sparql> { wd:Q43244 wdt:P625 ?center }  # Wikidata item with coordinates
  SERVICE wikibase:around {
      ?file wdt:P1259 ?coor.
      bd:serviceParam wikibase:center ?center .
      bd:serviceParam wikibase:radius "1". # 1 kilometer around
  }  
  ?file schema:contentUrl ?url .
  bind(iri(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(substr(str(?url),53)))) AS ?image)  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}

Try it!

Wikimedia Commons assessments[edit]

Valued Images depicting athletics competitors[edit]

#shows files that depict athletics competitors and with the Valued Image assessment 
#defaultView:ImageGrid
SELECT ?file ?image
WITH
{
  SELECT ?item
  WHERE
  {
    SERVICE <https://query.wikidata.org/sparql>
    {
      ?item wdt:P106 wd:Q11513337 .
    }
  }
} AS %get_items
WHERE
{
  INCLUDE %get_items
  ?file wdt:P180 ?item .
  ?file schema:contentUrl ?url .
  ?file wdt:P6731 wd:Q63348040 .
  BIND(IRI(CONCAT("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(SUBSTR(STR(?url),53)))) AS ?image)
}

Try it!

Quality Images depicting Hummingbirds[edit]

#shows files that depict Hummingbirds and with the Quality Image assessment 
#defaultView:ImageGrid
SELECT ?file ?image
WITH
{
  SELECT ?item 
  WHERE
  {
    SERVICE <https://query.wikidata.org/sparql>
    {
        ?item wdt:P171/wdt:P171* wd:Q43624.
     } 
  }
} AS %get_items
WHERE
{
  INCLUDE %get_items
  ?file wdt:P180 ?item .
  ?file schema:contentUrl ?url .
  ?file wdt:P6731 wd:Q63348069 .
  BIND(IRI(CONCAT("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(SUBSTR(STR(?url),53)))) AS ?image)
}

Try it!

Quality Images depicting arch bridges in Italy[edit]

#shows files that depict arch bridges in Italy and with the Quality Image assessment 
#defaultView:ImageGrid
SELECT ?file ?image
WITH
{
  SELECT ?item
  WHERE
  {
    SERVICE <https://query.wikidata.org/sparql>
    {
      ?item wdt:P31  wd:Q158438 .
      ?item wdt:P17  wd:Q38 .
    }
  }
} AS %get_items
WHERE
{
  INCLUDE %get_items
  ?file wdt:P180 ?item .
  ?file schema:contentUrl ?url .
  ?file wdt:P6731 wd:Q63348069 .
  BIND(IRI(CONCAT("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(SUBSTR(STR(?url),53)))) AS ?image)
}

Try it!

Featured Pictures depicting Lepidoptera and taken with a Canon camera[edit]

#shows files that depict Lepidoptera, with the Featured Picture assessment, and taken with a Canon camera
#defaultView:ImageGrid
SELECT ?file ?image
WITH
{
  SELECT ?item 
  WHERE
  {
    SERVICE <https://query.wikidata.org/sparql>
    {
        ?item wdt:P171/wdt:P171* wd:Q28319.
     } 
  }
} AS %get_items
WHERE
{
  INCLUDE %get_items
  ?file wdt:P180 ?item .
  ?file schema:contentUrl ?url .
  ?file wdt:P6731 wd:Q63348049 .
  ?file wdt:P4082 ?camera.
  SERVICE <https://query.wikidata.org/sparql>
    {
          ?camera wdt:P1716 wd:Q63554165 .
     } 
  BIND(IRI(CONCAT("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(SUBSTR(STR(?url),53)))) AS ?image)
}

Try it!

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!