Locked lesson.
About this lesson
Aliases can be used to temporary rename columns and tables
Exercise files
Download this lesson’s related exercise files.
Aliases.docx59 KB Aliases - Solution.docx
59.1 KB
Quick reference
Aliases
Aliases let us temporarily rename columns.
When to use
Use them whenever you want to rename a column temporarily for any given set of results.
Instructions
SELECT [First Name] AS "Customer First Name"
FROM [dbo].[Current_Customers]
This will give an alias of 'Customer First Name' to the column 'First Name'. This might be useful to differentiate between another column you have chosen to display, 'Potential Customer First Name' for example.
Hints & tips
- Aliases are good for temporarily renaming columns
- They aren't really renamed, just labeled differently in your results
- 00:05 In this video, I wanna talk about aliases, and aliases are used to temporarily rename
- 00:10 columns and tables too for that matter but we're gonna look at columns in this video.
- 00:14 So we can look at our column, and our column names are first name, last name,
- 00:18 email address, phone number, age, city, and country.
- 00:21 Well, whenever we're displaying data,
- 00:23 we may want to label them as different things.
- 00:26 Instead of city, we might want to label it as current city or whatever.
- 00:29 So alias is good for that.
- 00:31 Now, in the next few videos we're gonna be talking about using multiple tables and
- 00:35 pulling data from two or more tables and smushing them together.
- 00:39 When that happens,
- 00:39 we might want to label them differently than they're currently labeled.
- 00:42 So we'll use aliases for that, and aliases are pretty simple.
- 00:45 We just type in the column name we wanna mess with, and let's do City.
- 00:51 City. And then we just type A-S, AS, and
- 00:54 then just give it whatever you want to call it.
- 00:58 So, Current City let's change it to.
- 01:02 So if we run this query, boom.
- 01:04 We see Current City here at the top.
- 01:07 Now, if we get rid of the alias and run it again, you see it's just city.
- 01:11 If we wanna change it, Current City.
- 01:14 Now, if we get rid of all of this and just go back to our regular wild card
- 01:19 thing there, we can see the city hasn't changed.
- 01:21 The actual name of the column hasn't changed.
- 01:24 Like I said, this is just temporary.
- 01:25 It's just for the duration of that specific query that you can do that.
- 01:29 So that's aliases, very useful.
- 01:31 Now, like a lot of things in SQL, you can string these things together.
- 01:35 So let's go first
- 01:39 name and last name.
- 01:45 And let's run this query.
- 01:46 So we have a column for first name and a column of last name.
- 01:49 What if we want to put these two together?
- 01:51 Well, we're gonna look through this pass, but we can do this addition thing.
- 01:55 And let's give it an alias.
- 01:57 Let's call it As Customer Name.
- 02:02 Boom, now they get smushed into one column and the column is labeled Customer Name.
- 02:07 Now you'll notice the two words, the first name and
- 02:10 the last name, are smushed together and there's no space in between.
- 02:13 You can actually add a space just by doing that.
- 02:17 So we're just adding a single quotation mark with a space.
- 02:19 I just put a space in there.
- 02:21 If we execute that, boom, John Elder.
- 02:23 So very, very cool.
- 02:25 And it's not just stringing together things like this.
- 02:28 You can also put multiple ones on each line.
- 02:30 So we can give this a comma and then we can pull in our city like we did earlier
- 02:35 and give that an alias as we actually, whoops, we don't need those,
- 02:40 we need quotation marks.
- 02:41 Let's call it, what did we call it, Current City before?
- 02:44 So if execute that, boom, we get two columns, Current Customer and
- 02:47 Current City.
- 02:48 And both of the headers, the column names, have been given the alias that we want.
- 02:53 And like I said, if we get rid of all of this, go back to our wildcard,
- 02:57 the column names don't change.
- 02:59 They're physically not being changed.
- 03:01 We're just giving them a temporary alias.
- 03:03 For the duration of that query.
- 03:04 So those are aliases.
- 03:06 Pretty cool, very useful.
- 03:07 In the next video we're gonna look at inner join.
Lesson notes are only available for subscribers.