Docs
Docs/Aggregations & Grouping/Aggregate Functions

Aggregate Functions

Calculate summary statistics on data

Aggregate functions perform calculations across multiple rows and return a single result.

Common Aggregate Functions

FunctionDescriptionExample
COUNT(*)Count all rowsCOUNT(*)
COUNT(col)Count non-NULL valuesCOUNT(email)
SUM(col)Total of valuesSUM(total)
AVG(col)Average valueAVG(price)
MIN(col)Smallest valueMIN(created_at)
MAX(col)Largest valueMAX(score)

Basic Examples

-- Count total users
SELECT COUNT(*) AS total_users FROM users;

-- Sum all order totals
SELECT SUM(total) AS revenue FROM orders;

-- Average order value
SELECT AVG(total) AS avg_order FROM orders;

-- Find oldest and newest orders
SELECT 
    MIN(created_at) AS first_order,
    MAX(created_at) AS last_order
FROM orders;

COUNT Variations

-- Count all rows (including NULLs)
SELECT COUNT(*) FROM users;

-- Count non-NULL values in a column
SELECT COUNT(phone) FROM users;  -- Users with phone numbers

-- Count unique values
SELECT COUNT(DISTINCT country) FROM users;

-- Difference shows users without phone
SELECT 
    COUNT(*) AS total,
    COUNT(phone) AS with_phone,
    COUNT(*) - COUNT(phone) AS without_phone
FROM users;

Remember: All aggregate functions except COUNT(*) ignore NULL values.

Need help?

Join our Discord community for support and discussions.

Join Discord