Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing "USING" and "ON" in Oracle ANSI join

I wrote an Oracle SQL expression like this:

SELECT
...
FROM mc_current_view a
JOIN account_master am USING (account_no)
JOIN account_master am_loan ON (am.account_no = am_loan.parent_account_no)
JOIN ml_client_account mca USING (account_no)

When I try to run it, Oracle throws an error in the line with "ON" self-join saying: "ORA-25154: column part of USING clause cannot have qualifier".

If I omit the "am" qualifier, it says: "ORA-00918: column ambiguously defined".

What's the best way to resolve this?

like image 262
Sergey Stadnik Avatar asked Jan 19 '09 06:01

Sergey Stadnik


People also ask

What is ANSI joins in Oracle?

The ANSI join syntax was introduced in Oracle 9i. It has a number of advantages over the original syntax. It reads more like English, so it is much clearer. The tables and join conditions are all kept together in the FROM clause, so the WHERE clause only contains filters, not join conditions.

What does (+) mean in SQL join?

The plus sign is Oracle syntax for an outer join. There isn't a minus operator for joins. An outer join means return all rows from one table. Also return the rows from the outer joined where there's a match on the join key. If there's no matching row, return null.

Which Oracle join is faster?

- hash join with parallel hints: Fastest when joining a large table to a small table, hash joins perform full-table-scans, which can be parallelized for faster performance. See here, how to invoke a hash join.


2 Answers

The error message is actually (surprise!) telling you exactly what the problem is. Once you use the USING clause for a particular column, you cannot use a column qualifier/table alias for that column name in any other part of your query. The only way to resolve this is to not use the USING clause anywhere in your query, since you have to have the qualifier on the second join condition:

SELECT
...
FROM mc_current_view a
JOIN account_master am ON (a.account_no = am.account_no)
JOIN account_master am_loan ON (am.account_no = am_loan.parent_account_no)
JOIN ml_client_account mca ON (a.account_no = mca.account_no);
like image 88
DCookie Avatar answered Oct 31 '22 00:10

DCookie


My preference is never to use USING; always use ON. I like to my SQL to be very explicit and the USING clause feels one step removed in my opinion.

In this case, the error is coming about because you have account_no in mc_current_view, account_master, and ml_client_account so the actual join can't be resolved. Hope this helps.

like image 36
Nick Pierpoint Avatar answered Oct 31 '22 00:10

Nick Pierpoint