SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | USING Clause

The USING clause in SQL is primarily employed in two main scenarios:

  1. Join Operations
  2. DELETE and UPDATE statements with aliases

Let's delve into each of these:

1. Join Operations:

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.

2. DELETE and UPDATE statements with aliases:

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;

Important Points:

  • 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.

  1. How to use USING in SQL JOINs:

    • The USING clause is used in SQL joins to specify the columns used for matching.
    SELECT *
    FROM Table1
    INNER JOIN Table2
    USING (CommonColumn);
    
  2. INNER JOIN with USING clause in SQL:

    • An INNER JOIN using the USING clause returns only the matching rows.
    SELECT *
    FROM Employees
    INNER JOIN Departments
    USING (DepartmentID);
    
  3. 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);
    
  4. Using multiple columns with USING in SQL:

    • The USING clause can involve multiple columns for matching.
    SELECT *
    FROM Orders
    INNER JOIN Customers
    USING (CustomerID, ShipCity);
    
  5. 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);
    
  6. Handling NULL values with USING in SQL:

    • The USING clause handles NULL values like any other comparison.
    SELECT *
    FROM Employees
    INNER JOIN Departments
    USING (DepartmentID);
    
  7. 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;
    
  8. JOIN conditions and USING in SQL:

    • Specify additional conditions in the ON clause when using USING for more complex joins.
    SELECT *
    FROM Orders
    INNER JOIN Customers
    USING (CustomerID)
    WHERE Orders.OrderDate > '2022-01-01';
    
  9. Alternatives to USING clause in SQL:

    • Use the ON clause or fully qualify column names as alternatives to USING.
    SELECT *
    FROM Employees
    INNER JOIN Departments
    ON Employees.DepartmentID = Departments.DepartmentID;
    
  10. USING with different types of joins in SQL:

    • Apply USING with various join types like INNER JOIN, LEFT JOIN, or RIGHT JOIN.
    SELECT *
    FROM Employees
    LEFT JOIN Departments
    USING (DepartmentID);
    
  11. Practical use cases for USING in SQL:

    • Practical scenarios involve using USING when joining tables on common columns.
    SELECT *
    FROM Products
    INNER JOIN Suppliers
    USING (SupplierID);
    
  12. Limitations and considerations with USING:

    • Considerations include the need for columns with the same name and potential ambiguities.
    SELECT *
    FROM Employees
    INNER JOIN Departments
    USING (DepartmentID);