Alias in SQL Query: why you need it

An alias is an associated name for the actual table column name in an SQL query. The alias is also called a user-defined column name.

You can hide the original column name with an alias, and the legibility of the SQL query is good. You can use a short alias name so that your SQL query looks simple and easy to understand.

[code language="sql"]
SELECT CUSTOMER_NAME as CNAME,
CUSTOMER_ID as "CUST ID" ,
PRODUCT_ID as "AVG"
FROM PRODUCT_TABLE
WHERE PRODUCT_ID = '120'
[/code]


Notes: The “CUST ID” has a space, and “AVG” is a reserved word

3 Top rules on writing aliases.

  1. ‘AS’ is optional, but if you miss the ‘,’ it will take the column name as the alias
  2. You need to use double quotes when you have a space in the alias name
  3. If the alias name is a reserved word, then you need to give double quotes

In an SQL query, you never need double quotes unless you use an alias name with spaces or a reserved word