当前位置:网站首页>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 ~
边栏推荐
- L01_一条SQL查询语句是如何执行的?
- 12、 Demonstration of all function realization effects
- On the routing tree of gin
- 1844. replace all numbers with characters
- 华为路由器:GRE技术
- Mba-day25 best value problem - application problem
- 2021-05-20computed and watch applications and differences
- 深入解析 Apache BookKeeper 系列:第三篇——读取原理
- EasyExcel单sheet页与多sheet页写出
- PM2 deploy nuxt3 JS project
猜你喜欢
![[redis implements seckill business ①] seckill process overview | basic business implementation](/img/a3/9a50e83ece43904a3a622dcdb05b3c.png)
[redis implements seckill business ①] seckill process overview | basic business implementation

【bug】@JsonFormat 使用时出现日期少一天的问题

学习太极创客 — ESP8226 (十二)ESP8266 多任务处理

Epidemic situation, unemployment, 2022, we shouted to lie down!

Leetcode -- wrong set

“论解不了数独所以选择做个数独游戏这件事”

MBA-day25 最值问题-应用题

The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year

uniapp 开发多端项目如何配置环境变量以及区分环境打包

【ES6闯关】Promise堪比原生的自定义封装(万字)
随机推荐
GradScaler MaxClipGradScaler
Chapter 7 operation bit and bit string (III)
Data middle office: middle office architecture and overview
当程序员被问会不会修电脑时… | 每日趣闻
2020 China's provinces and cities, three-level linkage data, data agencies (data from the official website of the National Bureau of Statistics)
4275. Dijkstra序列
Applet wx show
4274. suffix expression
牛客网 字符串变形
【LeetCode】541. Reverse string II
Linux (centos7.9) installation and deployment of MySQL Cluster 7.6
目标检测系列——Fast R-CNN
零基础自学SQL课程 | SQL语句语法顺序与执行顺序
tcpdump抓包实现过程
Every (), map (), forearch () methods. There are objects in the array
Opencv daily function structure analysis and shape descriptor (7) finding polygon (contour) / rotating rectangle intersection
4274. 后缀表达式
Idea another line shortcut
【Redis实现秒杀业务①】秒杀流程概述|基本业务实现
零基础自学SQL课程 | 子查询