USING Keyword vs ON clause - MYSQL

MysqlSql

Mysql Problem Overview


> Possible Duplicate:
> MySQL ON vs USING?

Query 1:

SELECT *
FROM users
JOIN orders ON (orders.user_id = users.user_id)
WHERE users.user_id = 1;

Query 2:

SELECT *
FROM users
JOIN orders USING (user_id)
WHERE user_id = 1;

I want to join orders and users tables to get some certain data. It works fine. My issue is since both queries output the same results set, is it the same? Which one is more efficient to be used? Which one is good for performance ? Which one is the best practise ?

Mysql Solutions


Solution 1 - Mysql

The USING clause is something we don't need to mention in the JOIN condition when we are retrieving data from multiple tables. When we use a USING clause, that particular column name should be present in both tables, and the SELECT query will automatically join those tables using the given column name in the USING clause.

For example, if there are two common column names in the table, then mention the desired common column name in the USING clause.

USING is also used while executing Dynamic SQL, like so:

EXECUTE IMMEDIATE 'DELETE FROM dept WHERE deptno = :num'
  USING dept_id; 
  • 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.

The USING clause

>The USING clause is used if several columns share the same name but you don’t want to join using all of these common columns. The columns listed in the USING clause can’t have any qualifiers in the statement, including the WHERE clause.

The ON clause

> The ON clause is used to join tables where the column names don’t match in both tables. The join conditions are removed from the filter conditions in the WHERE clause.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTechieView Question on Stackoverflow
Solution 1 - Mysqlecho_MeView Answer on Stackoverflow