Skip to main content
Applies to: Embedded ❌, Enterprise ✅ Use SQL aggregates to summarize records, either across a whole table or within groups. Common examples include counts by category, totals and averages.

Grouping

WHERE filters rows before grouping; HAVING filters groups after. Use WHERE for row-level conditions and HAVING for conditions on aggregate results. The planner may push eligible predicates earlier in the query. Grouping by several expressions produces one row per distinct combination:

Aggregate without grouping

Omitting GROUP BY aggregates the whole table into one row, which is the usual way to ask how big something is:
count(*) counts rows; count(col) counts non-null values, which is how you measure how populated a computed column is before deciding to refresh it again.

The function set

Registered in DataFusion 54. Names below are what is actually available. General
Statistical
Approximate
Collecting
The approximate functions are the ones worth reaching for on a large table. approx_distinct answers “roughly how many distinct values” for a fraction of what count(DISTINCT ...) costs, which can help with high-cardinality columns. Measure the accuracy and runtime for your workload. DISTINCT works inside an aggregate: count(DISTINCT category). FILTER narrows one aggregate without narrowing the others:

Aggregating a sample

For an estimate over a very large table, aggregate a sample rather than the whole thing:
For a uniform percentage sample, counts and sums can be scaled by the inverse of the sample fraction to estimate whole-table values. Do not apply that scaling to averages, minima or maxima. Small samples can miss rare categories; consider the sampling method and uncertainty when interpreting the result.

What aggregation does not do

Aggregates run over rows the query already produced. Whether a statement can include vector search is described under SQL vector search. If your server does not support that query form, run the search in an SDK and aggregate its result, or materialize the search output first.