Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sql Oracle USING USE?

I am trying to use the "USING" instead of "ON" to make a query.

The problem is that in the "Employees" table it is called "codigo" and in the "trabajan" table it is called "cod_emp".

With the USING how would you join these two tables?
According to the Oracle documentation it is possible, but I can’t get it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT NOMBRE, FUNCION
FROM EMPLEADOS
JOIN TRABAJAN USING (CODIGO,COD_EMP);

The example from the oracle documentation:
https://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqljusing.html

The next query is similar to the one above, but it has the additional join condition that COUNTRIES.COUNTRY_ISO_CODE is equal to CITIES.COUNTRY_ISO_CODE:

SELECT * FROM COUNTRIES JOIN CITIES
    USING (COUNTRY, COUNTRY_ISO_CODE)

>Solution :

From the Oracle 19c documentation, not the JavaDB docs you linked to, and with emphasis added:

When you are specifying an equijoin of columns that have the same name in both tables, the USING column clause indicates the columns to be used. You can use this clause only if the join columns in both tables have the same name. Within this clause, do not qualify the column name with a table name or table alias.

I you really had to use USING for an assignment then you could do so if you alias one of the column names in a subquery:

SELECT NOMBRE, FUNCION
FROM (
  SELECT CODIGO AS COD_EMP, NOMBRE FROM EMPLEADOS
)
JOIN TRABAJAN USING (COD_EMP);

… which doesn’t seem very "comfortable". It’s simpler to use ON to handle the name discrepancy:

SELECT NOMBRE, FUNCION
FROM EMPLEADOS
JOIN TRABAJAN ON CODIGO = COD_EMP;

fiddle

It’s also a good idea to qualify all of the column names with the table names or aliases so you can see where each column is coming from, and you can use table aliases to make that shorter, e.g.

SELECT E.NOMBRE, T.FUNCION
FROM EMPLEADOS E
JOIN TRABAJAN T ON E.CODIGO = T.COD_EMP;

(But you don’t qualify common columns with USING, which personally I find confusing and another reason to avoid that form of join syntax.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading