67

Is the 'AS' keyword required in Oracle to define an alias name for a column in a SELECT statement?

I noticed that

SELECT column_name AS "alias"

is the same as

SELECT column_name "alias"

I am wondering what the consequences are of defining a column alias in the latter way.

Jonathan
  • 10,936
  • 8
  • 64
  • 79
  • 1
    Note that double quotes for the alias-name are required (as opposed to single quotes). A small, but important, point. – JosephDoggie Jan 19 '22 at 14:55

7 Answers7

52

According to the select_list Oracle select documentation the AS is optional.

As a personal note I think it is easier to read with the AS

Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
35

(Tested on Oracle 11g)

About AS:

  • When used on result column, AS is optional.
  • When used on table name, AS shouldn't be added, otherwise it's an error.

About double quote:

  • It's optional & valid for both result column & table name.

e.g

-- 'AS' is optional for result column
select (1+1) as result from dual;
select (1+1) result from dual;


-- 'AS' shouldn't be used for table name
select 'hi' from dual d;


-- Adding double quotes for alias name is optional, but valid for both result column & table name,
select (1+1) as "result" from dual;
select (1+1) "result" from dual;

select 'hi' from dual "d";
Eric
  • 22,183
  • 20
  • 145
  • 196
  • Do you know of any reason why Oracle disallows `AS` with a table name? I'm curious as to why this is, as it seems rather inconsistent intuition-wise, and other DBMS such as MySQL allow it. – ZeroKnight May 13 '20 at 20:37
  • @ZeroKnight No idea, maybe due to history reason. – Eric May 14 '20 at 00:41
9

AS without double quotations is good.

SELECT employee_id,department_id AS department
FROM employees
order by department

--ok--

SELECT employee_id,department_id AS "department"
FROM employees
order by department

--error on oracle--

so better to use AS without double quotation if you use ORDER BY clause

TNK
  • 195
  • 1
  • 2
  • 7
  • 1
    You can use an ORDER BY with a double quotation alias. This is especially helpfully if you have a space in the column name. So if your alias was `department_id AS "Department Name"` you would need to do `ORDER BY "Department Name"`. – fotijr Aug 27 '14 at 16:20
7

My conclusion is that(Tested on 12c):

  • AS is always optional, either with or without ""; AS makes no difference (column alias only, you can not use AS preceding table alias)
  • However, with or without "" does make difference because "" lets lower case possible for an alias

thus :

SELECT {T / t} FROM (SELECT 1 AS T FROM DUAL); -- Correct
SELECT "tEST" FROM (SELECT 1 AS "tEST" FROM DUAL); -- Correct
SELECT {"TEST" / tEST} FROM (SELECT 1 AS "tEST" FROM DUAL ); -- Incorrect

SELECT test_value AS "doggy" FROM test ORDER BY "doggy"; --Correct
SELECT test_value AS "doggy" FROM test WHERE "doggy" IS NOT NULL; --You can not do this, column alias not supported in WHERE & HAVING
SELECT * FROM test "doggy" WHERE "doggy".test_value IS NOT NULL; -- Do not use AS preceding table alias

So, the reason why USING AS AND "" causes problem is NOT AS

Note: "" double quotes are required if alias contains space OR if it contains lower-case characters and MUST show-up in Result set as lower-case chars. In all other scenarios its OPTIONAL and can be ignored.

nanosoft
  • 2,913
  • 4
  • 41
  • 61
IceSea
  • 101
  • 1
  • 5
7

Both are correct. Oracle allows the use of both.

Pawan Tejwani
  • 109
  • 1
  • 1
  • 6
5

The quotes are required when we have a space in Alias Name like

SELECT employee_id,department_id AS "Department ID"
FROM employees
order by department
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154
1

There is no difference between both, AS is just a more explicit way of mentioning the alias which is good because some dependent libraries depends on this small keyword. e.g. JDBC 4.0. Depend on use of it, different behaviour can be observed.

See this. I would always suggest to use the full form of semantic to avoid such issues.

ray
  • 5,454
  • 1
  • 18
  • 40