Over clause in SQL Server — Transcript
Full transcript
- 0:00this is part 108 of SQL Server tutorial
- 0:03in this video we'll discuss the power
- 0:05and use of Over clause in SQL Server the
- 0:08Over clause combined with Partition by
- 0:11is used to break up table data into
- 0:13partitions we have the syntax here first
- 0:15we specify a SQL Server function we can
- 0:17use any of these functions sum min max
- 0:20Etc and then we use the over keyword
- 0:23followed by that Partition by and then
- 0:26by whatever columns we want to partition
- 0:28that table data we have an example here
- 0:30let's say we have employees table and
- 0:32within that table we have gender column
- 0:34so here we using the count aggregate
- 0:37function over Partition by gender so
- 0:40this is basically going to tell
- 0:42partition the employees table data by
- 0:45gender column so it's going to divide
- 0:47that employees data into two partitions
- 0:50male and female and then this count
- 0:52aggregate function is applied over each
- 0:54of those partitions meaning we will get
- 0:56count of female and male employees let's
- 0:59look at this action so here we have our
- 1:02employees table here is the gender
- 1:04column now what we want to do is based
- 1:06on this table we want to write a query
- 1:08which is going to give us aggregated
- 1:10data by gender column so we want the
- 1:13count of female and male employees
- 1:15similarly we want to find out what is
- 1:17the average salary for female employees
- 1:19and male employees and similarly their
- 1:21men and Max salaries so at the moment
- 1:23notice we have got four female and six
- 1:25male employees now we can very easily
- 1:28achieve this using a simple group Group
- 1:30by query we want the data to be
- 1:32aggregated by gender column so we are
- 1:35using Group by on the gender column and
- 1:39to get the totals for genders we are
- 1:42using the count function to get the
- 1:43average we are using average to get
- 1:46minimum of salary we are using Min and
- 1:48to get the max we are using Max okay a
- 1:50simple Group by query I have this exact
- 1:53same query already typed here so when we
- 1:55execute this we get the result that we
- 1:57expect so straightforward so the date
- 2:00that we see in the result set is
- 2:02aggregated data now what if we want
- 2:05along with that aggregated data we also
- 2:08want non-aggregated values like name and
- 2:11salary so if you look at this aggregated
- 2:14data notice that we have four female
- 2:16employees and six male employees now in
- 2:19the result set right here we have the
- 2:22names and salaries of individual
- 2:24employees that's you know name and
- 2:26salary is not aggregated okay so what we
- 2:29want is this non-aggregated values along
- 2:32with these aggregated data for all
- 2:34female employees look at that the grand
- 2:36total I mean the gender total is four
- 2:38for all male employees it is six and
- 2:41average um salary for female employees
- 2:44is 5375 so it's the same for all female
- 2:47employees similarly 5,000 is the average
- 2:49salary for male employees so the
- 2:53aggregated data is repeated for each
- 2:55employee so we want aggregated and
- 2:58non-aggregated data now the first thing
- 3:00that comes to our mind is well why don't
- 3:02we use name and salary column in the
- 3:04select list if we do that since this a
- 3:07group by query and name and salary are
- 3:10not in group by clause and we are not
- 3:13using any of the aggregate functions on
- 3:15those two columns so we will get this
- 3:17error so it's saying employees. name so
- 3:20name column is coming from employees
- 3:22table so column employees. name is
- 3:25invalid in the select list because it's
- 3:27not contained in either an aggregate
- 3:30function or in the group by Clause okay
- 3:34let's actually look at that in action so
- 3:36if I try and include name comma salary
- 3:40columns and then if we try to execute
- 3:42this query we are going to get exactly
- 3:43the same error that we have seen on the
- 3:46slide so one way to achieve this result
- 3:49is by including all these aggregations
- 3:52in a subquery and then combining that
- 3:55subquery with the outer query let's
- 3:57actually look at that in action so what
- 3:59I'm going to to do is take this subquery
- 4:02so when we execute this we get um you
- 4:05know average min max salaries and the
- 4:08count aggregated by gender so I'm going
- 4:10to put this in a subquery so let's turn
- 4:14this into a
- 4:16subquery and let's call this genders
- 4:20okay and then I'm going to use an outer
- 4:22query so from the outer query we want
- 4:25the name of the employee we want their
- 4:28salary and we also want the agenda and
- 4:31we want that from employees table so I'm
- 4:36going to
- 4:38join this outer query with the subquery
- 4:41so let's
- 4:43use in a
- 4:45join so we joining employees with gender
- 4:48so we need to specify the join condition
- 4:52so on genders do gender so the common
- 4:57column between employees table and this
- 4:59genders result set is gender so genders
- 5:03do genda
- 5:06equals employees dot
- 5:10gender all right so what do we want in
- 5:13the result we want name salary gender we
- 5:15want gender totals average salary Min
- 5:17salary and maximum salary these will
- 5:20come from our subquery because that's
- 5:22what contains our aggregation so when we
- 5:25execute this we should get this result
- 5:28so what I'm going to do here is use this
- 5:31genders result set and it has got gender
- 5:34total similarly genders do average
- 5:40salary
- 5:42and genders do minimum
- 5:46salary genders do maximum salary all
- 5:50right let's go ahead and execute our
- 5:52query now we get an error and it says
- 5:55ambiguous column name gender so if you
- 5:56look at this gender column it's present
- 5:58both in the employ employees table and
- 6:00this gender result set so we'll have to
- 6:03tell from where we want that column we
- 6:05could either use employees table or this
- 6:07gender result set it doesn't really
- 6:09matter for the purpose of this query I'm
- 6:11going to use employees table all right
- 6:14so let's execute this query now and look
- 6:17at the result this is exactly what we
- 6:19see on the slide all right but look at
- 6:22the amount of code we have to write to
- 6:24produce this simple result set now it'll
- 6:27be much easier if we use Over clause
- 6:30with Partition bu let's look at that
- 6:32query
- 6:35now so what do we want when the result
- 6:37set we want actually I'm going to make a
- 6:40copy of this so we
- 6:42want name
- 6:46salary
- 6:49gender from employees
- 6:52table now along with the gender we want
- 6:56gender total average salary men and Max
- 6:58salaries
- 7:00so to get gender totals I'm going to use
- 7:03the count function and I'm going to
- 7:05specify a column name here count of
- 7:08gender we will use the over keyword over
- 7:12and then followed by open parenthesis
- 7:14close
- 7:15parenthesis Partition by gender column
- 7:20so what is this going to do this is
- 7:22going to partition this employees table
- 7:24data by gender so we will have two
- 7:26partitions male and female and then this
- 7:29count function will be applied for each
- 7:30of those partitions so we are going to
- 7:33get count of male and female employees
- 7:36and let's give this column an alas let's
- 7:38call it genders
- 7:42total so when we execute
- 7:45this look at what we get we get name of
- 7:48the employee salary gender column and
- 7:50gender totals four female employees and
- 7:52six male employees now getting average
- 7:55salary Min salary and maximum salary
- 7:57it's pretty much identical so I'm
- 7:59actually going to make a copy of this we
- 8:02I'm going to now use the average
- 8:04function but we want the average of
- 8:06salary column so average of salary over
- 8:10Partition by gender and I'm going to
- 8:12call this
- 8:14AVG salary okay so similarly let's go
- 8:19ahead and do the same thing for Min and
- 8:20Max so minimum of salary and let's call
- 8:24the column Min salary and here it's
- 8:27going to be Max salary and the function
- 8:30that we are going to use is
- 8:32Max all right let's get rid of this last
- 8:36comma Now when we execute this query
- 8:39look at that we get exactly the same
- 8:41result but look at the amount of code
- 8:43that we have to write this query is also
- 8:46easier on the eyes it's very easy to
- 8:49understand so partition the table data
- 8:51by gender column and then apply these
- 8:54aggregate
- 8:56functions which you know will produce
- 8:59the result that we
- 9:01want so here is our over with Partition
- 9:06by
- 9:08query thank you for listening and have a
- 9:11great day
About this transcript
This page contains the full transcript of Over clause in SQL Server by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,438 words across 210 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.