MySQL learning-group by and having

Operating table
mysql> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SHALL COMM DEPTNO
7369 SMITH CLERK 7902 1980-12-17 800.00 NULL 20
7499 ALLEN SALESMAN 7698 1981-02-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-02-22 1250.00 500.00 30
7566 JONES MANAGER 7839 1981-04-02 2975.00 NULL 20
7654 MARTIN SALESMAN 7698 1981-09-28 1250.00 1400.00 30
7698 BLAKE MANAGER 7839 1981-05-01 2850.00 NULL 30
7782 CLARK MANAGER 7839 1981-06-09 2450.00 NULL 10
7788 SCOTT ANALYST 7566 1987-04-19 3000.00 NULL 20
7839 KING PRESIDENT NULL 1981-11-17 5000.00 NULL 10
7844 TURNER SALESMAN 7698 1981-09-08 1500.00 0.00 30
7876 ADAMS CLERK 7788 1987-05-23 1100.00 NULL 20
7900 JAMES CLERK 7698 1981-12-03 950.00 NULL 30
7902 FORD ANALYST 7566 1981-12-03 3000.00 NULL 20
7934 MILLER CLERK 7782 1982-01-23 1300.00 NULL 10

group by

group by: Group by a certain field or certain fields.
having: having is to filter the data after grouping again.
Example: Find the highest salary for each job position.

(Find the highest salary of each group, so you need to group first)
    select job,max(sal) from emp group by job;

+-----------+----------+
| job       | max(sal) |
+-----------+----------+
| ANALYST   |  3000.00 |
| CLERK     |  1300.00 |
| MANAGER   |  2975.00 |
| PRESIDENT |  5000.00 |
| SALESMAN  |  1600.00 |
+-----------+----------+

First from, then group by, and finally the max
grouping function is always executed after the group function ends.
Note: The grouping function is generally used in conjunction with the group by, which is why it is called the grouping function.
And any grouping function (count sum avg max min) is executed after the group by statement is executed.

When a sql statement does not have a group by, the data of the entire table will form a group by itself.

Multi-field group query

Find out the highest salary of each job
    select ename,job,max(sal) from emp group by job; can
this statement be executed, Oracle can’t, MySQL can, but it’s meaningless

+-------+-----------+----------+
| ename | job       | max(sal) |
+-------+-----------+----------+
| SCOTT | ANALYST   |  3000.00 |
| SMITH | CLERK     |  1300.00 |
| JONES | MANAGER   |  2975.00 |
| KING  | PRESIDENT |  5000.00 |
| ALLEN | SALESMAN  |  1600.00 |
+-------+-----------+----------+

The above query results are available in mysql, but the results are meaningless, and errors will be reported in the Oracle database. Grammatical errors.
Oracle's grammar rules are stricter than MySQL grammar rules.
Remember a rule: when there is a group by in a statement, select can only be followed by the grouping function and the fields participating in the grouping.
The average salary per job position?
    select job,avg(sal) from emp group by job;

+-----------+-------------+
| job       | avg(sal)    |
+-----------+-------------+
| ANALYST   | 3000.000000 |
| CLERK     | 1037.500000 |
| MANAGER   | 2758.333333 |
| PRESIDENT | 5000.000000 |
| SALESMAN  | 1400.000000 |
+-----------+-------------+

Can multiple fields be grouped together?
Case: Find the highest salary for different jobs in each department.

Check the data first: select deptno,job,sal from emp order by deptno;

+--------+-----------+---------+
| deptno | job       | sal     |
+--------+-----------+---------+
|     10 | MANAGER   | 2450.00 |
|     10 | PRESIDENT | 5000.00 |
|     10 | CLERK     | 1300.00 |
|     20 | CLERK     |  800.00 |
|     20 | MANAGER   | 2975.00 |
|     20 | ANALYST   | 3000.00 |
|     20 | CLERK     | 1100.00 |
|     20 | ANALYST   | 3000.00 |
|     30 | SALESMAN  | 1600.00 |
|     30 | SALESMAN  | 1250.00 |
|     30 | SALESMAN  | 1250.00 |
|     30 | MANAGER   | 2850.00 |
|     30 | SALESMAN  | 1500.00 |
|     30 | CLERK     |  950.00 |
+--------+-----------+---------+
		select 
			deptno,job,max(sal)
		from
			emp
		group by
			deptno,job;

The choice of where and having
Find out the highest salary of each department, and it is required to display data with salary greater than 2900.

When writing SQL statements, sometimes don't imagine that you can write them in one step. Sometimes you read a sentence and write a sentence. Write in steps.
The first step: find out the highest salary of each department
    select max(sal), deptno from emp group by deptno;

+----------+--------+
| max(sal) | deptno |
+----------+--------+
|  5000.00 |     10 |
|  3000.00 |     20 |
|  2850.00 |     30 |
+----------+--------+

Step 2: Find out that the salary is greater than 2900
    select max(sal), deptno from emp group by deptno having max(sal)> 2900; // This method is inefficient. Because the first time the result has been found, the grouping function must be calculated

+----------+--------+
| max(sal) | deptno |
+----------+--------+
|  5000.00 |     10 |
|  3000.00 |     20 |
+----------+--------+

    select max(sal), deptno from emp where sal> 2900 group by deptno; // High efficiency, it is recommended to use where to filter as much as possible.
where has been filtered in advance.
Where we can't figure it out, we use having

To find out the average salary of each department, it is required to display data with salary greater than 2000.
The first step: to find out the average salary of each department
    select deptno, avg(sal) from emp group by deptno; the
second step: to display data with salary greater than 2000
    select avg(sal), deptno from emp group by deptno having avg (sal)>2000;

+--------+-------------+
| deptno | avg(sal)    |
+--------+-------------+
|     10 | 2916.666667 |
|     20 | 2175.000000 |
+--------+-------------+

Grouping function cannot be used after where:
    select deptno,avg(sal) from emp where avg(sal)> 2000 group by deptno; // It is wrong.
In this case, only having filtering can be used.
Having is the partner of groupby. Only when groupby appears, having has meaning

Summarize how to write a complete DQL statement?

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

The sorting is the last, and the sorting is the last output.

Remove duplicate records

1. About the de-duplication of the query result set?

    select DISTINCT job from emp; // The distinct keyword removes duplicate records.

+-----------+
| job       |
+-----------+
| CLERK     |
| SALESMAN  |
| MANAGER   |
| ANALYST   |
| PRESIDENT |
+-----------+

MySQL> the SELECT ename, DISTINCT the Job from emp;
by You have have AN error in your SQL syntax; the Check at The Manual that Corresponds to your MySQL Server Version for at The right syntax to use near 'DISTINCT the Job from emp' AT Line 1
SQL statement above is wrong.
Remember: distinct can only appear at the top of all fields.

    select distinct deptno,job from emp;

+--------+-----------+
| deptno | job       |
+--------+-----------+
|     20 | CLERK     |
|     30 | SALESMAN  |
|     20 | MANAGER   |
|     30 | MANAGER   |
|     10 | MANAGER   |
|     20 | ANALYST   |
|     10 | PRESIDENT |
|     30 | CLERK     |
|     10 | CLERK     |
+--------+-----------+

Putting distinct at the top means removing duplicate records in all subsequent fields. Here the table name removes deptno and job and removes duplicates at the same time.
Case: count the number of jobs?
   select count(distinct job) from emp;
count the number after deduplication

+---------------------+
| count(distinct job) |
+---------------------+
|                   5 |
+---------------------+

Guess you like

Origin blog.csdn.net/qq_39736597/article/details/111718933