Aggregate Functions
7 Functions
Aggregate functions perform a calculation on a set of values and return a single scalar value. They are often used with the GROUP BY clause to summarize data categories.
Table of Contents
COUNT()
Counts the number of rows that match a specified criterion.
COUNT(expression) or COUNT(*)You want to know how many orders have been placed or how many active users exist.
-- Count total rows in the orders tableSELECT COUNT(*) FROM orders;-- Count only orders where status is 'completed'SELECT COUNT(order_id)FROM ordersWHERE status = 'completed';COUNT(*) counts all rows, including those with NULL values.
COUNT(column_name) counts only non-NULL values in that specific column. Use this distinction wisely!
SUM()
Calculates the total sum of a numeric column.
SUM(column_name)Calculating total revenue from sales or total points earned by a player.
-- Calculate total revenue from all ordersSELECT SUM(total_amount)FROM orders;-- Calculate total sales per category (with GROUP BY)SELECT category, SUM(sales)FROM productsGROUP BY category;SUM() ignores NULL values. If you try to sum a column where all values are NULL, the result will be NULL, not 0. You can wrap it in COALESCE(SUM(col), 0) to force a zero result.
AVG()
Calculates the average (arithmetic mean) value of a numeric column.
AVG(column_name)Finding the average order value (AOV) or the average score of a student class.
-- Find the average price of productsSELECT AVG(price)FROM products;Be careful with precision! In some databases, averaging integers might return an integer (rounded down). In PostgreSQL, AVG returns a numeric type for accurate decimals, which is great for financial calculations.
MIN()
Returns the smallest (minimum) value in a set.
MIN(column_name)Finding the cheapest product, the earliest order date, or the lowest test score.
-- Find the cheapest product priceSELECT MIN(price) FROM products;-- Find the earliest registration date of a userSELECT MIN(created_at) FROM users;MIN() isn't just for numbers! It works on Dates (earliest date) and Strings (alphabetically first, e.g., "Apple" comes before "Banana").
MAX()
Returns the largest (maximum) value in a set.
MAX(column_name)Finding the most expensive item, the latest login time, or the highest XP score.
-- Find the most expensive productSELECT MAX(price) FROM products;-- Find the most recent activity timestampSELECT MAX(last_login) FROM users;Like MIN, MAX() works on text too. MAX(name) will return the name that is last in alphabetical order (e.g., "Zebra").
STRING_AGG()
Concatenates values from multiple rows into a single string, separated by a delimiter.
STRING_AGG(expression, delimiter)You want a comma-separated list of all email addresses belonging to a specific user group.
-- List all project names for each employee in one lineSELECT employee_id, STRING_AGG(project_name, ', ')FROM employee_projectsGROUP BY employee_id;This is incredibly useful for reporting. Instead of getting 5 rows for one user, you get 1 row with all their items listed clearly. You can also order the list inside the function: STRING_AGG(name, ', ' ORDER BY name).
COUNT(DISTINCT)
Counts the number of unique non-null values.
COUNT(DISTINCT column_name)You want to know how many unique customers made a purchase today (ignoring if the same customer bought 5 times).
-- Count how many unique customers placed an orderSELECT COUNT(DISTINCT customer_id)FROM orders;This is computationally more expensive than a regular COUNT, so use it only when you genuinely need to eliminate duplicates from your count.