当前位置:网站首页>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 ~
边栏推荐
- Data middle office: middle office practice and summary
- 普通人没有学历,自学编程可以月入过万吗?
- Redis实现全局唯一ID
- Linux (centos7.9) installation and deployment of MySQL Cluster 7.6
- Jincang KFS replicator installation (oracle-kes)
- Qingcloud based "real estate integration" cloud solution
- Get post: do you really know the difference between requests??????
- When to use RDD and dataframe/dataset
- 【Redis实现秒杀业务①】秒杀流程概述|基本业务实现
- Opencv maximum filtering (not limited to images)
猜你喜欢
Depens:*** but it is not going to be installed

When programmers are asked if they can repair computers... | daily anecdotes

Data middle office: middle office architecture and overview

【LeetCode】387. First unique character in string

浮点数表示法(总结自CS61C和CMU CSAPP)

Target detection series fast r-cnn

解决:jmeter5.5在win11下界面上的字特别小

【LeetCode】541. Reverse string II

零基础自学SQL课程 | 相关子查询

A tip to read on Medium for free
随机推荐
Linux MySQL installation
520. detect capital letters
Double pointer analog
小白学习MySQL - 增量统计SQL的需求
The border problem after the focus of input
Some common pitfalls in getting started with jupyter:
Squid proxy application
从华为WeAutomate数字机器人论坛,看政企领域的“政务新智理”
Remote connection of raspberry pie without display by VNC viewer
支持向量机(SVC,NuSVC,LinearSVC)
4274. suffix expression
Qingcloud based "real estate integration" cloud solution
How to import MDF and LDF files into MySQL workbench
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)
目标检测系列——Fast R-CNN
[e325: attention] VIM editing error
4275. Dijkstra序列
[Niuke] convert string to integer
[quantitative investment] discrete Fourier transform to calculate array period
【gdb调试工具】| 如何在多线程、多进程以及正在运行的程序下调试