SQL Reference Guide
Revision Time: 2 mins

3. Aggregate Functions Reference

Working with totals.

COUNT()
Return: Value

Count number of non-null records.

Syntax signature:COUNT(col) or COUNT(*)
Code snippet:
python
SELECT COUNT(*) FROM users;
Expected Output:Total users count.
Remember: Always ensure correct syntax formatting when calling COUNT().
SUM()
Return: Value

Calculate numerical sums.

Syntax signature:SUM(col)
Code snippet:
python
SELECT SUM(amount) FROM orders;
Expected Output:Total order spends sum.
Remember: Always ensure correct syntax formatting when calling SUM().
AVG()
Return: Value

Compute mathematical average.

Syntax signature:AVG(col)
Code snippet:
python
SELECT AVG(salary) FROM employees;
Expected Output:Average salary value.
Remember: Always ensure correct syntax formatting when calling AVG().
MIN()
Return: Value

Find minimum column value.

Syntax signature:MIN(col)
Code snippet:
python
SELECT MIN(price) FROM products;
Expected Output:Minimum product price.
Remember: Always ensure correct syntax formatting when calling MIN().
MAX()
Return: Value

Find maximum column value.

Syntax signature:MAX(col)
Code snippet:
python
SELECT MAX(price) FROM products;
Expected Output:Maximum product price.
Remember: Always ensure correct syntax formatting when calling MAX().
GROUP BY
Return: Value

Group records sharing values.

Syntax signature:SELECT col, AGG(val) FROM t GROUP BY col;
Code snippet:
python
SELECT dept, SUM(salary) FROM employees GROUP BY dept;
Expected Output:Total salary per department.
Remember: Always ensure correct syntax formatting when calling GROUP BY.
HAVING
Return: Value

Filter aggregated groups after GROUP BY.

Syntax signature:SELECT col FROM t GROUP BY col HAVING condition;
Code snippet:
python
SELECT dept FROM employees GROUP BY dept HAVING COUNT(*) > 5;
Expected Output:Departments with more than 5 employees.
Remember: Always ensure correct syntax formatting when calling HAVING.