SQL 如何使用GROUP BY

SQL 如何使用GROUP BY

在这个SQL部分,你将学习什么是GROUP BY关键字以及如何在结构化查询语言中使用它。我们还将讨论如何在WHERE子句中使用GROUP BY子句。

什么是GROUP BY?

GROUP BY是用于在SELECT查询中使用SQL函数对一列的相同值进行分组排列的SQL关键字。

GROUP BY子句的语法

SELECT Column_Name_1, Column_Name_2, ........, Column_Name_N FROM Table_Name GROUP BY Column_Name_1, Column_Name_2, ........, Column_Name_N;

我们可以在GROUP BY子句中使用多个表格字段。多个列名之间必须用逗号分隔。

按照以下步骤学习如何在SQL表中使用GROUP BY子句:

  1. 创建一个简单的数据库和表。
  2. 向表中插入数据。
  3. 查看不带GROUP BY子句的插入数据。
  4. 使用GROUP BY子句。

步骤1:创建一个简单的数据库和表

首先,在SQL中创建一个新数据库。

以下查询创建了Hospital数据库:

CREATE Database Hospital;

现在,您必须使用以下的CREATE TABLE语法来创建新表:

CREATE TABLE table_name  
(
column_Name_1 data type (size of the column_1),  
column_Name_2 data type (size of the column_2),  
column_Name_3 data type (size of the column_3),  
...  
column_Name_N data type (size of the column_1)
);  

下面的查询在医院数据库中创建了Doctor_Info表:

CREATE TABLE Doctor_Info
(
Doctor_ID Int PRIMARY KEY,  
Doctor_Name VARCHAR (100),  
Doctor_Specialist VARCHAR (80),  
Doctor_GenderVarchar (20),
Doctor_Country Varchar (80) 
) ;

步骤2:将数据插入表格中

现在,您需要使用以下语法将数据插入表格中:

INSERT INTO <Table_Name> (Column_Name_1, Column_Name_2, Column_Name_3, ......., Column_Name_N) VALUES (value_1 of Column_1, value_2, value_3, ...., value_N);  

以下查询插入那些在医院工作的医生的记录:

INSERT INTO Doctor_Info (Doctor_ID, Doctor_Name, Doctor_Specialist, Doctor_Gender, Doctor_Country) VALUES ( 1035, Jones, Malaria_Specialist, Male, United Kingdom),
(1015, Marry, Diabties_Specialist, Female, United States),
(1003, Harry, Fever_Specialist, Male, United Kingdom),
(1044, Ella, Cancer_Specialist, Female, United States),
(1025, Moria, Corona_Specialist, Female, Europe);

步骤3:查看未分组的插入数据表

以下查询显示医生记录,但是按照没有排序的方式显示:

SELECT * FROM Doctor_Info;

上面SELECT查询的输出如下所示:

Doctor_ID Doctor_Name Doctor_Disease Doctor_Gender Doctor_Country
1035 Jones Malaria_Specialist Male United Kingdom
1015 Marry Diabities_Specialist Female United State
1003 Harry Fever_Specialist Male United Kingdom
1044 Ella Cancer_Specialist Female United State
1025 Moria Corona_Specialist Female Europe

步骤4:使用GROUP BY子句

以下SQL查询使用GROUP BY关键字列出每个国家的医生数量:

SELECT COUNT (Doctor_ID), Doctor_Country GROUP BY Doctor_Country;

上述使用ORDER BY查询的输出如下所示:

输出:

SQL 如何使用GROUP BY

使用SQL的ORDER BY子句进行GROUP BY

我们也可以在SQL SELECT语句的GROUP BY子句中使用ORDER BY关键字。

GROUP BY子句与ORDER BY子句的语法

SELECT Function_Name (Column_Name) FROM Table_Name GROUP BY Column_Name ORDER BY Function_Name (Column_Name);

示例:使用GROUP BY子句和ORDER BY子句

下面的查询在School数据库中创建新的Subject表:

CREATE TABLE Subject
(
Subject_ID INT PRIMARY KEY,  
Subject_Name VARCHAR (50),  
Subject_Teacher VARCHAR (70),
Student_ID INT
) ;

下面的INSERT INTO查询将记录插入Subject表中:

INSERT INTO Subject(Subject_ID, Subject_Name, Subject_Teacher, Student_ID) VALUES (2211, Maths, Ramesh, 101),
(2252, English, Somya, 103),
(2201, Chemistry, Suresh, 101),
(2224, Physics, Aman, 103),
(2248, Computer, Bhanu, 101),
(2208, Hindi, Sonu, 104),
(2221, Biology, Punit, 104)); 

以下查询使用了带有GROUP BY的ORDER BY子句的SQL语句:

SELECT Count(Subject_ID), Student_ID FROM Subject GROUP BY Student_ID ORDER BY COUNT(Subject_ID) DESC; 

输出:

SQL 如何使用GROUP BY

使用MIN函数的GROUP BY子句

我们还可以在结构化查询语言中使用MIN聚合函数与GROUP BY子句一起使用。

GROUP BY子句与MIN函数的语法:

SELECT Column_Name_1, MIN(Column_Name) FROM Table_Name GROUP BY Column_Name_1;

