SQL | USING Clause

21 Mar, 2018
If several columns have the same names but the datatypes do not match, the NATURAL JOIN clause can be modified with the USING clause to specify the columns that should be used for an EQUIJOIN.
USING Clause is used to match only one column when more than one column matches.
NATURAL JOIN and USING Clause are mutually exclusive.
It should not have a qualifier(table name or Alias) in the referenced columns.
NATURAL JOIN uses all the columns with matching names and datatypes to join the tables. The USING Clause can be used to specify only those columns that should be used for an EQUIJOIN.
EXAMPLES:
We will apply the below mentioned commands on the following base tables:
Employee TableDepartment Table
Employee Table
Department Table
QUERY 1: Write SQL query to find the working location of the employees. Also give their respective employee_id and last_name?
Input : SELECT e.EMPLOYEE_ID, e.LAST_NAME, d.LOCATION_ID
FROM Employees e JOIN Departments d
USING(DEPARTMENT_ID);
Output :


QUERY 1: Write SQL query to find the working location of the employees. Also give their respective employee_id and last_name?

Input : SELECT e.EMPLOYEE_ID, e.LAST_NAME, d.LOCATION_ID
FROM Employees e JOIN Departments d
USING(DEPARTMENT_ID);
Output :


Explanation: The example shown joins the DEPARTMENT_ID column in the EMPLOYEES and DEPARTMENTStables, and thus shows the location where an employee works.
We will apply the below mentioned commands on the following base tables:
Country TableLocation TableQUERY 2: Write SQL query to find the location_id, street_address, postal_code and their respective country name?
Input : SELECT l.location_id, l.street_address, l.postal_code, c.country_name
FROM locations l JOIN countries c
USING(country_id);
Output : 


Country Table
Location Table
QUERY 2: Write SQL query to find the location_id, street_address, postal_code and their respective country name?

Input : SELECT l.location_id, l.street_address, l.postal_code, c.country_name
FROM locations l JOIN countries c
USING(country_id);
Output : 


Explanation: The example shown joins the COUNTRY_ID column in the LOCATIONS and COUNTRIEStables, and thus shows the required details.
NOTE: When we use the USING clause in a join statement, the join column is not qualified with table Alias. Do not Alias it even if the same column is used elsewhere in the SQL statement:
Example:
Input: SELECT l.location_id, l.street_address, l.postal_code, c.country_name
FROM locations l JOIN countries c
USING(country_id)
WHERE c.country_id'IT';
Output: 

Input: SELECT l.location_id, l.street_address, l.postal_code, c.country_name
FROM locations l JOIN countries c
USING(country_id)
WHERE c.country_id'IT';
Output: 

Explanation: Since the column in USING Clause is used again in WHERE Clause, thus it throws an error to the user.
SQL-Clauses-Operators
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
CTE in SQL
How to Update Multiple Columns in Single Update Statement in SQL?
SQL Interview Questions
SQL | Sub queries in From Clause
Difference between DELETE, DROP and TRUNCATE
SQL Correlated Subqueries
MySQL | Group_CONCAT() Function
Window functions in SQL
Difference between DELETE and TRUNCATE
MySQL | Regular expressions (Regexp)