Column alias means giving a new name to a column while selecting data. This name is temporary, that is only for viewing purpose. It does not change the original column name of the table. Let us understand more about Oracle column aliases with syntax and examples.
This page contains (Skip to topic)..
What is a column alias?
A column alias in the Oracle context is giving a temporary name to existing column name. We have to use the alias instead of the original column name after aliasing it.
Here is the syntax of Oracle column aliasing,
SELECT col1 as alias_1 ,col2 alias_2 ,col3 as "Alias Name 3" ,col4 "Alias Name 3" FROM table1;
Here there are three things to understand,
AS keyword in column aliasing
The “AS” keyword is optional in the Oracle database. So, saying “select col1 as alias_1 from table1;” is apparently the same as “select col1 alias_1 from table1;”.
Here is an example query which selects employee job_id and job_title and aliases them.
select job_id as employee_job_id , job_title emplyoyee_job_title from jobs;
Double quotes (“) in column aliasing
Sometimes we need space in between the column names. Let us see what will happen if we keep some spaces and run the query.
select job_id as employee_job_id , job_title emplyoyee job title from jobs; ORA-00923: FROM keyword not found where expected 00923. 00000 - "FROM keyword not found where expected" *Cause: *Action: Error at Line: 2 Column: 28
It showed us error as the Oracle is expecting a FROM clause after the column alias name, i.e. the word “job” here.
We can solve this issue, by adding double quotes around the alias names. So, the correct query looks like this.
select job_id as employee_job_id , job_title "Eemplyoyee Job Title" from jobs;
In the above example, we are using the alias name with spaces. That is why we have added the double quotes to surround this.
Please note that the alias name is only valid in the scope of the SQL query.
In the next section, we will know how we can filter rows from the result set using the where clause.