Lesson 30: 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.

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.

The Editor.


Show Answer

The Answer.

Correct SQL:

                    SELECT * FROM movies a INNER JOIN movie_actors_link b ON a.id=b.movie_id INNER JOIN actors c ON b.actor_id = c.actor_id

Correct Output:

Try the next lesson