Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

USING Keyword vs ON clause - MYSQL [duplicate]

Tags:

sql

mysql

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 ?

like image 360
Techie Avatar asked Dec 06 '12 18:12

Techie


People also ask

When can you use the using keyword rather than the on keyword?

In a nutshell, you use ON for most things, but USING is a handy shorthand for the situation where the column names are the same. The example above uses the ON keyword, but since the columns we use to join are called owners_id in both tables, then we can instead put in USING as a shorthand.

Is there any difference if you are adopting using keyword in join condition instead of using on keyword for join condition?

The main difference between USING and ON in a join condition is that when SELECT * is used in a query with a USING join, the resulting table will only have one instance of the column specified in the USING condition.

How do I ignore duplicates in SQL?

Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.

Does adding a WHERE clause improve performance?

A where clause will generally increase the performance of the database. Generally, it is more expensive to return data and filter in the application. The database can optimize the query, using indexes and partitions. The database may be running in parallel, executing the query in parallel.


1 Answers

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.

like image 61
echo_Me Avatar answered Sep 20 '22 08:09

echo_Me