Aggregate

SAMPLE

Pick one arbitrary value from a group. SAMPLE returns any single value from the grouped items, useful when you just need a representative example.

Understanding SAMPLE

SAMPLE is an aggregate function that returns one arbitrary value from a group. Unlike MIN or MAX which have deterministic behavior, SAMPLE can return any value. It's particularly useful when:

The specific value returned by SAMPLE may vary between query executions.

Basic SAMPLE Examples

One City per Country
Run ↗
SELECT ?country ?countryLabel (SAMPLE(?cityLabel) AS ?exampleCity) (COUNT(?city) AS ?cityCount)
WHERE {
  ?city wdt:P31 wd:Q515 ;
        wdt:P17 ?country .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". ?city rdfs:label ?cityLabel. ?country rdfs:label ?countryLabel. }
}
GROUP BY ?country ?countryLabel
ORDER BY DESC(?cityCount)
LIMIT 15

This query counts all cities in each country and shows one example city name. SAMPLE picks any city from the group — the specific city may change between runs.

Example Work per Author
Run ↗
SELECT ?author ?authorLabel (SAMPLE(?workLabel) AS ?exampleWork) (COUNT(?work) AS ?workCount)
WHERE {
  ?work wdt:P50 ?author .
  ?author wdt:P31 wd:Q5 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". ?work rdfs:label ?workLabel. ?author rdfs:label ?authorLabel. }
}
GROUP BY ?author ?authorLabel
HAVING (COUNT(?work) > 50)
ORDER BY DESC(?workCount)
LIMIT 20

Find authors with many works and show one example. SAMPLE provides a representative work without needing to choose the first, longest, or most recent one.

SAMPLE vs Other Approaches

Avoiding Non-Aggregated Variable Errors
Run ↗
SELECT ?continent ?continentLabel (COUNT(?country) AS ?countryCount)
  (SAMPLE(?countryLabel) AS ?exampleCountry)
  (SUM(?population) AS ?totalPop)
WHERE {
  ?country wdt:P31 wd:Q6256 ;
           wdt:P30 ?continent ;
           wdt:P1082 ?population .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". ?country rdfs:label ?countryLabel. ?continent rdfs:label ?continentLabel. }
}
GROUP BY ?continent ?continentLabel

Without SAMPLE, including ?countryLabel would require adding it to GROUP BY, which would create one row per country instead of per continent. SAMPLE lets you include a representative value without changing the grouping.

SAMPLE with Images

Countries with Example Image
Run ↗
SELECT ?continent ?continentLabel (COUNT(?country) AS ?countryCount)
  (SAMPLE(?image) AS ?exampleImage)
WHERE {
  ?country wdt:P31 wd:Q6256 ;
           wdt:P30 ?continent ;
           wdt:P18 ?image .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?continent ?continentLabel

SAMPLE is particularly useful with images — you often just need one representative image rather than all of them. This query picks one country's image per continent.

Key Entities Used in Examples

EntityDescription
wd:Q5Human
wd:Q515City
wd:Q6256Country
wdt:P17Country
wdt:P18Image
wdt:P30Continent
wdt:P31Instance of (type)
wdt:P50Author
wdt:P1082Population