SQL Aliases Use and Purpose in SQL

SQL Aliases

SQL aliases are used to rename a table or a column on temporary basis. These aliases are used to avoid duplication of field names especially when multiple tables with same field names are joined together or when a function is applied to any column.

 In general terms, aliases are used to make columns easily understandable and readable.

Demo Table

For establishing a clear understanding of the above functions, following Products table will be used:

ID Products Price
1 Keyboard 50
2 Mouse 50
3 LCD 100
4 Speaker 65.25
5 Printer 120

Syntax for Column Alias

SELECT col_name_1 AS alias_name_1,
col_name_2 AS alias_name_2, … , col_name_n AS alias_name_n

FROM tbl_name;

Example for Column Alias

SELECT ID, Products AS [Product List] FROM Products;

The above query will return following:

ID Product List
1 Keyboard
2 Mouse
4 Speaker
5 Printer

Syntax for Table Alias

SELECT col_name(s) FROM tbl_name AS alias_name; 

Example for Table Alias

SELECT * FROM Products AS Product_List;

Aliases for columns and tables are useful when:

  • Lengthy column names are used or are not user-friendly
  • Two or more columns are merged together to create single field
  • There are two or more than two tables included in a query
  • SQL Functions are part of the query

More Related Articles For You

    DBMS Tutorial

    SQL and DBMS Interview Questions with Answers

    DBMS and SQL Practice Tests , Questions and Quizzes

Shortcodes Ultimate

Follow Us