Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialChristopher Franklin
Courses Plus Student 440 PointsWhy 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.
5 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHey Christopher,
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 Lea
Courses Plus Student 6,497 PointsYou 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.
Jason Anders
Treehouse Moderator 145,860 PointsKind of, but not really. Aliasing is used solely for the purpose of what is displayed to the user. You as the programmer will only work with the actual database names.
Aliases do not create columns or create column names. Once you have generated the query and/or any functions or actions you need done on them using the actual database column names, you can alias it so the outputted data (What the user will see) has readable names. You can't actually use aliases to query the database.
Thomas Nilsen
14,957 PointsOne 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
SELECT A.ID
or
SELECT B.ID
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
Thomas Nilsen
14,957 PointsI realised after I wrote this - this isn't a alias problem, but still something that is nice to be aware of.
kevin hudson
Courses Plus Student 11,987 PointsThe answer is at the 4:00 mark of the video
Codi Hasbrouck
41 PointsWell at 4 yeah. In the realm of sigma script. You could develop personality. For ux. Ux this. Customer experience. Involvement really. Emotional response.v It's not just to hide your layer of a actual machine. Flavor. I don't know if font comes out of that equation down the processes. Like have you ever found phone lines are predictable on certain intervals.
Christopher Franklin
Courses Plus Student 440 PointsChristopher Franklin
Courses Plus Student 440 PointsThanks, that clears it up.