Why do we use SQL aliases?


#1

Question

What are SQL aliases and why do we use them?

Answer

SQL aliases are temporary names given to a column or table.

Column aliases are often used for changing the name of the column that is displayed in the result. This can help make the column name more readable:

SELECT 2 * amt AS doubled_amount
FROM tbl;

Table aliases are often used when working with multiple tables to help make the query cleaner and more readable. Especially if our tables have long names, aliases can make it easier for when we need to reference a table, such as in the SELECT statement or ON clause:

SELECT p.user_id,
  p.play_date,
  s.title
FROM plays AS p
JOIN songs AS s
  ON p.song_id = s.id;