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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With