The GROUP BY clause in SQL


Previous articles Taimienphi.vn introduced you to SQL commands, including CREATE statements, DELETE statements, UPDATE statements, INSERT statements, … this article Taimienphi.vn will introduce you to the GROUP clause BY in SQL.


SQL uses many different clauses that come with statements, such as clauses GROUP BY, clause HAVING, The WHERE clause in SQL, …. In this article, Taimienphi.vn will introduce you first to the GROUP BY clause in SQL.

The GROUP BY clause in SQL

The GROUP BY clause in SQL is used with the Group function to retrieve data grouped in one or more columns.

Example GROUP BY clause in SQL

If you want to know the total salary paid for each department in the company, the GROUP BY query in SQL would be:

SELECT dept, SUM (salary)

FROM employee

GROUP BY dept;

The output will look like this:

group by in sql 2

Notice with the GROUP BY clause in SQL

The GROUP BY clause in SQL contains all the columns in the list used with the Group function.

For example:

SELECT location, dept, SUM (salary)

FROM employee

GROUP BY location, dept;

The output will look like this:

group by in sql 3

https://thuthuat.taimienphi.vn/group-by-rong-sql-33472n.aspx
Above Taimienphi.vn just introduced you to the GROUP BY clause in SQL. In the next articles, Taimienphi.vn will introduce you more about the The HAVING clause in SQL, the WHERE clause in SQL. In addition, readers can refer to some other articles on Taimienphi.vn to learn about commands such as UPDATE, SELECT, INSERT statement in SQL, … to better understand the database administration knowledge.

.

Add a Comment

Your email address will not be published. Required fields are marked *