Learn SQL
SQL Aggregate Functions: COUNT, SUM, MIN, MAX, and AVG Explained
Aggregate functions turn many rows into useful summary values, which is the foundation of reporting, dashboards, and quick data checks.
On This Page
Summaries are the first reports
A table can contain hundreds, thousands, or millions of rows. Aggregate functions help you compress those rows into numbers you can understand quickly: counts, totals, extremes, and averages.
In SQL for Files, you can practice aggregates on sample sales data or run the same ideas against local files in your browser with DuckDB WASM.
COUNT and SUM answer how many and how much
COUNT tells you how many rows match a question. SUM adds numeric values such as revenue, quantity, or cost.
SELECT
COUNT(*) AS sale_count,
SUM(amount) AS total_amount
FROM sales
WHERE region = 'West';COUNT(*) vs COUNT(column)
COUNT(*) counts rows. COUNT(column_name) counts rows where that column is not NULL.
MIN, MAX, and AVG describe the spread
MIN finds the smallest value, MAX finds the largest value, and AVG calculates the mean. Together, they give you a quick profile of a numeric column.
SELECT
MIN(amount) AS smallest_sale,
MAX(amount) AS largest_sale,
AVG(amount) AS average_sale
FROM sales;Aggregates usually return fewer rows
A plain aggregate query without GROUP BY usually returns one row. That one row represents a summary of all matching rows after WHERE filtering has been applied.
This is why aggregates are so useful for dashboards: one query can return the headline numbers your reader needs first.
Practice the Simple Aggregates chapter
- Use COUNT to count matching rows.
- Use SUM to total sales amounts.
- Combine COUNT and SUM for compact reporting.
- Finish with MIN, MAX, and AVG to profile values.
Continue in the editor
Open SQL for Files to add your own CSV, JSON, or Parquet files and try these examples locally in your browser.
Open editorRelated guides
SQL WHERE Clause Explained: Filtering Rows with AND, OR, and LIKE
Filtering is where SQL starts to feel useful: keep only the rows that answer your question, then combine conditions as your questions get sharper.
SQL DISTINCT, GROUP BY, and HAVING: From Unique Values to Grouped Reports
DISTINCT cleans up repeated values, GROUP BY creates summaries per category, and HAVING filters those summaries after aggregation.
Practical SQL Examples for CSV, JSON, and Parquet Files
A compact collection of SQL patterns you can adapt for local file analysis in SQL for Files.