0% found this document useful (0 votes)
4 views8 pages

Understanding SQL Aliasing Techniques

Uploaded by

ahmednaser122122
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Understanding SQL Aliasing Techniques

Uploaded by

ahmednaser122122
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

0:00

what's going on everybody my name is


0:01
Alex free brands today what are we
0:03
talking about aliasing now all aliasing
0:05
really is is temporarily changing the
0:07
column name or the table name in your
0:09
script and it's not really gonna impact
0:11
your output at all
0:12
aliasing is really used for the
0:14
readability of your script so that if
0:16
you hand this off to somebody or
0:17
somebody comes behind you and starts
0:19
working on this they can more easily
0:20
understand it and it may not sound super
0:23
useful especially for small scripts like
0:24
what we have on the screen but when you
0:26
start getting to larger scripts where
0:28
you have six seven or eight joins and
0:30
you're selecting ten different column
0:32
names it actually is very useful and
0:34
very important so let's get into how
0:36
that actually works and then I'll have
0:37
an example later of how we can use
0:39
aliasing with a little bit of a larger
0:40
query so in this table
0:42
let's select first name and execute what
0:48
we want to do is just write as and let's
0:51
do f name and all that's gonna do is
0:54
it's gonna rename this column from first
0:57
name which it was originally named to f
0:58
name now you can use as but you can also
1:02
just get rid of that and do it exactly
1:04
how I have it and it's still gonna work
1:06
perfectly you can either use the adds or
1:08
you can not use it I typically don't I
1:10
just put a space in between the actual
1:12
column and the alias sounds look at an
1:14
example of how this might actually be
1:15
useful so we have a first name and a
1:17
last name in this column so what we're
1:18
gonna do is actually combine those so
1:21
let's do plus and let's add a space in
1:24
there and let's do a plus and let's do
1:26
last name so this is gonna take the
1:28
first name out of space and then do the
1:30
last name and we're gonna do that as I
1:32
let's do full name and let's execute
1:36
this so now we have a column called full
1:40
name which is our alias so we've
1:41
combined the first name and the last
1:43
name column into one single column and
1:45
we've renamed it full name if we have
1:48
not used this alias at all it would have
1:50
just said this which is no column name
1:52
at all we don't typically want that when
1:54
we have an output we want to give this
1:56
column a name so that's somebody who is
1:57
actually looking at this script or who's
1:59
looking at the output of the script
2:00
actually understand the what is
2:02
contained within this column so for that
2:04
we're just going to keep it as full name
2:07
now another time that you're often going
2:08
to use aliasing in the Select statement
2:10
is when
2:11
using aggregate functions so in this
2:13
table we have age so let's pull that up
2:15
really quick so we have age right here
2:19
and let's actually just do the average
2:22
age and when we execute this we're gonna
2:26
get no column name and 31 so we want to
2:30
do is give it a verage age and when we
2:34
do that we now have a column name and
2:36
again you want to have a column name in
2:38
case someone comes up behind you and is
2:40
reading the scripts that they understand
2:41
what this column is being used for now
2:43
that we've looked at aliasing column
2:45
names let's look at aliasing table names
2:47
it basically is the exact same thing
2:49
we're just gonna write as and let's do
2:52
demo for demographics and let's do demo
2:57
dot and it's going to give us all of our
2:59
options and we'll do employee ID so when
3:04
you alias in a table name when you are
3:07
selecting it in the Select statement you
3:09
actually need to preface your column
3:10
name with a table name or the table
3:13
alias dot and then employee ID and this
3:16
is extremely important to do especially
3:17
when you have a lot of joins that you're
3:19
doing or you're selecting a lot of
3:20
columns we have several joins because
3:23
they can get in very very messy quick so
3:25
let's actually join this to employee
3:30
salary and let's do that on demo dot
3:36
employee ID is equal to sow dot employee
3:45
ID so now let's do demo dot employee ID
3:48
comma Sal dot and let's do a salary so
3:53
looking at the script now is very clean
3:55
is very easy to understand and that is
3:57
what's so important with aliasing if for
4:00
example we took this off every time we
4:03
wanted to reference this table we would
4:05
have to put the entire table name and
4:07
putting the entire table name is correct
4:09
it just is very cumbersome and does not
4:11
look clean at all and so using something
4:13
like demo as an alias makes it a lot
4:16
more easily readable and a lot more
4:17
manageable when you're looking at it
4:19
we have a very long script let's look at
4:22
this query where we're joining the
4:23
others
4:23
three separate tables and after each
4:25
table we have an alias for employee
4:27
demographics we have a police salary we
4:29
have B and warehouse employee
4:30
demographics we have C now unfortunately
4:33
I have seen a lot of scripts that look
4:34
exactly like this and this is what you
4:36
do not want to do you do not want to use
4:38
your aliasing to just write an a a B or
4:40
a C that is very frowned upon when
4:42
writing queries because it really
4:43
doesn't give any context to what the
4:45
table that you're referencing is and it
4:47
gets really confusing as this query
4:49
continues to grow and as you add more
4:51
columns to your select statement it
4:53
makes it more difficult to understand
4:54
where those columns are coming from and
4:56
so when I'm reading that I say select a
4:58
dot employee ID okay what's a a is
5:01
employee demographics so you really do
5:03
not want to do that now it's looking an
5:05
example of what it should look like so
5:07
for employee demographics instead of
5:08
having an alias of an a I used demo for
5:11
demographics for employee salary I use
5:14
Sal and for warehouse employee
5:16
demographics I used where now this is
5:18
not perfect by any means but in the
5:20
select statement if you're just glancing
5:22
at it you can easily understand which
5:24
columns are coming from which tables so
5:25
when I look at employee ID I know that's
5:27
coming from employee demographics
5:29
because I have a demo as the alias so
5:31
it's a lot easier to understand and when
5:33
you hand this query off to somebody it
5:35
is gonna be a lot easier for them to
5:36
read through it and understand where
5:38
those columns and those table names are
5:40
coming from and so they will appreciate
5:42
that in the long run so that is all I
5:44
got that is aliasing again not a super
5:46
tough subject but a really important one
5:48
to understand especially as you start
5:49
working in teams and as you start
5:51
creating more and more complex queries
5:53
you want to have it more organized and
5:54
more easily readable and so it may not
5:57
come into play with those really simple
5:58
queries but again as you build out those
6:00
more complex queries this becomes very
6:03
useful I really hope you enjoyed this
6:05
video if you did be sure to comment and
6:07
subscribe below thank you so much for
6:09
watching and I'll see in the next video

You might also like