Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORA-00918: column ambiguously defined in SELECT *

Getting ORA-00918: column ambiguously defined: running this SQL:

SELECT * FROM   (SELECT DISTINCT(coaches.id),     people.*,     users.*,     coaches.*   FROM "COACHES"   INNER JOIN people ON people.id = coaches.person_id   INNER JOIN users ON coaches.person_id = users.person_id   LEFT OUTER JOIN organizations_users ON organizations_users.user_id = users.id ) WHERE rownum <= 25 

Any suggestions please?

like image 274
user43685 Avatar asked Jun 03 '11 22:06

user43685


People also ask

How do I fix Ora 00918 column ambiguously defined?

Luckily the solution to this Oracle error is just about as straightforward as finding what causes the problem. What is needed is to add the prefix to each column with the table name that it originally belonged too and then re-execute the SQL statement.

How do I know which column is ambiguously defined in SQL?

If more than one table includes the same column name and refers to those columns in a join, the column name will be ambiguous. Oracle will search in the joined tables if you refer to the column name. If the same column name appears in two or more tables, the column name is identified ambiguously.

What does column ambiguously defined mean?

The ambiguous column error message indicates that you have joined two (or more) columns in your query which share the same column name. The proper way to solve this is to give each table in the query an alias and then prefix all column references with the appropriate alias.

What is invalid identifier in SQL?

Ora-00904 Error Message “Invalid Identifier” Error Ora-00904 means you are attempting to execute an SQL statement that is one of the following: The SQL statement includes an invalid column name. The SQL statement includes a column name which does not currently exist.


1 Answers

A query's projection can only have one instance of a given name. As your WHERE clause shows, you have several tables with a column called ID. Because you are selecting * your projection will have several columns called ID. Or it would have were it not for the compiler hurling ORA-00918.

The solution is quite simple: you will have to expand the projection to explicitly select named columns. Then you can either leave out the duplicate columns, retaining just (say) COACHES.ID or use column aliases: coaches.id as COACHES_ID.

Perhaps that strikes you as a lot of typing, but it is the only way. If it is any comfort, SELECT * is regarded as bad practice in production code: explicitly named columns are much safer.

like image 87
APC Avatar answered Sep 28 '22 01:09

APC