Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "as" to set alias for joined tables in oracle 10

Tags:

oracle10g

I wrote this, and it is wrong syntax, help me fix it, I want 'T' to be an alias of the result of the two inner joins.

select T.id 
from table1 
  inner join table2 on table1.x = table2.y  
  inner join table3 on table3.z = table1.w as T;
like image 928
Rn2dy Avatar asked Sep 20 '10 21:09

Rn2dy


People also ask

Can you use alias in join?

An Alias is a shorthand for a table or column name. Aliases reduce the amount of typing required to enter a query. Complex queries with aliases are generally easier to read. Aliases are useful with JOINs and aggregates: SUM, COUNT, etc.

Can we use as in Oracle?

The AS clause allows you to give an alias name to EQL attributes and results. The alias name can be given to an attribute, attribute list, expression result, or query result set. The aliased name is temporary, as it does not persist across different EQL queries.

Why do we alias the tables in a join?

SQL aliases are custom names that you can give to the columns and tables you include in your queries. Aliases are very useful, as they can greatly improve the readability and maintainability of your query.

Can we use alias in Oracle?

In Oracle, aliasing can also be done in column name as well as in table name. Aliasing is done to give a temporary to a column or table.


2 Answers

You cannot use aliases to name the "entire" join, you can, however, put aliases on individual tables of the join:

select t1.id
from table1 t1
   inner join table2 t2 on t1.x = t2.y
   inner join table3 t3 on t3.z = t1.w

In the projection, you will have to use the alias of the table, which defines the id column you are going to select.

like image 102
Dirk Avatar answered Oct 06 '22 23:10

Dirk


You can't directly name the result of a join. One option is to use a subquery:

select T.id
from (
  select *
  from table1
  inner join table2 on table1.x = table2.y
  inner join table3 on table3.z = table1.w
) T

Another option is subquery factoring:

with T as (
  select *
  from table1
  inner join table2 on table1.x = table2.y
  inner join table3 on table3.z = table1.w
)
select T.id
from T
like image 37
David Phillips Avatar answered Oct 06 '22 22:10

David Phillips