SQL Aggregate Functions (MIN, MAX, SUM, AVG, COUNT)
MIN() – returns the minimum value of the selected field.
MIN() syntax:
SELECT MIN(column_name) FROM table_name;
MIN() Example:
SELECT MIN(salary) FROM employee_record;
It will display the minimum salary.
Result:
+-------------+ | MIN(salary) | +-------------+ | 15000 | +-------------+ 1 row in set (0.00 sec)
MAX() – returns the maximum value of the selected field.
MAX() syntax:
SELECT MAX(column_name) FROM table_name;
MAX() example:
SELECT MAX(salary) FROM employee_record;
It will display the maximum salary.
Result:
+-------------+ | MAX(salary) | +-------------+ | 55000 | +-------------+ 1 row in set (0.00 sec)
SUM() – returns the sum of the values of the selected field.
SUM() syntax:
SELECT SUM(column_name) FROM table_name;
SUM() example:
SELECT SUM(salary) FROM employee_record;
It will display the sum of all ‘salary’ field.
Result:
+-------------+ | SUM(salary) | +-------------+ | 190000 | +-------------+ 1 row in set (0.05 sec)
AVG() – returns the average value of the selected field.
AVG() syntax:
SELECT AVG(column_name) FROM table_name;
AVG() example:
SELECT AVG(salary) FROM employee_record;
It will display the average value of the field ‘salary’.
Result:
+-------------+ | avg(salary) | +-------------+ | 31666.6667 | +-------------+ 1 row in set (0.00 sec)
COUNT() – returns the number of records of the selected field.
COUNT() syntax:
SELECT COUNT(column_name) FROM table_name;
COUNT() example:
SELECT COUNT(id) FROM employee_record;
It will display the number of records in the field ‘id’.
Result:
+-----------+ | COUNT(id) | +-----------+ | 6 | +-----------+ 1 row in set (0.00 sec)