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:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: MySQL by
Post your Comment