Hone

Lessons · SQL · aliases (AS)

Naming a column or table

AS gives a column or table a name for this query; the data is unchanged, the label is yours.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

The name a query produces becomes the field name in the report or API. Aliases make it readable and stable.

How to think about it

What should this column be called in the result? Alias every computed column, and alias tables to one or two letters in joins.

Worked example

SELECT title, minutes / 60.0 AS hours
A computed column, named.
FROM films AS f;
Table alias.

Your turn

Name the count.

SELECT COUNT(*)  film_count FROM films;

The trap

Referring to an alias in WHERE. WHERE runs before SELECT names things; repeat the expression or use a subquery.

Practise aliases (AS) on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.