🐘 PostgreSQL
Beginner
What are aggregate functions in PostgreSQL?
Answer
Aggregate functions compute a single result from a set of rows. Common ones: COUNT(*) (all rows), COUNT(col) (non-NULL values), COUNT(DISTINCT col), SUM(col), AVG(col), MIN(col), MAX(col). PostgreSQL-specific: STRING_AGG(col, ',') (concatenate strings), ARRAY_AGG(col) (collect into array), JSON_AGG(col) (collect into JSON array), BOOL_AND(col), BOOL_OR(col). Used with GROUP BY: SELECT dept, AVG(salary) FROM employees GROUP BY dept;. FILTER clause for conditional aggregation: COUNT(*) FILTER (WHERE status = 'active').
Previous
What is the difference between WHERE and HAVING?
Next
How do you use GROUP BY in PostgreSQL?