In this section we are going to illustrate aggregate function, with the help of which we can use many arithmetic operation like average, count, maximum and many more in SQL. They are briefly described and denoted by the keyword in the given below section.
Here is the video tutorial of: "How to use aggregate functions in MySQL?"
For all of the above given function the standard code syntax will be:
Table structure with data:
CREATE TABLE `emp_details` ( `id` int(11) NOT NULL AUTO_INCREMENT, `emp_name` varchar(50) DEFAULT NULL, `salary` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1; /*Data for the table `emp_details` */ insert into `emp_details`(`id`,`emp_name`,`salary`) values (1,'Deepak',15000),(2,'Ravi',16000),(3,'Chandan',15000),(4,'Rajesh',18000);
SELECT MAX(salary) FROM emp_details; SELECT MIN(salary) FROM emp_details; SELECT AVG(salary) FROM emp_details; SELECT SUM(salary) FROM emp_details; SELECT COUNT(id) FROM emp_details;
|SELECT "function type" ("column_name") FROM "table_name"|
For example we just take a Employee table and use the aggregate function SUM on the field "Salary" to find out the total salary of the Employee table.
To find out the total salary of the company's employee we should write the following code:
|SELECT SUM (Salary) FROM Employee;|
When we run the above query we will get the following result: