[Transfer] If you want to understand GROUP BY, it is enough to read this article

If you want to understand GROUP BY, it is enough to read this article

The struggle of Java programmers2020-12-22  09:38

 

foreword

The group by keyword is often used in both work and interviews, so it is very necessary to understand it. To understand group by, we have to combine: aggregate function, group by, and having to explain together.

Before we talk, let's prepare a table:

aggregate function

In order to talk about group by, we must first use the aggregation function, so don't worry, let's look down step by step.

The aggregation function is to aggregate "several rows of data" into "one row of data" after calculation

Commonly used aggregate functions:

1.MAX:返回某列的最大值 
2.MIN(column)	返回某列的最高值 
3.COUNT(column)	返回某列的总行数 
4.COUNT(*)	返回表的总行数
5.SUM(column)	返回某列的相加总和
6.AVG(column)	返回某列的平均值

Let's briefly use these aggregation functions.

  1. MAX and MIN functions

Let's calculate the maximum salary and minimum salary in the employee table.

select Max(sal) , Min(sal) from emp;

  1. SUM and AVG functions

Let's calculate the sum of salaries and the average salary.

select sum(sal),avg(sal) from emp;

3. COUNT function

The count function is to calculate the total number of rows. count(*) is to calculate the total number of rows in the table. count (column name) is to calculate the total number of rows in a column (excluding null values).

select count(*),count(comm) from emp;

Careful friends may have discovered, why are the values ​​of count(*) and count(comm) different?

The answer is: the aggregation function only works on non-null data, because null data does not participate in the operation.

Please keep this in mind when using aggregate functions, otherwise the calculated results may not be what you want. We use the average value of the bonus to do the test, the code is as follows:

select avg(comm),avg(ifnull(comm,0)) from emp;

Why is the average value indicated by comm also calculated, but the calculated value is different?

We mentioned above that null does not participate in calculations in aggregate functions. so:

avg(comm) only calculated the bonuses of 4 people (Guan Yu, Zhang Fei, Diao Chan, Wu Yong) and took the average.

However, avg(ifnull(comm,0)) calculates the bonus of all people and takes the average value. So the value is much smaller, and the key is to use the ifnull() function (the function of the ifnull function is to change the value to 0 after finding that the value is null).

Reminder: Pay attention to the null value when using the aggregate function, and use it with the ifnull function~

Now look back at the statement select count(*), count(comm) from emp;, is it much clearer?

Well, we'll stop here for the aggregate function, but it's really not that difficult, is it? Let's start with group by.

GROUP BY

The GROUP BY statement groups the result set by one or more columns. We usually use COUNT, SUM, AVG and other functions together on grouped columns.

In order to make it easier for everyone to watch, I brought the employee table here, let's execute it in groups of jobs first.

select job ,group_concat(ename) from emp group by job;

After seeing that there is no grouping by job, we will automatically classify according to the job field. The group_concat function is to concatenate the classified names into strings with commas. This function is enough for everyone to understand. I am here just to let everyone see it more clearly.

Below we have such a requirement:

求每个部门所有工资总和。

In a simple sentence, it is difficult to use where alone. But group by is very simple. code show as below:

select deptno,sum(sal) from emp group by  deptno;

After we group the table data by the deptno field, we then calculate the sum of each group by sum(sal).

Let's do another difficult exercise: query the number of people whose salary is greater than 1500 in each department.

Is this difficult? Let's split it up a little bit.

1.每个部门:按照deptno分组,
   select deptno from emp group by deptno;
2.工资大于1500:where sal >1500
3.人数:count(*)函数。

Is such a split more clear, we write the statement:

select deptno,count(*) from emp where sal >1500 group by deptno;

Well, that's all for group by, let's talk about having.

HAVING

HAVING is used for re-screening after grouping and can only be used for grouping. (note: after grouping)

Exercise: Find the departments whose salary sum is greater than 9000, and sort by the salary sum.

This question is continued from the above: "Find the sum of all wages in each department", plus a filter after grouping. The sql statement is as follows:

select deptno,sum(sal)  total  from emp group by  deptno having sum(sal) >9000 
order by sum(sal) asc;

The difference between having and where:

   1.having是分组后,where是分组前
   2.where不用使用聚合函数,having可以使用聚合函数。
   3.where在分组之前就会进行筛选,过滤掉的数据不会进入分组。

Summary of execution order of keywords

The writing order of keywords is as follows:

		1.select 
		2.from
		3.where
		4.group by
		5.having
		6.order by
		7. limit

The execution order of the keywords is as follows:

    1.from	//行过滤
		2.where
		3.group by
		4.having
		5.select	//列过滤
		6.order by//排序
		7.limit//附加

Still taking the employee table as an example, let's take the following statement as an example to analyze it step by step.

select deptno,sum(sal)  total  from emp where sal>1000 group by  deptno having sum(sal) >9000 
order by sum(sal) asc;

The result is as follows:

Step 1: Execute the from keyword

等同于执行语句:select *  from emp;

The second step: execute where on the basis of the first step

等同于:select deptno from emp where sal >1000;

The third step: execute group by on the basis of the second step

等同于:select deptno from emp where sal >1000 group by deptno;

Step 4: Execute having on the basis of Step 3

等同于:select deptno from emp where sal >1000 group by deptno having sum(sal) >9000;

Step 5: Select columns based on the third step.

等同于:select deptno,sum(sal)  total from emp where sal >1000 
group by deptno having sum(sal) >9000;

Step 6: order by sorting (omitted)

Step Seven: limit (omitted)

end

Well, that's all for this chapter.

Thank you for following me, the editor will continue to output high-quality content!

Guess you like

Origin blog.csdn.net/jessezappy/article/details/131189750