SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The USING
clause in SQL is primarily employed in two main scenarios:
Let's delve into each of these:
When you're joining two or more tables and they have columns with the same name that you're using as the join criteria, you can use the USING
clause as a shorthand.
For example, consider two tables, orders
and customers
, both having a column named customer_id
. If you want to join these tables on the customer_id
column, you could use:
SELECT * FROM orders JOIN customers USING (customer_id);
This does the same thing as:
SELECT * FROM orders JOIN customers ON orders.customer_id = customers.customer_id;
But the USING
clause provides a more concise syntax when the columns you're joining on have the same name in both tables.
In some SQL databases (like PostgreSQL), the USING
clause can be used in UPDATE
and DELETE
statements along with table aliases. This can be particularly helpful when you want to update or delete records based on a comparison with another table.
For example, if you wanted to update prices in a products
table based on some criteria from a discounts
table:
UPDATE products AS p SET price = p.price * d.discount_factor FROM discounts AS d WHERE p.product_id = d.product_id AND d.expiry_date > CURRENT_DATE;
However, the above syntax doesn't work on all SQL databases. The same can be achieved in PostgreSQL with a USING
clause:
UPDATE products AS p SET price = p.price * d.discount_factor USING discounts AS d WHERE p.product_id = d.product_id AND d.expiry_date > CURRENT_DATE;
Not all databases support the USING
clause in the same way. The join operation use-case is more widely supported, but the DELETE
and UPDATE
usage is more specific to certain RDBMS like PostgreSQL.
When you use the USING
clause in a join operation, the output will have just one column for the join condition instead of two (as seen when using the ON
clause). This is because the database recognizes that the columns from the two tables are the same.
Always refer to the specific documentation for your RDBMS to understand how the USING
clause can be utilized.
How to use USING in SQL JOINs:
USING
clause is used in SQL joins to specify the columns used for matching.SELECT * FROM Table1 INNER JOIN Table2 USING (CommonColumn);
INNER JOIN with USING clause in SQL:
INNER JOIN
using the USING
clause returns only the matching rows.SELECT * FROM Employees INNER JOIN Departments USING (DepartmentID);
LEFT JOIN and RIGHT JOIN with USING:
LEFT JOIN
and RIGHT JOIN
can also use the USING
clause for matching.SELECT * FROM Employees LEFT JOIN Departments USING (DepartmentID);
Using multiple columns with USING in SQL:
USING
clause can involve multiple columns for matching.SELECT * FROM Orders INNER JOIN Customers USING (CustomerID, ShipCity);
USING clause vs. ON clause in SQL JOIN:
USING
is a shorthand for specifying columns in the ON
clause when the column names are the same.SELECT * FROM Employees INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
SELECT * FROM Employees INNER JOIN Departments USING (DepartmentID);
Handling NULL values with USING in SQL:
USING
clause handles NULL values like any other comparison.SELECT * FROM Employees INNER JOIN Departments USING (DepartmentID);
USING clause with NATURAL JOIN in SQL:
USING
can be used with NATURAL JOIN
to match columns with the same names.SELECT * FROM Employees NATURAL JOIN Departments;
JOIN conditions and USING in SQL:
ON
clause when using USING
for more complex joins.SELECT * FROM Orders INNER JOIN Customers USING (CustomerID) WHERE Orders.OrderDate > '2022-01-01';
Alternatives to USING clause in SQL:
ON
clause or fully qualify column names as alternatives to USING
.SELECT * FROM Employees INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
USING with different types of joins in SQL:
USING
with various join types like INNER JOIN
, LEFT JOIN
, or RIGHT JOIN
.SELECT * FROM Employees LEFT JOIN Departments USING (DepartmentID);
Practical use cases for USING in SQL:
USING
when joining tables on common columns.SELECT * FROM Products INNER JOIN Suppliers USING (SupplierID);
Limitations and considerations with USING:
SELECT * FROM Employees INNER JOIN Departments USING (DepartmentID);