Chapter 3

Aggregate Queries

Analyze and summarize your data with powerful aggregate functions. Count items, calculate averages, find extremes, and group results for insightful analytics.

9
Topics
7+
Examples
Intermediate
Level

Data Analysis with SPARQL

Aggregate functions transform multiple rows of data into summary statistics. Instead of listing every programming language, you can count them, find the oldest, calculate average ages, or group them by paradigm.

Combined with GROUP BY and HAVING, aggregates turn SPARQL into a powerful tool for data analysis and insight generation.

Aggregate Functions

Quick Examples

Counting Results

Count Programming Languages
Run ↗
SELECT (COUNT(?lang) AS ?total)
WHERE {
  ?lang wdt:P31 wd:Q9143 .
}

Grouping and Counting

Languages by Paradigm
Run ↗
SELECT ?paradigmLabel (COUNT(?lang) AS ?count)
WHERE {
  ?lang wdt:P31 wd:Q9143 ;
        wdt:P3966 ?paradigm .
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
}
GROUP BY ?paradigmLabel
ORDER BY DESC(?count)
LIMIT 10

Filtering Groups

Popular Paradigms (5+ Languages)
Run ↗
SELECT ?paradigmLabel (COUNT(?lang) AS ?count)
WHERE {
  ?lang wdt:P31 wd:Q9143 ;
        wdt:P3966 ?paradigm .
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }
}
GROUP BY ?paradigmLabel
HAVING (?count > 5)
ORDER BY DESC(?count)

Aggregate Functions Reference

Function Description Example
COUNT(?var) Count number of values COUNT(?item)
COUNT(DISTINCT ?var) Count unique values COUNT(DISTINCT ?type)
SUM(?var) Sum of numeric values SUM(?population)
AVG(?var) Average of numeric values AVG(?age)
MIN(?var) Minimum value MIN(?date)
MAX(?var) Maximum value MAX(?date)
SAMPLE(?var) Arbitrary value from group SAMPLE(?image)
GROUP_CONCAT(?var; separator=",") Concatenate values GROUP_CONCAT(?name; separator=", ")

Continue Learning

🔢

← Expressions

Review string, date, and math functions.

Go Back
🚀

Advanced Queries →

Explore ASK, CONSTRUCT, and DESCRIBE forms.

Continue