Grouping Sets in SQL Server — Transcript
Full transcript
- 0:00this is part 101 of SQL Server tutorial
- 0:03in this video we'll discuss grouping
- 0:04sets in SQL Server grouping sets is a
- 0:07new feature introduced in SQL Server
- 0:092008 let's understand grouping sets with
- 0:12an example we'll be using this employees
- 0:14table in this demo I've already created
- 0:16this table here is the SQL script to
- 0:18create and populate it with test data
- 0:20I'll have the script available on my
- 0:21blog in case you need it now based on
- 0:24this employees table we want to write a
- 0:26query that is going to calculate some of
- 0:29salary
- 0:30grouped by country column first and then
- 0:33by gender column so the result of a
- 0:35query should be like this so if you look
- 0:38at the data here notice for female
- 0:41employees in India we are paying 4,000
- 0:44as the total salary for UK female
- 0:46employees we paying 5,000 for us female
- 0:48employees
- 0:5012,500 so here the salaries are grouped
- 0:53by country and then by gender to achieve
- 0:56this we can write a simple Group by
- 0:57query as you can see here within the set
- 1:00we want these three columns country
- 1:02genda and sum of salary So within our
- 1:05select list here we have country genda
- 1:08we are applying the sum aggregate
- 1:09function on the salary column and we are
- 1:12giving that column and alas total salary
- 1:15from employees table we first grouping
- 1:17by country and then by agenda I have
- 1:20this group by query already typed so if
- 1:22we execute this query we should get the
- 1:25salaries grouped by country and then by
- 1:27genda so along with this we also want
- 1:31the sum of salaries grouped just by
- 1:34country so if you look at the three rows
- 1:36that are with green background color
- 1:39notice that for Indian employees we are
- 1:41paying 12,000 now if you look at India
- 1:44female employees we are paying 4,000 for
- 1:46India male employees 8,000 so if you add
- 1:49those two rows together 4 + 8 is 12,000
- 1:52so for all Indian employees it 12,000
- 1:55similarly if you look at UK for UK
- 1:57female employees it is 5,000 for UK
- 2:00employees it's 12,000 12 + 5 is 17 so UK
- 2:04total salary by country is
- 2:06177,000 now to produce these three rows
- 2:09that are with green background color you
- 2:11know we can use another group by query
- 2:14as you can see here so we want the
- 2:16country So within the select list we
- 2:17want country now since we are grouping
- 2:20here just by country gender is going to
- 2:23be null so we are selecting null for
- 2:26gender column and then the sum of salary
- 2:29from employees and we want to group just
- 2:31by country and between these two group
- 2:33by queries we are applying Union all
- 2:35operator so this query is going to
- 2:38produce this result set let's quickly
- 2:40look at that in action so I'm going to
- 2:42make a copy of this
- 2:44query and then I'm going to make the
- 2:46modifications so we want country gender
- 2:49is going to be null because we are going
- 2:51to group just by uh country so from our
- 2:54group by Clause I'm going to remove
- 2:55jenda and between these two queries we
- 2:58will apply the union all operator but
- 3:00before that if we execute just this
- 3:02query notice that we get the total
- 3:04salaries grouped only by country whereas
- 3:07when we execute this query we get the
- 3:10first six rows that is salaries grouped
- 3:12by country and then by agenda now if we
- 3:15apply Union all operator between these
- 3:18two group by queries we get the result
- 3:20set that we see on the slide here now in
- 3:24addition to these two result sets we
- 3:26also want to compute total salary is
- 3:30grouped just by gender column since here
- 3:33we're grouping just by gender country
- 3:36column is going to be null and to
- 3:38achieve this as you might have already
- 3:39guessed we're going to use another
- 3:42groupby query and that groupby query is
- 3:44going to be very similar to the group by
- 3:47query that we have used to produce the
- 3:49country groupings so I'm going to make a
- 3:52copy of this second group by
- 3:56query so now country is going to be null
- 3:59because we're going to group just by
- 4:01gender and we want the gender column and
- 4:05within our group by we're going to use
- 4:07just the gender column so when we
- 4:09execute this we should get the total
- 4:11salaries grouped by gender and now
- 4:14between all these three Group by queries
- 4:17I'm going to apply Union all operator so
- 4:19when we execute this we should get the
- 4:22result set that we see on the
- 4:24slide now in addition to these three
- 4:27result sets we also want the grand total
- 4:31so here we are not grouping by any
- 4:33specific column we're not grouping by
- 4:35gender or country we want the grand
- 4:38total of salaries you know basically the
- 4:40total amount of um salary that we paying
- 4:42for all employees across the globe to
- 4:46achieve
- 4:47that I'm going to use another select
- 4:50query now we don't require any Group by
- 4:52Clause at all you know country is going
- 4:55to be null gender is going to be null
- 4:58and we just want some of salary column
- 5:00across the entire table so when we
- 5:03execute this we should get the total
- 5:05salary that we are paying for all our
- 5:08employees so now we are going to apply
- 5:11another Union all operator here so when
- 5:14we execute this we should get the result
- 5:17that we see on the
- 5:18slide now if you look at this query we
- 5:21definitely have two problems here first
- 5:23the amount of transact SQL code that we
- 5:25have to write is huge second to produce
- 5:29this result result this transact SQL
- 5:31code is going to hit that employees
- 5:33table four times so it's not very good
- 5:36from a performance standpoint as well
- 5:38now if we use the grouping sets feature
- 5:41introduced in SQL Server 2008 then the
- 5:44amount of transact SQL code that we have
- 5:46to write will be greatly reduced this is
- 5:48just the amount of code that we have to
- 5:50write to produce this same result set
- 5:54okay so let's look at that grouping sets
- 5:58query So within our results set we want
- 6:00those three columns country gender and
- 6:01total salary so the select Clause is not
- 6:04going to change country gender sum of
- 6:06salary we giving it an alas total salary
- 6:09from employees table we're still going
- 6:11to use group buy because we use grouping
- 6:13sets with group buy so group buy and
- 6:15grouping sets so we specify our grouping
- 6:18sets here so we have an open and a close
- 6:20parenthesis and then we specify our
- 6:23groups so we want to first group by
- 6:26country and genda so the first six rows
- 6:29here
- 6:30will be produced by this first grouping
- 6:34here okay so this six rows you know have
- 6:39the salary grouped by country and gender
- 6:42and this is the group that will produce
- 6:44that and then we are grouping by country
- 6:46so the next three rows that we have here
- 6:49so we have got three countries India UK
- 6:51USA so salary is grouped by Country and
- 6:54this is the group within our grouping
- 6:56sets that's going to produce those three
- 6:58rows and then the rows 10 and 11 are
- 7:04produced by the third group that we have
- 7:07here gender and then to produce this
- 7:09grand total right here we're using this
- 7:12empty parenthesis so this is basically
- 7:14telling to SQL Server we want the grand
- 7:16total of sum of uh you know salary
- 7:19column all right so let's look at this
- 7:22in
- 7:23action let's fire up another query
- 7:25editor window and I'm going to make a
- 7:27copy of this select query
- 7:33so we want to use grouping
- 7:38sets open parenthesis close parenthesis
- 7:42and then we specify our groups first we
- 7:45want to group by country and gender so
- 7:49this is going to give us the first six
- 7:51rows and then we want to group just by
- 7:54country and then just by gender and then
- 7:59finally lay grand total so when we
- 8:03execute this
- 8:04query we should get the result set that
- 8:08this Union all operator has produced now
- 8:11if you look at the data first of all
- 8:13look at the number of rows the union all
- 8:15query has produced 12 rows the grouping
- 8:19sets query also produced 12 rows but if
- 8:21you look at it's the same data but if
- 8:24you look at the order of the rows here
- 8:27between the grouping set query and the
- 8:29union all query it's not the same now if
- 8:31you want to control the ordering of rows
- 8:33you can use Auto by Clause with this
- 8:36grouping sets
- 8:38query so
- 8:40for this query you know basically to
- 8:43produce the data in the same order as
- 8:46this Union all operator query I'm going
- 8:48to use order by and we are first going
- 8:51to order by country grouping and then by
- 8:53gender grouping and within each gender
- 8:55grouping we want to order the genders
- 8:58Okay so
- 9:05Auto bu I'm going to use the grouping
- 9:08function and we want to group first by
- 9:11country grouping and
- 9:14then by gender
- 9:17grouping and then finally by gender so
- 9:20when we execute this now The Ordering of
- 9:23the data should be same as well between
- 9:25these two queries
- 9:32thank you for listening and have a great
- 9:34day
About this transcript
This page contains the full transcript of Grouping Sets in SQL Server by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,568 words across 219 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.