当前位置:网站首页>Zero foundation self-study SQL course | having clause
Zero foundation self-study SQL course | having clause
2022-06-24 09:14:00 【Meow Ningyi】
Hello everyone , I'm Ning Yi .
Today's talk SQL Course No 16 course :HAVING Clause .
HAVING It is also a conditional filter statement , Put it in GROUP BY Behind .
Basic grammar :
SELECT < Field name >
FROM < Table name >
GROUP BY < Field name >
HAVING < filter >;1、HAVING And WHERE The difference between
HAVING And what we learned before WHERE Are all conditional filter statements , Not only do they have the same effect , The writing is also similar .
WHERE The comparison operator after the statement 、IN、BETWEEN、LIKE etc. , stay HAVING Can also be used in .
The essential difference between the two is :
WHERE Is in GROUP BY Filter the conditions before grouping , Aggregate functions cannot be followed .
HAVING Is in GROUP BY Condition filtering after grouping , It can be directly followed by the aggregate function .
example : stay Students In the table , Find the student number Sid Less than 8 The record of , And find out the number of male and female students each head teacher brings , The last output quantity is greater than 2 The record of .

Sample results :

Instance analysis : We want to output more than 2 The record of , The screening criteria are COUNT(*)>2,WHERE It can't be followed by an aggregate function , So this filter condition should be placed in HAVING Behind .
SELECT Tid,Ssex,COUNT(*)
FROM Students
WHERE Sid<8
GROUP BY Tid,Ssex
HAVING COUNT(*) > 2Some students here may also have questions , since HAVING Can also do conditional screening , Then we can put the top SQL Of the statement WHERE Get rid of , Filter criteria are merged into HAVING Medium is OK , It looks like this :
SELECT Tid,Ssex,COUNT(*)
FROM Students
GROUP BY Tid,Ssex
HAVING Sid<8 AND COUNT(*) > 2You can run this statement , Will report a mistake Unknown column 'Sid' in 'having clause’, We can't write like this .
because HAVING If the following field is an existing column in the table , Then this column must appear in SELECT Back .
For example, we will HAVING hinder Sid Change to Tid, The statement is correct , because Tid, Also in the SELECT Behind .
HAVING Tid<8 AND COUNT(*) > 22、 Field summary
HAVING Field 、SELECT Field , In addition to the above GROUP BY Field , The relationship between them in the following columns , You may feel a little confused , Let's summarize here as a whole , Straighten out your thinking .
Finally, it can be summarized as the following SQL sentence , among A、B、C Are all existing columns in the table .
SELECT A, C,COUNT(A)
GROUP BY A,B,C
HAVING A>1GROUP BY If it is A,B,C:
SELECT The following column , As long as it is included in A,B,C Medium will do . It can be A,B,C, It can also be A,B, It can also be A,C, It can also be A, But it can't be A,D.
SELECT If it is A,C:
HAVING The following column , As long as it is included in A,C Medium will do , It can be A,C, It can also be A, It can also be C, But it can't be B.
That is to say GROUP BY Columns that do not appear in ,SELECT Can't appear in ,SELECT Columns that do not appear in ,HAVING Can't appear in .
It's a little twisted , You can feel it according to the above example ~
The reason is that , It is related to the execution order of statements , We will explain the execution sequence of statements in detail in the following courses .
Homework : combination Students Table and Teachers surface , Find the number of male and female students each head teacher brings , The number of students in each group is greater than 2 The record of .

Sample results :

Homework analysis : Answer according to the example , The first column is the name of the head teacher ,Students There are only teacher numbers in the table Tid, So we need JOIN Connect Teachers surface , Get the name of the head teacher .
Also find out the number of male and female students each head teacher brings , adopt GROUP BY To the head teacher Tname, Student gender Ssex grouping , Re pass COUNT(*) Just count the quantity .
Finally, the number of students in each group is greater than 2 The record of , The screening criteria are COUNT(*)>2,WHERE It can't be followed by an aggregate function , So this filter condition should be placed in HAVING Behind .
SELECT
t.Tname AS " Teacher name ",
s.Ssex AS " Student gender ",
COUNT(*) AS " Number "
FROM Teachers AS t
JOIN Students AS s
ON t.Tid = s.Tid
GROUP BY t.Tname,s.Ssex
HAVING COUNT(*)>2;In the next class, I will explain in detail SQL The order in which statements are written and executed .
Click on Focus on , Update the course and notify the first time ~
边栏推荐
猜你喜欢

On the routing tree of gin

Squid代理服务器应用

NETRCA: AN EFFECTIVE NETWORK FAULT CAUSE LOCALIZATION之论文阅读
![[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)](/img/b3/b156d75c7b4f03580c449f8499cd74.png)
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)

玄铁E906移植----番外0:玄铁C906仿真环境搭建

【gdb调试工具】| 如何在多线程、多进程以及正在运行的程序下调试
![[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle](/img/db/e581087e550a2e460f12047685c48f.png)
[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle

CF566E-Restoring Map【bitset】

Squid proxy application

2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
随机推荐
读CVPR 2022目标检测论文得到的亿点点启发
Pytorch读入据集(典型数据集及自定义数据集两种模式)
On the routing tree of gin
"Unusual proxy initial value setting is not supported", causes and Solutions
Spark - the number of leftouterjoin results is inconsistent with that of the left table
Chapter 7 operation bit and bit string (III)
The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year
十二、所有功能实现效果演示
小白学习MySQL - 增量统计SQL的需求
荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
Qingcloud based "real estate integration" cloud solution
【LeetCode】415. String addition
听说你还在花钱从网上买 PPT 模板?
Essay - Reflection
KaFormer个人笔记整理
leetcode——错误的集合
怎么把mdf和ldf文件导入MySQL workbench中
Cmake命令之target_compile_options
Data middle office: middle office practice and summary
Framework tool class obtained by chance for self use