SQLite ALIASES


 

 

SQLite: ALIASES

This SQLite tutorial explains how to use the SQLite ALIASES with syntax and examples.

 

Description:

  • SQLite ALIASES can be used to create a temporary name for columns or tables.
  • COLUMN ALIASES are used to make column headings in your result set easier to read.
  • TABLE ALIASES are used to shorten your SQLite to make it easier to read or when you are performing a self join (ie: listing the same table more than once in the FROM clause).

 

Syntax:

  • The syntax to ALIAS A COLUMN in SQLite is:

 

  • The syntax to ALIAS A TABLE in SQLite is:

 

Parameters or Arguments:

column_name

The original name of the column that you wish to alias.

table_name

The original name of the table that you wish to alias.

AS

Optional. Most programmers will specify the AS keyword when aliasing a column name, but not when aliasing a table name. Whether you specify the AS keyword or not has no impact on the alias in SQLite. It is a personal choice in SQLite, unlike other databases. (Our examples will use AS when aliasing a column name but omit AS when aliasing a table name.)

alias_name

The temporary name to assign to the table or column.

 

Aliases can be useful when:

  • There are more than one table involved in a query.
  • Functions are used in the query.
  • Column names are big or not very readable.
  • Two or more columns are combined together.

Note: If the alias_name contains spaces, you must enclose the alias_name in quotes or square brackets. It is acceptable to use spaces when you are aliasing a column name. However, it is not generally good practice to use spaces when you are aliasing a table name. The alias_name is only valid within the scope of the SQLite statement.

 

Examples:

Alias Example for Table Columns:

  • The following SQLite statement creates a tempporary name for “name” column as “EmployeeName” and “hire_date” column as “Date” in the result set:

    EmployeeName Date
    chiru 1/1/2014
    arunesh 1/1/2012
    naga 1/1/2013
    nithya 6/1/2013
    jaya 1/1/2014
    uma 1/1/2013
    mathu 1/1/2013
    anitha 1/1/2014
    kannan 6/1/2013
    deepak

Alias Example for Tables:

  • The following SQL statement selects all the employees from the Employee table and salary from the Account table. We use the “Employee” and “Account” tables, and give them the table aliases of “e” and “a” respectively (Here we have used aliases to make the SQL shorter):

    salary name
    30000 chiru
    60000 arunesh
    40000 naga
    35000 nithya
    35000 jaya
    5000 uma
    5000 mathu