SQL 如何使用HAVING

SQL 如何使用HAVING

在这个SQL页面中,您将学习到HAVING关键字是什么,以及如何在结构化查询语言中实现它。

HAVING是什么?

HAVING是SQL中的一个关键字,它基于特定的单个或多个条件选择通过GROUP BY关键字过滤的行。

在SQL中使用它是因为SQL软件不允许您在聚合函数中使用WHERE子句。在SQL中,我们只能在SELECT查询中使用HAVING子句。

HAVING子句的语法

SELECT First_Column_Name, Second_Column_Name, ........, Nth_Column_Name FROM Table_Name GROUP BY Column_Name HAVING Single or Multiple Conditions; 

在上述语法中,GROUP BY子句创建了一组相同的行,HAVING子句将条件应用于结果分组。HAVING子句在表的列或字段上执行其操作。如果在HAVING语法中不能使用GROUP BY子句,则HAVING子句的工作方式类似于SQL WHERE子句。

如果您想了解如何在SQL表中使用HAVING子句,则必须按照以下步骤进行操作:

  1. 创建简单的数据库和表。
  2. 将数据插入表中。
  3. 在没有HAVING子句的情况下查看插入的数据。
  4. 使用HAVING子句。

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

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

下面的查询将创建Fortis_Hospital数据库:

CREATE Database Hospital;

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

CREATE TABLE table_name
(
First_column_Name data type (size of the First_column),  
Second_column_Name data type (size of the Second_column),  
Third_column_Name data type (size of the Third_column),  
...  
Nth_column_Name data type (size of the Nth_column)
);

以下查询在Fortis_Hospital数据库中创建Doctor_Info表:

CREATE TABLE Doctor_Info
(
Doctor_ID Int PRIMARY KEY,  
Doctor_Name VARCHAR (100),  
Doctor_Specialist VARCHAR (80),  
Doctor_Salary INT NOT NULL,
Doctor_Gender Varchar (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);  

以下查询插入在Fortis_Hospital工作的医生的记录

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

步骤3:不使用HAVING子句查看表的数据

以下查询以无序方式显示医生的记录:

SELECT * FROM Doctor_Info;

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

Doctor_ID Doctor_Name Doctor_Specialist Doctor_Salary Doctor_Gender Doctor_Country
1035 Jones Malaria_Specialist 25000 Male United Kingdom
1015 Marry Diabities_Specialist 30000 Female United State
1003 Harry Fever_Specialist 29000 Male United Kingdom
1044 Ella Cancer_Specialist 35000 Female United State
1025 Moria Corona_Specialist 34000 Other Europe

第四步:使用HAVING子句

以下SQL SELECT查询显示了使用HAVING子句的表格记录:

SELECT Doctor_Gender, SUM (Doctor_Salary) FROM Table_Name GROUP BY Doctor_Gender HAVING SUM (Doctor_Salary) < 60000;

输出:

Doctor_Gender SUM(Doctor_Salary)
Male 54000
Other 34000

使用SQL ORDER BY子句的HAVING子句

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

HAVING子句与ORDER BY子句的语法

SELECT Function_Name (Column_Name) FROM Table_Name GROUP BY Column_Name HAVING Column_Name ORDER BY Column_Name DESC/ASC;

使用ORDER BY子句的HAVING子句示例

以下查询在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)); 
(2224, Physics, Aman, 103),
(2248, Computer, Bhanu, 101),
(2208, Hindi, Sonu, 104)

以下查询显示了Subject表的记录:

SELECT * FROM Subject;
Subject_ID Subject_Name Subject_Teacher Student_ID
2248 Computer Bhanu 101
2221 Biology Punit 103
2201 Chemistry Suresh 101
2224 Maths Rohit 103
2248 Computer Bhanu 105
2208 Hindi Sonu 104
2221 Biology Punit 104
2224 Physics Aman 108
2248 Computer Bhanu 106
2208 Maths Sonu 109

下面的SQL查询使用了带有HAVING子句的SQL ORDER BY子句:

SELECT Subject_ID, COUNT(Student_ID) >=2 FROM Subject GROUP BY Subject_ID HAVING COUNT(Student_ID) >=2 ORDER BY Subject_ID ASC;

输出:

Subject_ID COUNT(Student_ID) >=2
2208 2
2221 2
2248 3

具有MIN函数的HAVING子句

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

MIN函数的GROUP BY子句语法:

SELECT Column_Name_1, MIN(Column_Name) FROM Table_Name HAVING Column_Name_1;

示例使用HAVING子句的MIN聚合函数

此示例将使用以下的College_Stu_Details表来理解使用MIN聚合函数的HAVING子句的概念:

CREATE TABLE College_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 College_Stu_Details VALUES (1001, Anuj, English, 20, 70);
INSERT INTO College_Stu_Details VALUES (1002, Raman, Maths, 24, 68);
INSERT INTO College_Stu_Details VALUES (1040, Shyam, Hindi, 19, 92);
INSERT INTO College_Stu_Details VALUES (1007, Vikash, Computer, 20, 78);
INSERT INTO College_Stu_Details VALUES (1011, Monu, English, 21, 65);
INSERT INTO College_Stu_Details VALUES (1014, Jones, Hindi, 18, 93);
INSERT INTO College_Stu_Details VALUES (1021, Parul, Maths, 20, 97);
INSERT INTO College_Stu_Details VALUES (1023, Divya, English, 21, 89);
INSERT INTO College_Stu_Details VALUES (1028, Hemant, Computer, 23, 90);
INSERT INTO College_Stu_Details VALUES (1030, Nidhi, Hindi, 20, 88);
INSERT INTO College_Stu_Details VALUES (1032, Priya, English, 22, 99);
INSERT INTO College_Stu_Details VALUES (1038, Mohit, Maths, 21, 92);

以下查询只是在屏幕上以表格形式显示学生的记录:

SELECT * FROM College_Stu_Details;
Stu_ID Stu_Name Stu_Subject Stu_Age Stu_Marks
1001 Anuj English 20 70
1002 Raman Maths 24 68
1004 Shyam Hindi 19 92
1007 Vikash Computer 20 78
1011 Monu English 21 65
1014 Jones Hindi 18 93
1021 Parul Maths 20 97
1023 Divya English 21 89
1028 Hemant Computer 23 90
1030 Nidhi Hindi 20 88
1032 Priya English 22 99
1038 Mohit Maths 21 92

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

SELECT Stu_Subject, MIN (Stu_Marks) FROM College_Stu_Details HAVING Stu_Subject;

输出:

Subject_ID MIN (Stu_Marks)
English 65
Maths 92
Hindi 88
Computer 78

使用MAX函数的HAVING子句

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

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

SELECT Column_Name_1, MAX(Column_Name) FROM Table_Name HAVING Column_Name_1;

MAX聚合函数与HAVING子句的示例

这个示例将使用下面的College_Stu_Details表来理解HAVING子句与SQL MAX聚合函数的概念:

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

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

SELECT * FROM College_Stu_Details;
Stu_ID Stu_Name Stu_Subject Stu_Age Stu_Marks
101 Anuj English 20 70
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

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

SELECT Stu_Subject, MAX (Stu_Marks) FROM College_Stu_Details HAVING Stu_Subject;

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程