Aggregate
SUM
Calculate totals from numeric values. SUM adds up all values in a group, essential for population totals, revenue calculations, and more.
Understanding SUM
SUM calculates the total of numeric values. It ignores null values and works with GROUP BY to compute sums per category.
Basic SUM Examples
Total World Population
SELECT (SUM(?population) AS ?totalPopulation)
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P1082 ?population .
}
Total Area of European Countries
SELECT (SUM(?area) AS ?totalArea)
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P30 wd:Q46 ;
wdt:P2046 ?area .
}
SUM with GROUP BY
Population per Continent
SELECT ?continent ?continentLabel (SUM(?population) AS ?totalPop)
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P30 ?continent ;
wdt:P1082 ?population .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?continent ?continentLabel
ORDER BY DESC(?totalPop)