SQL utilizes various clauses in its commands, such as the GROUP BY clause, the HAVING clause, WHERE clause in SQL, ... . In this article, Mytour will introduce you to the GROUP BY clause in SQL.
The GROUP BY Clause in SQL
Using the GROUP BY clause in SQL with the Group function to aggregate data based on one or more columns.
Example of the GROUP BY clause in SQL
If you want to find out the total salary expenditure for each department in the company, the SQL GROUP BY query would be:
SELECT dept, SUM(salary)
FROM the employee table
GROUP BY department;
The output will be as follows:
Note regarding the GROUP BY clause in SQL
The GROUP BY clause in SQL includes all the columns listed used with the Group function.
For example:
SELECT location, department, SUM(salary)
FROM the employee table
GROUP BY location, department;
The output will be as follows:
Here, Mytour has just introduced you to the GROUP BY clause in SQL. In the next articles, Mytour will further introduce you to the HAVING clause in SQL, the WHERE clause in SQL. In addition, readers can refer to some other articles on Mytour to learn about commands such as UPDATE, SELECT, INSERT command in SQL,... to have a better understanding of database management knowledge.
