View video tutorial

ORACLE Column Alias

ORACLE

An Oracle column alias assigns a temporary name to a column during the result set of an SQL query.

Oracle Column Alias


➔ Column aliases make the output more readable.

➔ This alias exists only for the duration of the execution of that particular query.

➔ The AS keyword is optional if there are no spaces in the alias name.

➔ If there are no spaces in the alias name, it is not mandatory to use quotes around the alias name.

➔ If the column alias name contains spaces, special characters, or requires specific case preservation of characters, the column alias name must be enclosed in double quotation marks.

➔ Column aliases are very helpful when using concatinating or calculative expressions and the column headings for those expressions become too long.

Syntax

If the alias name does not contain any spaces, the AS keyword is not required.

/* Oracle column alias */
SELECT column_name AS alias_name
FROM table_name;

Syntax
/* Oracle column alias */
SELECT column_name AS alias_name
FROM table_name;

Syntax
/* Oracle column alias with space */
SELECT column_name AS "alias name"
FROM table_name;

Example
/* Oracle column alias */
SELECT EMPLOYEE_ID AS ID 
FROM employees;

Example: column alias in expression
/* Oracle column alias  in expression */
SELECT EMPLOYEE_ID AS ID  FIRST_NAME || LAST_NAME AS NAME
FROM employees;

Oracle Example 1

select * from employees;
Try it Now »

Copy the code and try it out practically in your learning environment.


Oracle Example 2

select * from departments;
Try it Now »

Copy the code and try it out practically in your learning environment.


Practice this lesson in your Oracle learning environment.

The best way to learn is to implement it yourself after any lesson.

Type or copy the code and try it out practically in your learning environment.

Create table:

Example

/* Drop table if the table name is already exists */
DROP TABLE IF EXISTS demo_column_alias;
/* Create Table*/
CREATE TABLE demo_column_alias (
    employee_id   NUMBER(6) PRIMARY KEY,
    first_name    VARCHAR2(20),
    last_name    VARCHAR2(20),
    salary        NUMBER(8, 2),
    department_id NUMBER(4)
);

Copy the code and try it out practically in your learning environment.


Insert value:

Example

INSERT INTO demo_column_alias(employee_id, first_name, last_name, salary, department_id)
VALUES (101, 'Neena', 'Kochhar', 17000, 90);
INSERT INTO demo_column_alias (employee_id, first_name, last_name, salary, department_id)
VALUES (102, 'Lex', 'De Haan', 17000, 90);
INSERT INTO demo_column_alias (employee_id, first_name, last_name, salary, department_id)
VALUES (103, 'Alexander', 'Hunold', 9000, 60);

Copy the code and try it out in your learning environment.


Check the query results against the data.

Example

SELECT employee_id AS "ID"  first_name || last_name AS "FULL NAME"
FROM demo_column_alias;

Copy the code and try it out practically in your learning environment.


If desired, delete the table after finishing the exercise.

Example

/* Drop the table from the schema. */
DROP TABLE demo_column_alias;

Copy the code and try it out practically in your learning environment.