Say I have tables and I join them with inner join. What is the advantage of using "ON" over "USING". Or are they the same in all sense?
With the AND in the inner join you can specify it even more. Join the tables on the columns, where A1. Column = 'TASK' and throw away the rest. You could just as easily move the AND to the WHERE -Clause.
The USING clause: This allows you to specify the join key by name. The ON clause: This syntax allows you to specify the column names for join keys in both tables.
In MySQL, it's possible to have a JOIN statement without ON as ON is an optional clause. You can just simplly JOIN two tables like this: SELECT * FROM table_a JOIN table_b; It will match each row from table_a to every row in table_b .
USING
is an equijoin and causes duplicate columns to be removed from the resultset (arguably this makes it "more relational").
ON
is a theta join (i.e. join condition need not be equality and may involve columns with differing names) and allows duplicate columns to appear in the resultset.
USING
requires the names of the columns in both tables to be identical:
SELECT *
FROM employee
INNER JOIN department
USING (DepartmentID);
whereas ON
allows you to designate any columns to join on:
SELECT *
FROM employee
JOIN department
ON employee.DepartmentID = department.ID;
In short, USING
is more succinct, but ON
is more flexible.
http://en.wikipedia.org/wiki/Join_(SQL)#Equi-join
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