Note: This post is related to the last one ‘Day 9’ so if you haven’t read that, please read that first.
Hey Guys, hope you are good.
I hope you guys are following my posts and understanding the concepts in SQL.
Today, we’ll be looking at the HAVING clause in SQL. Its similar to WHERE as a filter but there’s a difference. What is it? Let’s see
Use Of HAVING In SQL
HAVING is used as a filter like WHERE but the difference is, it is used on aggregates. So if I was to add this following statements in HAVING it would give me an error.
select total_sales from sample.video_games_sales
HAVING sales > 10
So the error is unknown column in the HAVING clause.
Now let’s add this HAVING clause to our code in yesterday’s post.
Yesterday, we used GROUP BY to group our results using the console column. Today, we’ll be looking at the total sales by console having total sales greater than 2000 or 2 Billion if you will. (Our sales column is sales in Millions, so 2000 Mil is 2 Bil)
So, lets get started.
There we have it, only 4 rows available in the results for sales above 2000. Previously we had 16 rows and now we have 4.
But wait, what if I wanted to see consoles that had sales between a 1000 and 2000 Million. Lets see!
We can see that we have two consoles available who have game sales between 1000 and 2000.
Now, the cool thing is, as
once said write SQL as if you’re writing simple English. So how would you write HAVING total_games_sold between 1000 and 2000?This is a question to you, readers.
Hint: The answer is in the question.
Exercise
Here’s what you need to do today
Load the UK Train Rides dataset from Maven Analytics
Show count of transactions grouped by Departure Station having transactions more than a 100.
Share your queries in the chat thread related to Day 10.
Do share it guys, its for your own benefit and who knows maybe you land a job just by commenting here on my post.
I am a BI Engineer working at a Data And AI Firm. I’m trying to help newbies in Data land their first data job and impact as many fellow data professionals as I can. Here’s my contact info:
ajmal7809@gmail.com
bideveloper_ (Discord)
SELECT [Departure Station], count(*) as Total_Departure
from railway
GROUP BY [Departure Station]
HAVING COUNT([Departure Station]) > 100
ORDER BY [Departure Station] DESC