SQLite Tutorial on SQLite ALIAS Syntax

you can rename a table or a column temporarily by giving another name, which is known as alias. the use of table aliases means to rename a table in a particular sqlite statement. renaming is a temporary change and the actual table name does not change in the database.

the column aliases are used to rename a table's columns for the purpose of a particular sqlite query.

syntax

following is the basic syntax of table alias.

select column1, column2....
from table_name as alias_name
where [condition];

following is the basic syntax of column alias.

select column_name as alias_name
from table_name
where [condition];

example

consider the following two tables, (a) company table is as follows −

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

(b) another table is department as follows −

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, following is the usage of table alias where we use c and d as aliases for 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          engineering
3           teddy       23          engineering
4           mark        25          finance
5           david       27          engineering
6           kim         22          finance
7           james       24          finance

consider an example for the usage of column alias where company_id is an alias of id column and company_name is an alias of 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          engineering
3           teddy         23          engineering
4           mark          25          finance
5           david         27          engineering
6           kim           22          finance
7           james         24          finance