SQLite Aliases

You can temporarily rename a table or column to another name, which is calledan alias. Using a table alias means renaming a table in a specific SQLite statement. The renaming is a temporary change; the actual name of the table in the database does not change.

A column alias is used to rename a column in a table for a specific SQLite statement.

Syntax

TableThe basic syntax of aliases is as follows:

SELECT column1, column2....
FROM table_name AS alias_name
WHERE [condition];

ColumnThe basic syntax of aliases is as follows:

SELECT column_name AS alias_name
FROM table_name
WHERE [condition];

Examples

Assume there are the following two tables: (1) The COMPANY table is as shown below:

sqlite> select * from COMPANY;
ID          NAME                  AGE         ADDRESS     SALARY
----------  --------------------  ----------  ----------  ----------
1           Paul                  32          California  20000.0
2           Allen                 25          Texas       15000.0
3           Teddy                 23          Norway      20000.0
4           Mark                  25          Rich-Mond   65000.0
5           David                 27          Texas       85000.0
6           Kim                   22          South-Hall  45000.0
7           James                 24          Houston     10000.0

(2) The other table is DEPARTMENT, as shown below:

ID          DEPT                  EMP_ID
----------  --------------------  ----------
1           IT Billing            1
2           Engineering           2
3           Finance               7
4           Engineering           3
5           Finance               4
6           Engineering           5
7           Finance               6

Now, the following is thetable aliasusage. Here we use C and D as aliases for the COMPANY and DEPARTMENT tables respectively:

sqlite> SELECT C.ID, C.NAME, C.AGE, D.DEPT
        FROM COMPANY AS C, DEPARTMENT AS D
        WHERE  C.ID = D.EMP_ID;

The above SQLite statement will produce the following result:

ID          NAME        AGE         DEPT
----------  ----------  ----------  ----------
1           Paul        32          IT Billing
2           Allen       25          Engineerin
3           Teddy       23          Engineerin
4           Mark        25          Finance
5           David       27          Engineerin
6           Kim         22          Finance
7           James       24          Finance

Let us look at acolumn aliasexample, where COMPANY_ID is the alias for the ID column and COMPANY_NAME is the alias for the name column:

sqlite> SELECT C.ID AS COMPANY_ID, C.NAME AS COMPANY_NAME, C.AGE, D.DEPT
        FROM COMPANY AS C, DEPARTMENT AS D
        WHERE  C.ID = D.EMP_ID;

The above SQLite statement will produce the following result:

COMPANY_ID  COMPANY_NAME  AGE         DEPT
----------  ------------  ----------  ----------
1           Paul          32          IT Billing
2           Allen         25          Engineerin
3           Teddy         23          Engineerin
4           Mark          25          Finance
5           David         27          Engineerin
6           Kim           22          Finance
7           James         24          Finance
Other Extensions