SQL Reference Guide
Revision Time: 2 mins
3. Aggregate Functions Reference
Working with totals.
COUNT()
Return: ValueCount 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: ValueCalculate 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: ValueCompute 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: ValueFind 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: ValueFind 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: ValueGroup 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: ValueFilter 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.