Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Databases SQL Basics Getting Data from a Database Categorizing Your Output with 'AS'

Christopher Franklin
PLUS
Christopher Franklin
Courses 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.

4 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,623 Points

Hey 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).

:dizzy:

Cindy Lea
PLUS
Cindy Lea
Courses 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.

Jason Anders
Jason Anders
Treehouse Moderator 145,623 Points

Kind 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.

:dizzy:

Thomas Nilsen
Thomas Nilsen
14,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

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
Thomas Nilsen
14,957 Points

I realised after I wrote this - this isn't a alias problem, but still something that is nice to be aware of.

The answer is at the 4:00 mark of the video