Rollup in SQL Server — Transcript
Full transcript
- 0:00this is part 102 of SQL Server tutorial
- 0:03in this video we'll discuss rollup and
- 0:05SQL Server rollup is used to do
- 0:08aggregate operation on multiple levels
- 0:11in a hierarchy let's understand this
- 0:13with an example we have this employees
- 0:15table here notice we have got employees
- 0:18in three different countries us UK and
- 0:21India now based on these employees table
- 0:24we want to write a query that's going to
- 0:26retrieve salaries grouped by country
- 0:29along with grand total so the result of
- 0:32our query should be like this notice for
- 0:34Indian employees we are paying 12,000
- 0:36for UK employees 17,000 us employees
- 0:4022,500 and then we have the grand total
- 0:43and if you look at the grand total Row
- 0:45the country column is null that's
- 0:46because for Grand totals we are not
- 0:49grouping by any specific column and then
- 0:52we have the grand total there are
- 0:53several ways to produce this result but
- 0:56the easiest and best way is by using
- 0:58rollup with groupi So within our result
- 1:01set we want two columns the country and
- 1:04total salary so we are selecting country
- 1:07column from employees table and then we
- 1:09are applying the sum aggregate function
- 1:11on the salary column and we giving that
- 1:14an alas total salary from employees
- 1:17Group by and we're using roll up so we
- 1:20want to roll up on the country column so
- 1:23that's going to give us subtotal as well
- 1:25I have this exact same query already
- 1:27typed so when we execute this we get the
- 1:30salaries grouped by country as well as
- 1:33the grand total now we discuss that
- 1:36there are other ways of doing this as
- 1:38well now we can rewrite this group by
- 1:40query in a slightly different fashion so
- 1:43I can also write this query like this
- 1:45group
- 1:47by country column and I can use with
- 1:51rollup so now when we execute these two
- 1:54queries we should get exactly the same
- 1:56result now we can also achieve this by
- 2:00using Union all with groupi so let's
- 2:03make a copy of
- 2:06this so now when I remove this with rule
- 2:10up from this query and when we execute
- 2:13this we are going to get the salaries
- 2:15grouped only by country now if we want
- 2:19the grand total I can write another
- 2:21query so let's make a copy of this
- 2:24one now within grand total the country
- 2:27column is going to be null so let's
- 2:30select null sum of salary as total
- 2:33salary from employees and for grand
- 2:35total we are not going to group on any
- 2:37specific column so I'm going to remove
- 2:39Group by so when we execute this query
- 2:42we are going to get the grand total now
- 2:44we will apply the union all
- 2:47operator
- 2:49between the group buy and our grand
- 2:52total
- 2:53query so when we execute this we get
- 2:56exactly the same
- 2:57result now we can also use grouping sets
- 3:00to achieve exactly the same thing so
- 3:03let's again make a copy of this
- 3:08query so we want the same two columns
- 3:11from employees table Group by now we
- 3:15want to use grouping sets and then we
- 3:18Define our groups so we want to group by
- 3:23country so when we execute just this
- 3:25grouping sets query we're going to get
- 3:27salaries grouped only by country notice
- 3:30we don't have grand total now to get
- 3:33grand
- 3:35total we are going to use empty
- 3:37parentheses so this is going to specify
- 3:39that we want the grand total so now when
- 3:41we execute this we should get salaries
- 3:43grouped by country plus the grand
- 3:47total so there is several ways of
- 3:49achieving the same result but the
- 3:51easiest and best way is by using rollup
- 3:54with
- 3:56groupi now let's look at another example
- 4:00so now we want to group salaries by
- 4:03country and then by gender and we also
- 4:06want subtotals at Country level as well
- 4:10as the grand total so if you look at the
- 4:12result set right here notice for Indian
- 4:14female employees we are paying 4,000 uh
- 4:17for Indian male employees 8,000 and for
- 4:19all Indian employees so that's the
- 4:21subtotal for India country so we're
- 4:24paying 12,000 similarly for UK female
- 4:27employees 5,000 UK male employees 12,000
- 4:30when we add them together 177,000 so for
- 4:32all UK employees it's 177,000 so we have
- 4:35the subtotals at Country level same is
- 4:38the case for USA as well and then in the
- 4:42end we also have the grand total if you
- 4:44look at the subtotal rows notice gender
- 4:47column is
- 4:48null whereas if you look at the grand
- 4:51total row both country and gender column
- 4:53is null because for grand total we are
- 4:55not grouping on any specific column so
- 4:59again to achieve this result there are
- 5:01several ways of doing it but the best
- 5:03and easiest way is by using roll up with
- 5:06group I so this query is very identical
- 5:09to the previous query the only
- 5:11difference is in the select list so
- 5:13within our result set we want the gender
- 5:14column as well so along with the country
- 5:16column we included the gender column and
- 5:19then in our to our rollup function along
- 5:21with country we're also passing the
- 5:24gender column those are the only two
- 5:26differences okay so let's quickly look
- 5:28at that in action So within our select
- 5:31list we want the gender
- 5:34column and we also want to roll
- 5:37up on gender column so when we execute
- 5:40this we should get the salaries grouped
- 5:43by country and the subtotal for country
- 5:47and then basically the grand total as
- 5:49well very easy now we can also rewrite
- 5:53this group by query like this so within
- 5:57our result set we want
- 6:00gender and here we will say Group by
- 6:03country gender with rollup again you
- 6:06know when we execute these two qu is the
- 6:08same results so if you look at the
- 6:09messages look at that 10 rows 10 rows
- 6:12and the output is exactly the same
- 6:15thing all right now let's see how to do
- 6:20the same thing using Union all so the
- 6:22first query here you know this is just
- 6:25going to produce total salaries grouped
- 6:27by country but within our result set we
- 6:30also want the gender column so let's
- 6:32specify the gender column right
- 6:36here and then we want to group
- 6:39by gender as well so when we execute
- 6:42this we should get the salaries grouped
- 6:44by country and by agenda now we want the
- 6:49salaries grouped by country that is the
- 6:51country subtotals so in this case we are
- 6:54going to select
- 6:56country gender is going to be null and
- 6:59then of salary from employees and then
- 7:02we're going to
- 7:03use Group by just country so this one is
- 7:08going to give us salaries grouped just
- 7:11by country so these are like country
- 7:14subtotals and then finally we want the
- 7:16grand total and getting grand total is
- 7:18very easy let's use another Union all
- 7:21operator and let's make a copy of
- 7:24this now when we want the grand total we
- 7:27are not going to group by any specific
- 7:29column so we will not have the group by
- 7:32country is going to be null gender is
- 7:35going to be null and we get the sum of
- 7:37salary so now when we execute these
- 7:41three queries we should get the same
- 7:43result but one thing to keep in mind
- 7:45here is the order of the Ross in the
- 7:47result set is not the same to control
- 7:50the order you can use order by
- 7:56Clause we can also use the grouping sets
- 7:58to do the same thing so at the moment
- 8:00when we execute this we are getting only
- 8:02the salaries grouped by country and then
- 8:05the grand total now first of all within
- 8:08our result set we want gender as well so
- 8:10within our select Clause I'm going to
- 8:12specify gender column and then we want
- 8:15to first group by country and gender
- 8:19okay and then we want to
- 8:21group just by country so that's going to
- 8:24give us the sub totals for country and
- 8:27then we want the grand total so when we
- 8:30execute this again the result should be
- 8:32identical look at that we have the
- 8:34salaries grouped by country and agenda
- 8:37then the country subtotal similarly for
- 8:39UK and USA and finally the grand
- 8:45total so we have those queries here
- 8:47using rule up Clause with Group by using
- 8:50Union all with Group by and finally
- 8:53using grouping
- 8:55sets thank you for listening and have a
- 8:58great day
About this transcript
This page contains the full transcript of Rollup in SQL Server by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,421 words across 200 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.