MySQL by

In the following example you will see the how to use the MySQL by. Here, we will use GROUP BY clause. It displays the data in the group of employee salary.

MySQL by

MySQL by

     

In the following example you will see the how to use the MySQL by. Here, we will use GROUP BY clause. It displays the data in the group of employee salary. The GROUP BY statement is used with aggregate functions to group the result.

 

 

 

 

 

Table: employee

CREATE TABLE `employee` ( 
`emp_id` int(11) NOT NULL auto_increment, 
`emp_name` varchar(10) character set utf8 NOT NULL, 
`emp_salary` int(11) NOT NULL, 
`emp_startDate` datetime NOT NULL, 
`dep_name` varchar(50) NOT NULL, 
PRIMARY KEY (`emp_id`) 
)

Query to select all data

SELECT * FROM employee;

It displays the data as follow:

Query to use GROUP BY:

SELECT  dep_name, SUM(emp_salary)  FROM employee GROUP BY dep_name;

The above query sums the employee salary having same department name and shows it in one row. you get the following output: