Detailed explanation of the group by statement in MySQL database group query

Detailed explanation of the group by statement in MySQL database group query

1: Statement order of grouping function

   1 SELECT
    ...
   2 FROM
   ...
   3 WHERE
   ...
   4 GROUP BY
   ...
   5 HAVING
   ...
   6 ORDER BY
   ...

2. The difference between WHERE and HAVING filter conditions

                  Data source location keyword WHERE Original table ORDER BY statement before WHERE
  
  HAVING Grouped result set after ORDER BY statement HAVING

Three: Examples

#1. Query the maximum age of students in each classSELECT 
       MAX(age),class
  FROM
       STU_CLASS
  GROUP BY CLASS;
# 2. Query the maximum age of male and female students in each class SELECT
         MAX(age), class, sex
  FROM
        STU_CLASS
   GROUP BY
        class,sex;
#3. Query the average age of boys and girls in each class, and the average age is greater than 15 years old SELECT
       AVG (age), class, sex
FROM 
       STU_CLASS
GROUP BY
       class,sex
 HAVING
        avg(age) > 15;

Four: Note

1. Usually, the GROUP BY keyword is used together with an aggregate function. First, the GROUP BY keyword is used to group the records, and then the aggregate function is used to calculate each group. The GROUP BY keyword and aggregate functions are often needed when performing statistics.

COUNT() function: used to count the number of records.
SUM() function: used to calculate the sum of the values ​​of a field.
AVG() function: used to calculate the average value of a field.
MAX() function: used to query the maximum value of a field.
MIN() function: used to query the minimum value of a field.

The above is the detailed content of the detailed explanation of the group by statement of MySQL database group query. For more information about MySQL database statements, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • A brief discussion on group by in MySQL
  • How to use the query expression GroupBy in C#
  • Detailed explanation of the usage rules of group by in Oracle group query
  • How to use groupby in datatable for group statistics
  • Detailed explanation of group by usage

<<:  Install MySQL (including utf8) using Docker on Windows/Mac

>>:  Detailed explanation of Grid layout and Flex layout of display in CSS3

Recommend

Problems and solutions encountered when installing mininet on Ubuntu 16.04.4LTS

Mininet Mininet is a lightweight software defined...

Example code for configuring monitoring items and aggregated graphics in Zabbix

1. Install Zabbix Agent to monitor the local mach...

Detailed tutorial for upgrading zabbix monitoring 4.4 to 5.0

1. Zabbix backup [root@iZ2zeapnvuohe8p14289u6Z /]...

Detailed explanation of the use of Linux seq command

01. Command Overview The seq command is used to g...

A great collection of web standards learning resources

These specifications are designed to allow for bac...

Completely uninstall mysql. Personal test!

Cleanly uninstall MySQL. Personally tested, this ...

Solve the problem of specifying udp port number in docker

When Docker starts a container, it specifies the ...

Beginners learn some HTML tags (2)

Related article: Beginners learn some HTML tags (1...

Common methods and problems of Docker cleaning

If you use docker for large-scale development but...

The practical process of login status management in the vuex project

Table of contents tool: Login scenario: practice:...

Pessimistic locking and optimistic locking in MySQL

In relational databases, pessimistic locking and ...

XHTML introductory tutorial: text formatting and special characters

<br />This section introduces how to impleme...

Linux concurrent execution is simple, just do it this way

Concurrency Functions time for i in `grep server ...

CentOS IP connection network implementation process diagram

1. Log in to the system and enter the directory: ...

Detailed explanation of linux crm deployment code

Linux basic configuration Compile and install pyt...