Difference between where and having in sql server — Transcript
Full transcript
- 0:00this is part 98 of SQL Server tutorial
- 0:03in this video we'll discuss the
- 0:04difference between where and having
- 0:06Clauses in SQL server for the examples
- 0:09in this video we'll be using the sales
- 0:11table I've already created this table
- 0:13here is the SQL script to create and
- 0:15populate it with test data I'll have the
- 0:17script available on my blog in case you
- 0:19need it this sales table is going to
- 0:22have an entry for every sale that we
- 0:24make if you look at the data that we
- 0:26have here notice we have sold iPhone
- 0:28twice laptop twice and speakers once now
- 0:32we want to write a query that's going to
- 0:35calculate the total sales by product so
- 0:38the result of that query should be like
- 0:40this selling iPhones we made
- 0:43$1,500 laptops 1,400 and speakers 400 we
- 0:47can very easily achieve this using a
- 0:49group buy query we want to calculate
- 0:52total sales by product so we are using
- 0:55group Buy on this product column since
- 0:59we want the total sale amount we're
- 1:01using the summ aggregate function on the
- 1:03sale amount column so this query is
- 1:06going to produce this result I've
- 1:08already typed this query so when we
- 1:10execute this we should get the total
- 1:13sales by product now we want to find
- 1:17only those products where the total
- 1:20sales is greater than
- 1:22$1,000 that means we want to filter this
- 1:25speakers group we can very easily
- 1:28achieve this using having Clause let's
- 1:31look at that in action so I am going to
- 1:33use the having clause and we want to
- 1:36filter based on this total sales column
- 1:39and if you look at what is producing
- 1:40data for this total sales column it is
- 1:43this expression right here sum of sale
- 1:45amount so I'm going to use the same
- 1:48expression and specify our condition
- 1:51greater than 1,000 so when we execute
- 1:53this we should get only those products
- 1:56where the total sales is greater than
- 1:581,000 so here by using the having Clause
- 2:02with some aggregate function to filter
- 2:05the groups now what do you think is
- 2:07going to happen if we use the we Clause
- 2:10instead of having look at this when I
- 2:12execute it we get the syntax error
- 2:14straight away so this is one of the
- 2:17primary difference between wear and
- 2:19having Clauses wear Clause cannot be
- 2:22used with Aggregates whereas having can
- 2:25be used with Aggregates there are other
- 2:28differences as well
- 2:30where Clause filters rows before
- 2:33aggregate calculations are performed
- 2:35whereas having Clause filters rows after
- 2:37aggregate calculations are performed
- 2:39let's understand what we mean by this
- 2:42now we want to calculate total sales of
- 2:45iPhone and speakers so we know the
- 2:47products already we want to compute the
- 2:50total sales you know for those two
- 2:53products there are two ways we can
- 2:55achieve this we can achieve this either
- 2:57by using wear or having claw CL let's
- 3:00look at that in
- 3:02action so I'm going to
- 3:05use the wear Clause here and we want to
- 3:09compute the total sales
- 3:12for iPhones and speakers so where
- 3:16product and
- 3:18iPhone
- 3:20comma
- 3:22speakers so when we execute this notice
- 3:26that we get the total sales for iPhone
- 3:28and speakers now we can achieve the same
- 3:32thing using having as well so I'm going
- 3:34to make a copy of this query and
- 3:37then use
- 3:40having instead of where now let's
- 3:43execute these two queries look at that
- 3:46we get the same result now what is the
- 3:48difference between these two queries now
- 3:50with the first query this is going to
- 3:52filter all the rows it is going to
- 3:54retrieve only Iphone and speaker rows
- 3:57and then it's going to perform the
- 3:58aggregate calculation that is sum
- 4:01whereas with having here it's going to
- 4:03retrieve all the products it's going to
- 4:05perform the aggregate calculation on all
- 4:08the products and then it's going to
- 4:12filter the
- 4:14groups so clearly we can see that we
- 4:17have a performance impact when we use
- 4:20having Clause so from a performance
- 4:22standpoint having is going to be slower
- 4:24than where and should be avoided when
- 4:27possible because having here is just
- 4:30going to filter the groups whereas where
- 4:32filters you know all the rows it only
- 4:34gets iPhones and speakers and then
- 4:36performs the aggregation on those two
- 4:38products whereas in this case it
- 4:40performs the aggregation for all the
- 4:42products and then filters iPhones and
- 4:44speakers groups okay so having is
- 4:48definitely slower than where in this
- 4:50case and should be
- 4:52avoided so let's quickly recap the
- 4:55differences we Clause cannot be used
- 4:57with Aggregates whereas having can this
- 5:00means wear Clause is used for filtering
- 5:02individual rows whereas having Clause is
- 5:05used to filter groups where comes before
- 5:08Group by this means where Clause filters
- 5:11rows before aggregate calculations are
- 5:13performed having comes after Group by
- 5:16this means having CLA filters rows after
- 5:19aggregate calculations are performed so
- 5:21from a performance standpoint having is
- 5:23slower than where and should be avoided
- 5:25when possible there are also situations
- 5:28where you can use wear and having
- 5:30Clauses together in a select query in
- 5:33this case we Clause is applied first to
- 5:36filter individual rows the rows are then
- 5:39grouped and aggregate calculations
- 5:40performed and then the having Clause
- 5:43filter the groups so let's look at an
- 5:45example of using where and having
- 5:49together so here we are saying you know
- 5:51this is going to retrieve the sum of
- 5:54iPhone and speakers now let's say we
- 5:57want only that product group where the
- 6:01total sales is greater than $1,000 if
- 6:04that's the case I can use having and use
- 6:09our aggregate function sum of total I
- 6:11mean sale amount greater than 1,000 so
- 6:15when we execute this now we should only
- 6:18get the iPhone row because that's the
- 6:20only row where total sales is greater
- 6:22than 1,000 so in this query we're using
- 6:25both the wear and having
- 6:28clauses
- 6:31thank you for listening and have a great
- 6:34day
About this transcript
This page contains the full transcript of Difference between where and having in sql server by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 998 words across 152 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.