Splitting Groups using HAVING Clause

4 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€60.04
List Price:  €85.78
You save:  €25.73
£52.20
List Price:  £74.57
You save:  £22.37
CA$96.57
List Price:  CA$137.96
You save:  CA$41.39
A$107.16
List Price:  A$153.09
You save:  A$45.93
S$90.17
List Price:  S$128.82
You save:  S$38.65
HK$546.07
List Price:  HK$780.13
You save:  HK$234.06
CHF 56.24
List Price:  CHF 80.34
You save:  CHF 24.10
NOK kr701.61
List Price:  NOK kr1,002.34
You save:  NOK kr300.73
DKK kr448.11
List Price:  DKK kr640.19
You save:  DKK kr192.07
NZ$119.34
List Price:  NZ$170.50
You save:  NZ$51.15
د.إ257.03
List Price:  د.إ367.21
You save:  د.إ110.17
৳8,512.87
List Price:  ৳12,161.77
You save:  ৳3,648.89
₹6,162.88
List Price:  ₹8,804.49
You save:  ₹2,641.61
RM295.88
List Price:  RM422.70
You save:  RM126.82
₦107,536.13
List Price:  ₦153,629.63
You save:  ₦46,093.50
₨19,804.90
List Price:  ₨28,293.93
You save:  ₨8,489.02
฿2,263.93
List Price:  ฿3,234.32
You save:  ฿970.39
₺2,880.27
List Price:  ₺4,114.84
You save:  ₺1,234.57
B$382.90
List Price:  B$547.02
You save:  B$164.12
R1,233.84
List Price:  R1,762.70
You save:  R528.86
Лв117.53
List Price:  Лв167.91
You save:  Лв50.37
₩97,257.52
List Price:  ₩138,945.27
You save:  ₩41,687.75
₪235.73
List Price:  ₪336.77
You save:  ₪101.04
₱4,010.39
List Price:  ₱5,729.37
You save:  ₱1,718.98
¥10,397.50
List Price:  ¥14,854.21
You save:  ¥4,456.71
MX$1,309.19
List Price:  MX$1,870.35
You save:  MX$561.16
QR255.01
List Price:  QR364.32
You save:  QR109.30
P943.39
List Price:  P1,347.77
You save:  P404.37
KSh9,039.20
List Price:  KSh12,913.70
You save:  KSh3,874.50
E£3,401.11
List Price:  E£4,858.94
You save:  E£1,457.82
ብር9,999.68
List Price:  ብር14,285.86
You save:  ብር4,286.18
Kz63,827.73
List Price:  Kz91,186.39
You save:  Kz27,358.65
CLP$68,145.06
List Price:  CLP$97,354.26
You save:  CLP$29,209.20
CN¥499.08
List Price:  CN¥713
You save:  CN¥213.92
RD$4,408.74
List Price:  RD$6,298.47
You save:  RD$1,889.73
DA9,093.35
List Price:  DA12,991.06
You save:  DA3,897.70
FJ$158.28
List Price:  FJ$226.12
You save:  FJ$67.84
Q536.40
List Price:  Q766.32
You save:  Q229.91
GY$14,634.99
List Price:  GY$20,908.02
You save:  GY$6,273.03
ISK kr8,623.46
List Price:  ISK kr12,319.76
You save:  ISK kr3,696.30
DH633.06
List Price:  DH904.41
You save:  DH271.35
L1,154.10
List Price:  L1,648.78
You save:  L494.68
ден3,701.61
List Price:  ден5,288.25
You save:  ден1,586.63
MOP$562.68
List Price:  MOP$803.86
You save:  MOP$241.18
N$1,239.98
List Price:  N$1,771.48
You save:  N$531.49
C$2,575.56
List Price:  C$3,679.53
You save:  C$1,103.97
रु9,878.18
List Price:  रु14,112.30
You save:  रु4,234.11
S/247.37
List Price:  S/353.41
You save:  S/106.03
K293.47
List Price:  K419.26
You save:  K125.79
SAR262.62
List Price:  SAR375.19
You save:  SAR112.57
ZK1,658.21
List Price:  ZK2,368.97
You save:  ZK710.76
L304.89
List Price:  L435.57
You save:  L130.68
Kč1,468.04
List Price:  Kč2,097.30
You save:  Kč629.25
Ft23,641.10
List Price:  Ft33,774.46
You save:  Ft10,133.35
SEK kr660.62
List Price:  SEK kr943.78
You save:  SEK kr283.16
ARS$95,148.96
List Price:  ARS$135,932.91
You save:  ARS$40,783.95
Bs483.58
List Price:  Bs690.86
You save:  Bs207.28
COP$280,654.43
List Price:  COP$400,952.08
You save:  COP$120,297.65
₡35,381.84
List Price:  ₡50,547.65
You save:  ₡15,165.81
L1,844.66
List Price:  L2,635.35
You save:  L790.68
₲505,480.47
List Price:  ₲722,145.91
You save:  ₲216,665.44
$U2,801.15
List Price:  $U4,001.81
You save:  $U1,200.66
zł255.30
List Price:  zł364.73
You save:  zł109.43
Already have an account? Log In

