MIN
Find the smallest value in a set. MIN returns the minimum value for numeric, date, or string expressions across grouped results.
Understanding MIN
The MIN aggregate function returns the smallest value from a set of values. It works with numbers (smallest value), dates (earliest), and strings (alphabetically first).
| Syntax | Description | Example Result |
|---|---|---|
MIN(?value) |
Smallest value overall | Single minimum |
MIN(?value) ... GROUP BY ?category |
Smallest per group | One min per category |
MIN(?date) |
Earliest date | First chronologically |
Finding Global Minimums
SELECT (MIN(?area) AS ?minArea)
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P2046 ?area .
}
SELECT (MIN(?elevation) AS ?lowestPoint)
WHERE {
?place wdt:P2044 ?elevation .
FILTER(?elevation < 0)
}
SELECT (MIN(?founded) AS ?earliestFounding)
WHERE {
?uni wdt:P31 wd:Q3918 ;
wdt:P571 ?founded .
}
MIN with GROUP BY
Combining MIN with GROUP BY finds the minimum value within each category, allowing comparisons across different groups.
SELECT ?country ?countryLabel (MIN(?population) AS ?smallestCity)
WHERE {
?city wdt:P31 wd:Q515 ;
wdt:P17 ?country ;
wdt:P1082 ?population .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
GROUP BY ?country ?countryLabel
ORDER BY ?smallestCity
LIMIT 20
SELECT ?occupation ?occupationLabel (MIN(YEAR(?birth)) AS ?earliestBirth)
WHERE {
VALUES ?occupation { wd:Q36180 wd:Q82955 wd:Q1028181 wd:Q593644 }
?person wdt:P31 wd:Q5 ;
wdt:P106 ?occupation ;
wdt:P569 ?birth .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
GROUP BY ?occupation ?occupationLabel
ORDER BY ?earliestBirth
SELECT ?continent ?continentLabel (MIN(?length) AS ?shortestRiver)
WHERE {
?river wdt:P31 wd:Q4022 ;
wdt:P2043 ?length ;
wdt:P30 ?continent .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
GROUP BY ?continent ?continentLabel
ORDER BY ?shortestRiver
Finding the Item with Minimum Value
Often you want to find which item has the minimum value, not just what the minimum is. This requires a subquery pattern.
SELECT ?country ?countryLabel ?area
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P2046 ?area .
{
SELECT (MIN(?a) AS ?minArea)
WHERE {
?c wdt:P31 wd:Q6256 ;
wdt:P2046 ?a .
}
}
FILTER(?area = ?minArea)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
SELECT ?occupation ?occupationLabel ?person ?personLabel ?birth
WHERE {
VALUES ?occupation { wd:Q36180 wd:Q82955 wd:Q1028181 }
?person wdt:P31 wd:Q5 ;
wdt:P106 ?occupation ;
wdt:P569 ?birth .
{
SELECT ?occupation (MIN(?b) AS ?minBirth)
WHERE {
VALUES ?occupation { wd:Q36180 wd:Q82955 wd:Q1028181 }
?p wdt:P106 ?occupation ;
wdt:P569 ?b .
}
GROUP BY ?occupation
}
FILTER(?birth = ?minBirth)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
MIN vs ORDER BY + LIMIT 1
For finding a single minimum, ORDER BY ... LIMIT 1 is often simpler.
Use MIN when you need the value in an aggregate context or with GROUP BY.
SELECT ?country ?countryLabel ?area
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P2046 ?area .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY ?area
LIMIT 1