SQLite | Query Data | Assign Column Aliases (AS Clause)

When querying a table with SELECT, the AS clause lets you display a column under a name different from its original name. This article explains how to assign column aliases.

 

Assigning a Different Name with AS

Use AS to assign a different name to a column in a SELECT result. The syntax is as follows.

SELECT column_name AS alias ... FROM table_name;

Place AS after the column name and specify its alias.

Try it with an example. Create the following table.

create table product (name text, num integer, price integer);
sqlite> create table product (name text, num integer, price integer);
sqlite> 

Insert data into the table with INSERT statements.

insert into product values ('Mouse', 10, 2500);
insert into product values ('Printer', 4, 8200);
insert into product values ('NotePC', 6, 54000);
insert into product values ('Display', 5, 18000);
insert into product values ('Desk', 8, 27000);
sqlite> insert into product values ('Mouse', 10, 2500);
sqlite> insert into product values ('Printer', 4, 8200);
sqlite> insert into product values ('NotePC', 6, 54000);
sqlite> insert into product values ('Display', 5, 18000);
sqlite> insert into product values ('Desk', 8, 27000);
sqlite> 

A normal query against this table returns the following result.

select name, num, price from product;
sqlite> select name, num, price from product;
name        num         price     
----------  ----------  ----------
Mouse       10          2500      
Printer     4           8200      
NotePC      6           54000     
Display     5           18000     
Desk        8           27000     
sqlite> 

Now use AS to display the price column under the alias value.

select name, num, price as value from product;
sqlite> select name, num, price as value from product;
name        num         value     
----------  ----------  ----------
Mouse       10          2500      
Printer     4           8200      
NotePC      6           54000     
Display     5           18000     
Desk        8           27000     
sqlite> 

The result shows value as the column name.

A SELECT statement can also return the result of an expression applied to column values.

select name, num, price, num * price from product;
sqlite> select name, num, price, num * price from product;
name        num         price       num * price
----------  ----------  ----------  -----------
Mouse       10          2500        25000      
Printer     4           8200        32800      
NotePC      6           54000       324000     
Display     5           18000       90000      
Desk        8           27000       216000     
sqlite> 

By default, the expression num * price becomes the result column name. Use AS to assign the alias value to that expression.

select name, num, price, num * price as value from product;
sqlite> select name, num, price, num * price as value from product;
name        num         price       value     
----------  ----------  ----------  ----------
Mouse       10          2500        25000     
Printer     4           8200        32800     
NotePC      6           54000       324000    
Display     5           18000       90000     
Desk        8           27000       216000    
sqlite> 

In this way, you can assign a meaningful column name to a calculated result.