SQL ALIASES – Give temporary name to Column or Table

SQL ALIASES example – Learn how to create temporary name / readable name for columns or tables in MySQL database with example using AS keyword in SQL and important points about it. Giving a custom name to any column or table is known as alias.

For example,

lets say we have a column “title” in the BOOKS table and you want to see the list of books available in the table. So, we can  write title as “Book Title” in the SQL SELECT clause. Book Title is the alias of column “title” and is more readable.  Here is the query example for alias : “Select Title AS ‘Book Title, price FROM BOOKS;”.

Points to Notice:                             

In SQL database, aliases can be used to create a temporary name for columns name or tables.

  • Column Aliases  make column headings in result set easier to read.
  • Table Aliases shorten SQL to make it easier to read when performing a join operation.

SQL Alias Syntax

SQL syntax for aliasing a Column

To give the column a custom name we write query like [ “column name” AS “whatever custom name you want to give”]

Syntax:
SELECT [column_name]  AS [alias_name] FROM [table_name];

SQL  syntax for aliasing a Table

Syntax:
SELECT [column_names] FROM [table_name]  AS [alias_name];

Points to notice:

  • If the alias name contains spaces, enclose the alias name in quotes.
  • Spaces are allowed while aliasing a column name, but it is not good practice to use spaces.
  • The alias name is only valid within the scope of the statement. Meaning, you query for alias and see the alias name in the result set and then gone.

SQL Alias example for column of BOOKS Table

  Query example:

SELECT TITLE AS ‘BOOK TITLE’, PRICE FROM BOOKS;

BOOK TITLEPRICE
The Castle100
Animal Farm90
Madhushala92
Historica98
MOON CALLED456
TWILIGHT640
PRIDE AND PREJUDICE506

Related Posts