Welcome to the Treehouse Community
Looking to learn something new?
Christopher FranklinCourses Plus Student 440 Points
Why would you want to Alias a column name? Does this actually change the name of the column in the database?
I'm just not sure why you would want to do this, and if it changes the name of the column in the data base or if it just changes the name for your results. What would be a real world example of this.
Jason AndersTreehouse Moderator 145,623 Points
First off, no, aliasing a column will not change the name of it in the database. It doesn't affect the database at all, in any way. You mostly alias to make your output much easier to read and understand.
A real world example could be:
You have a database that stores all the movies a store has available for purchase. In that database they use a column for the year the movie was released named
movie_released_year. While this is fine for the database, it not very readable for the user if they were to pull up the data on a website. Therefore, you would alias that column in the query as "Year Released" ... much more clear and user friendly. It's really all about the UX (User Experience).
Cindy LeaCourses Plus Student 6,485 Points
You can create aliases for column names to make it easier to work with column names, calculations, and summary values. For example, you can create a column alias to: Create a column name, such as "Total Amount," for an expression such as (quantity * unit_price) or for an aggregate function.
Thomas Nilsen14,957 Points
One really good reason why you would want to (and have to) alias columns is:
Let's say you have two table - TABLE A and TABLE B. You want to join these together.
SELECT ID FROM TABLE_A AS A JOIN TABLE_B AS B ON B.ID = A.ID
The example above would not work, since the column ID exists on both TABLE_A and TABLE_B and sql don't know which one of those you intended to use.
you would have to write either
BUT a new problem would occur id you were to do this:
SELECT ZZ.ID /*this wouldnt work because both of them is called ID You have rename them to for example 'A.ID as A_ID' and 'B.ID as B_ID'*/ FROM ( SELECT A.ID, B.ID FROM TABLE_A AS A JOIN TABLE_B AS B ON B.ID = A.ID ) ZZ
The answer is at the 4:00 mark of the video