0% found this document useful (0 votes)
4 views

Intermediate SQL 7

Uploaded by

ahmednaser122122
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Intermediate SQL 7

Uploaded by

ahmednaser122122
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

0:00

what's going on everybody welcome back


0:01
to another intermediate sql tutorial
0:03
today we're going to be covering
0:04
partition by now partition by is often
0:07
compared to the group by statement
0:08
the group by statement is a little bit
0:10
different the group by statement is
0:12
going to reduce the number of rows
0:13
in our output by actually rolling them
0:15
up and then calculating the sums or
0:17
averages
0:17
for each group whereas partition by
0:19
actually divides the result set into
0:21
partitions and changes how the window
0:23
function is calculated
0:24
and so the partition by doesn't actually
0:26
reduce the number of rows returned in
0:28
our output
0:28
let's get started to look at the actual
0:30
syntax of how to use partition by and
0:32
then we'll compare it to the group by
0:33
statement later
0:34
just to see the differences between the
0:35
two we're going to be using these two
0:37
tables on our left over here so i'm
0:38
going to pull those up really quick
0:40
so let's run this and let's look at the
0:42
two these two tables side by
0:44
well one underneath the other really
0:46
quick so what we're going to be using to
0:48
demonstrate the partition by is this
0:50
gender column as well as
0:51
this salary column and so we just need
0:53
to join these two tables together on the
0:55
employee id and then we'll go from there
0:57
now i'm not going to bore you with that
0:58
i'm going to skip ahead and we'll
0:59
actually look at how to use this
1:01
partition by so i've joined these two
1:02
tables together and this is our output
1:04
but we don't want every single column
1:06
i'm going to start selecting some of
1:07
these columns and then
1:08
we'll start using this partition by and
1:10
see what the output looks like after
1:11
that
1:12
alright so let's go right up here let's
1:14
choose the first name
1:15
let's do the last name we'll do
1:19
gender and let's do salary
1:22
and now we want to identify how many
1:23
male and female employees we actually
1:25
have
1:26
and so we're going to say count of
1:28
gender
1:30
and it's going to be over and now we're
1:33
going to do our partition by
1:37
and we're also going to partition that
1:39
by the gender
1:41
as total gender now i'm going to come
1:44
back to
1:44
why we did each part but i want to see
1:46
the output first
1:48
and then i'm going to come back to why
1:49
we wrote it this way so let's just do
1:52
this really quick
1:55
so it's going to be a little bit
1:57
different than what you typically would
1:58
expect in a group by statement
2:00
the group by is going to roll everything
2:02
up and you typically would have like a
2:04
first name last name and a group by
2:05
statement because
2:07
it would be very hard to roll all those
2:09
things up into those individual columns
2:10
and to reduce the number of columns that
2:12
are in your output
2:13
and so in our output we can see pam
2:15
beasley she's a female
2:16
she makes 36 000 as a salary and there
2:19
are three
2:20
total women that work alongside her in
2:22
this employee demographics table
2:24
and so in our total gender column over
2:26
here this is where we use the partition
2:28
by
2:28
and if we used a group by statement to
2:30
get this kind of information
2:32
all we would be able to do to get this
2:34
information in a group by statement is
2:36
say select
2:37
gender count of gender and then group by
2:39
the gender down below underneath the
2:41
join
2:42
so because we're using the partition by
2:43
we're able to isolate just
2:45
one column that we want to perform our
2:46
aggregate function on and so we're able
2:48
to add things like the first name
2:50
and last name columns even though we
2:51
aren't trying to include that in any
2:53
partition or group by statement
2:54
yet we're still able to add the
2:56
aggregate function to each individual
2:57
row while still maintaining those other
2:59
columns
3:00
let's take this entire query and let's
3:02
basically just transform it into a group
3:04
by statement
3:05
and we'll see kind of what that looks
3:07
like and what the difference is
3:09
so all i'm going to do is get rid of all
3:11
this
3:12
i'm going to copy
3:15
all of and i'm going to say
3:19
group by and
3:22
i'm going to do that because we have to
3:24
use all these columns in our group by
3:26
statement
3:27
so let's execute this and as you can
3:30
tell we are not able to see the output
3:31
for the aggregate function that we were
3:33
hoping for
3:34
if we wanted to get the same output that
3:35
we had before where we're showing
3:37
three for females and six for males what
3:39
we'd have to do is get rid of this first
3:41
and last name
3:43
and the salary and do the same thing in
3:46
the group by statement
3:48
and so let me get rid of these really
3:50
quick
3:53
and run this and so what the partition
3:56
by is doing is basically taking
3:58
this query right here and sticking it on
4:01
one line
4:02
in the select statement and so i hope
4:03
now you can see how valuable the
4:05
partition by can be
4:06
if used correctly thank you guys so much
4:08
for watching i really appreciate it
4:10
if you like this video be sure to like
4:11
and subscribe below and i'll see in the
4:12
next video

You might also like