Latest web development tutorials

SQLite alias

You can temporarily rename the table or column to another name, which is calledan alias.Use table alias refers to a particular SQLite statement to rename tables. Rename the temporary change the actual name of the table does not change in the database.

Column alias is used for a particular SQLite statement to rename columns in the table.

grammar

Table alias basic syntax is as follows:

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

The basic syntax fora column alias is as follows:

SELECT column_name AS alias_name
FROM table_name
WHERE [condition];

Examples

Suppose there are two tables below, (1) COMPANY tables are 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

(2) is another table DEPARTMENT, as follows:

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

Now, here is thetable alias usage, where we use the C and D, respectively, and as an alias COMPANY DEPARTMENT table:

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 results:

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's look at acolumn alias instances where COMPANY_ID alias ID column, COMPANY_NAME alias 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 results:

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