- Read Tutorial
- Watch Guide Video
This section of the course is all about control flow in SQL, but before we can get there, I think it's important to take a step back. What we're going to talk about is alias's, and this is going to be something that is necessary when it comes to managing control flow. And so I wanted to put this lesson here before we get into things like case statements and false conditionals.
So what is an alias? Well if I were to build out a query like this, if I were to say SELECT and then come down to my columns list and start going down the list and listing out each one of these.
You're going to see pretty much what you've already seen before where we can query this and then I'll say from addresses. Now if I run this query it pulls up all of our addresses. Nothing new here but there is something that is a little bit annoying, and that is that our column names don't need to be up top and following our naming structure.
So you notice how we have a "addresses_" in front of each one of our column names? If you were to give this report to an executive or someone like that that may look a little bit weird to them.
That's where aliases come in where we can streamline what is shown and what is returned in the query. Instead of having to just use our kind of formalized names. The way that you can use an alias is right after the column that we're querying. You can say "AS" and then pass in whatever you want to name it. There are two ways that you can do. I could say street, and this would work.
Now if I run the query Now notice how the column name says street as we have up here now this way this works fine.
However, I wouldn't recommend it. I'd recommend the second approach where you wrap it in quotation marks and the reason for that is going to become evident right here. When I say as for street 2, I can't say Street 2 just like this.
This will throw an error, and it's because the only way that you can do this without quotation marks is only if you don't have any spaces. Right here wrap this in quotation marks, and now it works, and we can do the same thing for each one of these.
The reason why I'm putting the quotation marks for all of them even when it's not necessary. It's just that I like it to be very clear on what my aliases compared with what the column name is. addresses_city as 'city', addresses_state AS 'state', and addresses_postal_code as 'Postal code'.
SELECT addresses_street_one AS 'Street', addresses_street_two AS 'Street 2', addresses_city AS 'City', addresses_state AS 'State', addresses_postal_code AS 'Postal Code' FROM addresses;
Now if I run this you'll see that this works perfectly.
Look at how much nicer this looks so if you were to export this so if you were to say that I want to convert this into a CSV file.
I would say addresses and save this as a CSV file.
If I were to go and pull this up click on addresses file and open up Excel depending on what your default for CSV is and you'll see that we have nice column names now. We have Excel now that has all of our data, and because we're using aliases now, we don't have those ugly MySQL column names.
Now, this is something that someone could use and looks much more formalized and it has much more of a professional look and feel to it. I definitely recommend being able to use aliases whenever you want to build a report that you're going to give out to other people. So that is how you use aliases in SQL.
Code
USE devcamp_sql_course_schema; SELECT addresses_street_one AS 'Street', addresses_street_two AS 'Street 2', addresses_city AS 'City', addresses_state AS 'State', addresses_postal_code AS 'Postal Code' FROM addresses;