Detailed explanation of group by and having in MySQL

Detailed explanation of group by and having in MySQL

The GROUP BY syntax can group and count the query results according to each member of a given data column, and finally obtain a group summary table.

The column name in the select clause must be a grouping column or a column function that returns a result for each group defined by the group by clause.

The structure and data of an employee information table are as follows:

  id name dept salary edlevel hiredate 
  1 Zhang San Development Department 2000 3 2009-10-11
  2 Li Si Development Department 2500 3 2009-10-01
  3 Wang Wu Design Department 2600 5 2010-10-02
  4 Wang Liu Design Department 2300 4 2010-10-03
  5 Maqi Design Department 2100 4 2010-10-06
  6 Zhao Ba Sales Department 3000 5 2010-10-05
  7 Qianjiu Sales Department 3100 7 2010-10-07
  8 Sun Ten Sales Department 3500 7 2010-10-06

I want to list the highest salary in each department. The SQL statement is as follows:

select dept , max(salary) AS MAXIMUM
FROM STAFF
GROUP BY DEPT

The query results are as follows:

dept MAXIMUM
Development Department 4500
Design Department 2600
Sales Department 3500

Explain this result:

1. "The column name in the SELECT clause must be a grouping column or a column function" is satisfied because the SELECT has a column dept included in group by;

2. "The column function returns a result for each group defined by the group by clause". According to the department grouping, a result is returned for each department, which is the highest salary of each department.

Using the where clause with the group by clause

Grouped queries can have a standard where clause that eliminates non-qualifying rows before forming the groups and computed column functions. The where clause must be specified before the group by clause

For example, query the highest salary of each department and each level of the company who joined in 2010

SELECT dept,edlevel,MAX(salary) AS MAXIMUM
FROM STAFF
WHERE hiredate > '2010-01-01'
GROUP BY dept,edlevel

The query results are as follows:

dept edlevel MAXIMUM
Design Department 4 2300
Design Department 5 2600
Sales Department 5 3000
Sales Department 7 3500

Every column name specified in the SELECT statement is also mentioned in the GROUP BY clause. Column names not mentioned in both places will generate an error. The GROUP BY clause returns one row for each unique combination of dept and edlevel.

Using the Having Clause after the GROUP BY Clause

You can apply qualifications to groupings so that only results are returned for groups that meet the criteria. Therefore, a HAVING clause is included after the GROUP BY clause. HAVING is similar to WHERE (the only difference is that WHERE filters rows, HAVING filters groups). HAVING supports all WHERE operators.

For example, to find the highest and lowest salaries for departments with more than 2 employees:

SELECT dept ,MAX(salary) AS MAXIMUM ,MIN(salary) AS MINIMUM
FROM STAFF
GROUP BY dept
HAVING COUNT(*) > 2
ORDER BY dept

The query results are as follows:

dept MAXIMUM MINIMUM
Design Department 2600 2100
Sales Department 3500 3000

For example, to find the highest and lowest salaries in departments where the average salary of employees is greater than 3000:

SELECT dept,MAX(salary) AS MAXIMUM,MIN(salary) AS MINIMUM
FROM STAFF
GROUP BY dept
HAVING AVG(salary) > 3000
ORDER BY dept

The query results are as follows:

dept MAXIMUM MINIMUM
Sales Department 3500 3000

This is the end of this article about the detailed usage of MySQL group by and having. For more relevant MySQL group by and having content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the group by statement in MySQL database group query
  • MySQL efficient query left join and group by (plus index)
  • Detailed explanation of MySQL Group by optimization
  • How to optimize MySQL group by statement
  • How to use MySQL group by and order by together
  • mysql group by grouping multiple fields
  • Implement group by based on MySQL to get the latest data of each group
  • A brief discussion on group by in MySQL

<<:  Detailed example of HTML element blocking Flash

>>:  Detailed explanation of gantt chart draggable and editable (highcharts can be used for vue and react)

Recommend

Detailed explanation of samba folder sharing server configuration under centos

1. Introduction Recently I found that there are m...

Summary of Common Letters in Unicode

Most of the earliest computers could only use ASC...

Three commonly used MySQL data types

Defining the type of data fields in MySQL is very...

How to use Vue to implement CSS transitions and animations

Table of contents 1. The difference between trans...

Briefly describe the use and description of MySQL primary key and foreign key

Table of contents 1. Foreign key constraints What...

Pure CSS to achieve cool charging animation

Let’s take a look at what kind of charging animat...

mysql 8.0.12 winx64 download and installation tutorial

MySQL 8.0.12 download and installation tutorial f...

Detailed example of inserting custom HTML records in Quill editor

It is already 2020. Hungry humans are no longer s...

How to configure Nginx's anti-hotlinking

Experimental environment • A minimally installed ...

How to install PostgreSQL and PostGIS using yum on CentOS7

1. Update the yum source The PostgreSQL version o...

A brief discussion on the VUE uni-app life cycle

Table of contents 1. Application Lifecycle 2. Pag...

Detailed usage of React.Children

Table of contents 1. React.Children.map 2. React....

Object.entries usage you don't know in JavaScript

Table of contents Preface 1. Use for...of to iter...

Telnet is moved to busybox-extras in Alpine image

The telnet in the Alpine image has been moved to ...