MIN聚合函数与GROUP BY子句的示例

此示例使用以下School_Stu_Details表来理解使用MIN聚合函数的GROUP BY子句的概念:

CREATE TABLE School_Stu_Details
(
Stu_ID INT NOT NULL, 
Stu_Name varchar(100),
Stu_Subject varchar(50),
Stu_Age INT, 
Stu_Marks INT
); 

以下的INSERT INTO语句插入了关于学校学生的记录:

INSERT INTO School_Stu_Details VALUES (101, Anuj, English, 20, 88);
INSERT INTO School_Stu_Details VALUES (102, Raman, Maths, 24, 98);
INSERT INTO School_Stu_Details VALUES (104, Shyam, Hindi, 19, 92);
INSERT INTO School_Stu_Details VALUES (107, Vikash, Computer, 20, 78);
INSERT INTO School_Stu_Details VALUES (111, Monu, English, 21, 65);
INSERT INTO School_Stu_Details VALUES (114, Jones, Hindi, 18, 93);
INSERT INTO School_Stu_Details VALUES (121, Parul, Maths, 20, 97);
INSERT INTO School_Stu_Details VALUES (123, Divya, English, 21, 89);
INSERT INTO School_Stu_Details VALUES (128, Hemant, Computer, 23, 90);
INSERT INTO School_Stu_Details VALUES (130, Nidhi, Hindi, 20, 88);
INSERT INTO School_Stu_Details VALUES (132, Priya, English, 22, 99);
INSERT INTO School_Stu_Details VALUES (138, Mohit, Maths, 21, 92);

以下查询仅以表格形式显示学生的记录:

SELECT * FROM School_Stu_Details;
Stu_ID Stu_Name Stu_Subject Stu_Age Stu_Marks
101 Anuj English 20 88
102 Raman Maths 24 98
104 Shyam Hindi 19 92
107 Vikash Computer 20 78
111 Monu English 21 65
114 Jones Hindi 18 93
121 Parul Maths 20 97
123 Divya English 21 89
128 Hemant Computer 23 90
130 Nidhi Hindi 20 88
132 Priya English 22 99
138 Mohit Maths 21 92

以下查询显示了上述School_Stu_Details表中每个科目中学生的最低分:

SELECT Stu_Subject, MIN (Stu_Marks) FROM School_Stu_Details GROUP BY Stu_Subject;

输出:

Stu_Subject MIN (Stu Marks)
English 65
Maths 92
Hindi 88
Computer 78

GROUP BY子句与MAX函数

我们还可以在结构化查询语言中使用MAX聚合函数与GROUP BY子句。

带有MAX聚合函数的GROUP BY子句的语法:

SELECT Column_Name_1, MAX(Column_Name) FROM Table_Name GROUP BY Column_Name_1;

MAX聚合函数与GROUP BY子句的示例

这个示例使用下面的School_Stu_Details表来理解使用SQL MAX聚合函数的GROUP BY子句的概念:

CREATE TABLE School_Stu_Details
(
Stu_ID INT NOT NULL, 
Stu_Name varchar(100),
Stu_Subject varchar(50),
Stu_Age INT, 
Stu_Marks INT
); 

以下的INSERT INTO语句将插入学校学生的记录:

INSERT INTO School_Stu_Details VALUES (101, Anuj, English, 20, 88);
INSERT INTO School_Stu_Details VALUES (102, Raman, Maths, 24, 98);
INSERT INTO School_Stu_Details VALUES (104, Shyam, Hindi, 19, 92);
INSERT INTO School_Stu_Details VALUES (107, Vikash, Computer, 20, 78);
INSERT INTO School_Stu_Details VALUES (111, Monu, English, 21, 65);
INSERT INTO School_Stu_Details VALUES (114, Jones, Hindi, 18, 93);
INSERT INTO School_Stu_Details VALUES (121, Parul, Maths, 20, 97);
INSERT INTO School_Stu_Details VALUES (123, Divya, English, 21, 89);
INSERT INTO School_Stu_Details VALUES (128, Hemant, Computer, 23, 90);
INSERT INTO School_Stu_Details VALUES (130, Nidhi, Hindi, 20, 88);
INSERT INTO School_Stu_Details VALUES (132, Priya, English, 22, 99);
INSERT INTO School_Stu_Details VALUES (138, Mohit, Maths, 21, 92);

下面的查询仅以表格形式显示学生的记录:

SELECT * FROM School_Stu_Details;
Stu_ID Stu_Name Stu_Subject Stu_Age Stu_Marks
101 Anuj English 20 88
102 Raman Maths 24 98
104 Shyam Hindi 19 92
107 Vikash Computer 20 78
111 Monu English 21 65
114 Jones Hindi 18 93
121 Parul Maths 20 97
123 Divya English 21 89
128 Hemant Computer 23 90
130 Nidhi Hindi 20 88
132 Priya English 22 99
138 Mohit Maths 21 92

以下查询显示了上述School_Stu_Details表中每个科目中学生的最高分数:

SELECT Stu_Subject, MAX (Stu_Marks) FROM School_Stu_Details GROUP BY Stu_Subject;

输出:

Stu_Subject MAX (Stu Marks)
English 99
Maths 98
Hindi 93
Computer 90

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程