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:
- You need just one example from a group
- All values in the group are effectively equivalent
- You want to avoid GROUP BY errors without listing all variables
The specific value returned by SAMPLE may vary between query executions.
Basic SAMPLE Examples
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.
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
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
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
| Entity | Description |
|---|---|
wd:Q5 | Human |
wd:Q515 | City |
wd:Q6256 | Country |
wdt:P17 | Country |
wdt:P18 | Image |
wdt:P30 | Continent |
wdt:P31 | Instance of (type) |
wdt:P50 | Author |
wdt:P1082 | Population |