 What is going on everybody? My name is Alex Febregan. Today, we're going to be looking at the having clause. Now the having clause I feel is a little bit unappreciated in the SQL community. I feel like it doesn't get a lot of love. And so today I want to describe how to use it and what it's used for. So before we use the having clause, I want to set up our query here. We want to use an aggregate function in the group by statement, and then I will show you how to use this having clause. So let's look at the job title and let's look at the count of job titles and then down here we need to do group by job title and let's execute this and here is our job titles and here's the count of how many people have those job titles. So now let's say we want to look at all the jobs that have more than one person in that specific job. So let's do where is greater, oops, is greater than one and let's run that. And as you can see, we're going to get this message right here. Now let's read it. An aggregate may not appear in the where clause unless it is in a subquery contained in a having clause or a select list and the column being aggregated is an outer reference. What that is basically saying is is we cannot use this aggregate function in the where statement. We need to use a having clause. So let's get rid of this and let's say having the count of job title greater than one. I did the same thing again. And let's execute this and we're still going to get an error. Now, why are we getting that error? The reason is is because this having statement is completely dependent on the group by statement because we are performing this after it has been aggregated. So this having statement actually needs to go after the group by statement because we can't look at the aggregated information before it's actually aggregated in that group by statement. So now let's run this and it worked perfectly. So now we only have the jobs that have more than one employee for that job title. So now let's look at one more example. Let's do the average, let's say salary and let's get rid of this having clause real quick and just to look at this information. Let's do order by and we'll do average salary. So let's look at this and we have 36,000 to 65,000. So in the middle we have 44,500. So let's use this having statement and let's say the average of salary where it is greater than 45,000. And we actually need to put this right here, right after the group by and before the order by. So let's run this and see what we get and it worked perfectly. So now we're looking at the job titles that have an average salary of over 45,000 dollars. So there you go. That is the having clause. Definitely one that is good to know and is very useful in specific situations. Thank you guys so much for watching. I really appreciate it. If you liked this video or learned anything today, be sure to subscribe below and I'll see you in the next video.