Using a Column Alias.
What is a Column Alias and why would we use it?
Just as we used an alias for a table name in our previous lesson, we can also use an alias for a column name.
The syntax is the same ("AS alias_name") and helps us clear up any confusion when fields from different tables have the same name (but hold different data).
The Column Alias is also useful for tidying up our SQL code and results if the column names are long, repetitive or not particularly easy to decipher.
SELECT ID,
Actor_First_Name as FName,
Actor_Last_Name as Surname,
Actor_Date_Of_Birth as DOB,
Actor_Annual_Earnings as Income
FROM Movies;
In this example, we've taken some very longwinded Column names (such as Actor_First_Name) and made them into a more readable Column Alias (e.g. FName).
Quiz Time.
The Question.
Let's link our movies and actors back together but this time we only want to see two columns returned - 1) movie_name (which is the title of the movie) and 2) actor_name (which is the actor's name).The Data.
Table Name: movies
id | title | release_date |
---|---|---|
1 | Iron Man | 2008-05-02 |
2 | Captain America: Civil War | 2016-05-06 |
3 | Ant Man | 2015-07-17 |
Table Name: actors
actor_id | name |
---|---|
1 | Robert Downey Jr. |
2 | Chris Evans |
3 | Paul Rudd |
Table Name: movie_actors_link
link_id | movie_id | actor_id |
---|---|---|
1 | 1 | 1 |
2 | 2 | 1 |
3 | 2 | 2 |
4 | 2 | 3 |
5 | 3 | 3 |
The Editor.
Show Answer