How to Alias Table Names in SQL
This guides provides the syntax for assigning alias names to tables in SQL. Additionally, we'll discuss when table name aliases are practical to use.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

So we've spoken about alias's and how they can be used when it comes to being able to format data for reporting purposes. There's also another way that we can use aliases which is that we can use them with our tables. If that's a little bit unclear, don't worry because we're going to go through an example right here.

I'm going to start off with just a basic query. I'm going to say select guides_title and guides_revenue, and that's going to be from guides, where guides_revenue is greater than 600.

picture

I also wanted to introduce this greater than or less than because mainly we've been using the equals sign for equivalence. We also can use where to be able to see if something is greater than or less than, and that's going to do here. So any guides that are greater than 600 are going to run this, and this will work perfectly fine.

picture

Now as good as that is this can start to get a little bit confusing not as much when we're dealing with single elements. Right now all of these tables are just single tables. But what happens when we start to work with data coming from multiple tables inside of the same query that can get a little bit more confusing. That's where table aliases come from. So what I'm going to do is I'll copy this>

SELECT guides_title, guides_revenue
FROM guides
WHERE guides_revenue > 600;

I'm going to show you how you can create an alias for a table and I will give you the caveat that right now this isn't going to seem very practical because we're going to end up with the same result set and we're going to write a little bit more code for it.

Where this comes in handy is when you're working with multiple tables like we're going to get into when we get into our relational database section. The way that you can do it is you can say from Guides and then just add a g. Now it doesn't have to be "g", It could be "a", "z", and it could be anything you wanted. I'm going to do "g" just because I think that that's the most logical.

picutre

Now we can add g in front of each one of these columns. This is essentially saying that this is going to be from the guides table and you say it.

SELECT g.guides_title, g.guides_revenue
FROM guides g
WHERE g.guides_revenue > 600;

I mean I've put it down right here as well. Now if I run this code everything still works the same and it also doesn't change the naming in the columns because here we're not Aliasing the column name.

picture

Instead, we're just putting an alias that we can access inside of the table to represent the table. Like I said this doesn't seem very practical and if you were to only run a database query on a single table.

Then it would be completely pointless alias it. Where this becomes, very very handy is when we're going to get into joining tables together. Then with just one quick glance, you can see immediately which column belongs to which table.

Code

USE devcamp_sql_course_schema;

SELECT guides_title, guides_revenue
FROM guides
WHERE guides_revenue > 600;

SELECT g.guides_title, g.guides_revenue
FROM guides g
WHERE g.guides_revenue > 600;