Transcript

Hey guys, in this video we will be looking at splitting groups using the having clause. Now prior to this, you would have learned that if you wanted to see only records that meet a certain condition that you could use a where, where command right after or from our well after our Inner Joins, then you could see we're and then stipulate your condition. Now what if you were faced with a scenario where you wanted to see only students that are carrying the minimum or what three credits, so only students with three credits, then by you know, instinct, you would probably say where and then you would take that column. And then you stay there condition where it is less than or equal to three. All right, or Well, you only wanted to see those taking three. So when you say equal to three, and then you would execute this, but then that would probably snap work because then you'll see here where it's still bringing back everything.

All right. And the issue is that when you're dealing with group by or aggregated data, then the where clause is not how you're going to go about filtering or splitting up this group's data. So that is where we have a new keyword called having. So I would say having, and then that would allow me to see the sum. Because remember that this whole query is based on a sum. And I want to see where after summing up all of the credits being taken by students, I am left with three that is my condition.

So I can't see where some and I'm going to try it and just show you what would happen. So I'm going to say where the sum is equal to three. And if I attempt this, then I just get an error. It says it's an invalid user. of group function. That's because the summary doesn't get executed until the group by gets executed or it gets incremented as the group by does its work.

So to just say we're so dumb is really not going to cut it because this is in the middle of the clumping exercise, it wouldn't be clean. So we have a keyword called having that allows us to stipulate the same kind of condition, except we can do it on the aggregate column this time. So we can say having some equals three. And then by doing that, after summing up, it will say Okay, give me back only those that meet this condition. And so we see here, we're only left with three. If I said six, then it would only bring back those with six credits.

And this having clause can be applied anywhere you have an aggregate function. If I go back to one of the previous queries where we Had min and max, I can see, give me back all of that data, but I only want where the minimum grade is greater than 50. All right, so I only want back all of the cloud all of this data where the minimum grade is 50 or more. And then that would, oh, that is actually giving me a tight Miss much error. I'm sorry, from the previous example. I didn't fill out the aliases.

And so that was causing the problem. So if I attempt this again, then there we go. So I'm only seeing the records that meet this condition where the minimum grade returned was over 50. And so you can apply this having clause if you feel the need that are having grouped, having performed some, you still need to filter out some of the the returns are the results that come back. So you can just use that having keyword and add your aggregate column and whatever condition it must meet. And you see that if I attempt to put in a non aggregate column here, it does not work.

So it will give you something on both an unknown column. And that's really because it can only look at the aggregate value as opposed to the value of being aggregated. And that is the end of this video. I trust you learn something valuable with the having clause and I know you put it to good use

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.