We use cookies to enhance your experience on the site
CodeWorlds

Column aliases - give it a pretty name

Field names on a shelf can be technical:

rok_wydania
,
autor_id
. In the result we can rename them into something friendlier for the reader - this is an alias.

The word AS

We create aliases with the word AS:

1SELECT tytul AS Tytul, cena AS Cena FROM ksiazki;

In the answer the column headers will read Tytul and Cena, even though on the shelf they are still called

tytul
and
cena
. An alias changes only the label in the result - it never touches the data.

Aliases with a space

If an alias is to contain a space, wrap it in quotes:

1SELECT rok_wydania AS "Rok wydania" FROM ksiazki;

AS is optional

MySQL lets you drop the word

AS
- a single space is enough:

1SELECT nazwisko Autor FROM autorzy;

It works the same, but with

AS
the code is clearer - and that is what I recommend, @name.

Why use aliases?

  • Headers in reports become understandable to people.
  • In later locations you will see that aliases are priceless with joins (
    JOIN
    ) and functions.

Remember: an alias is a nickname given to a field for the span of a single query.

Go to CodeWorlds