MAX
Find the largest value in a set. MAX returns the maximum value for numeric, date, or string expressions across grouped results.
Understanding MAX
The MAX aggregate function returns the largest value from a set of values. It works with numbers (highest value), dates (most recent), and strings (alphabetically last).
| Syntax | Description | Example Result |
|---|---|---|
MAX(?value) |
Largest value overall | Single maximum |
MAX(?value) ... GROUP BY ?category |
Largest per group | One max per category |
MAX(?date) |
Most recent date | Last chronologically |
Finding Global Maximums
SELECT (MAX(?area) AS ?maxArea)
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P2046 ?area .
}
SELECT (MAX(?elevation) AS ?highestPoint)
WHERE {
?place wdt:P31 wd:Q8502 ;
wdt:P2044 ?elevation .
}
SELECT (MAX(?founded) AS ?latestFounding)
WHERE {
?uni wdt:P31 wd:Q3918 ;
wdt:P571 ?founded .
}
MAX with GROUP BY
Combining MAX with GROUP BY finds the maximum value within each category, enabling comparisons across different groups.
SELECT ?country ?countryLabel (MAX(?population) AS ?largestCity)
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 DESC(?largestCity)
LIMIT 20
SELECT ?occupation ?occupationLabel (MAX(YEAR(?birth)) AS ?latestBirth)
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 DESC(?latestBirth)
SELECT ?continent ?continentLabel (MAX(?length) AS ?longestRiver)
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 DESC(?longestRiver)
Finding the Item with Maximum Value
Often you want to find which item has the maximum value, not just what the maximum is. This requires a subquery pattern.
SELECT ?country ?countryLabel ?area
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P2046 ?area .
{
SELECT (MAX(?a) AS ?maxArea)
WHERE {
?c wdt:P31 wd:Q6256 ;
wdt:P2046 ?a .
}
}
FILTER(?area = ?maxArea)
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 (MAX(?b) AS ?maxBirth)
WHERE {
VALUES ?occupation { wd:Q36180 wd:Q82955 wd:Q1028181 }
?p wdt:P106 ?occupation ;
wdt:P569 ?b .
}
GROUP BY ?occupation
}
FILTER(?birth = ?maxBirth)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
MAX vs ORDER BY DESC + LIMIT 1
For finding a single maximum, ORDER BY DESC ... LIMIT 1 is often simpler.
Use MAX 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 DESC(?area)
LIMIT 1
Combining MIN and MAX
You can use both MIN and MAX in the same query to find ranges or compare extremes.
SELECT ?country ?countryLabel
(MIN(?pop) AS ?smallestCity)
(MAX(?pop) AS ?largestCity)
(MAX(?pop) - MIN(?pop) AS ?range)
WHERE {
?city wdt:P31 wd:Q515 ;
wdt:P17 ?country ;
wdt:P1082 ?pop .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
GROUP BY ?country ?countryLabel
ORDER BY DESC(?range)
LIMIT 10