Using a Table Alias.

By Alan Hylands — 1 minute read

What is a Table Alias and why would we use it?

Typing out full table names in our SELECT statement and WHERE, GROUP BY and ORDER BY clauses can get a bit tedious if we are specifying a lot of columns. It also gets very messy to read, follow and maintain.

We can tidy up the look of our query by using an alias to represent each table in the query.

SELECT Products.description, Products.category, Products.price 
FROM Products;

could be re-written as:

 SELECT p.description, p.category, p.price 
FROM Products AS p;

This allows us to use the alias ("p" in our example) instead of typing out the full table name each time.

We can even omit the AS keyword, if the table name is immediately followed by its alias:

 SELECT p.description, p.category, p.price
FROM Products p;

Quiz Time.

The Question.

We're going to take another run at the three table join we did in the Multiple Joins Quiz.

Writing out the full table names is tedious, looks messy and is fraught with the dangers of mis-typing something and borking your whole query.

Use a TABLE ALIAS for each table to get a dataset back for each movie with their actors that appeared in them.

The Data.

Table Name: movies
idtitlerelease_date
1Iron Man2008-05-02
2Captain America: Civil War2016-05-06
3Ant Man2015-07-17
Table Name: actors
actor_idname
1Robert Downey Jr.
2Chris Evans
3Paul Rudd
Table Name: movie_actors_link
link_idmovie_idactor_id
111
221
322
423
533

The Editor.


Run SQL


Show Answer
Try the next lesson