Home »
SQL
SQL USING Clause
SQL | USING Clause: In this tutorial, we are going to learn about the USING clause with its usages, syntax and query examples in SQL (Structured Query Language).
Submitted by Abhishek Goel, on April 05, 2020
SQL | USING Clause
On the off chance that few sections have similar names however the datatypes don't coordinate, the NATURAL JOIN statement can be altered with the USING condition to determine the segments that ought to be utilized for an EQUIJOIN.
- USING Clause is utilized to coordinate just a single section when more than one segment matches.
- Regular JOIN and USING Clause are unrelated.
- It ought not to have a qualifier(table name or Alias) in the referenced segments.
- Regular JOIN utilizes all the segments with coordinating names and datatypes to join the tables. The USING Clause can be utilized to determine just those segments that ought to be utilized for an EQUIJOIN.
Syntax:
SELECT <table_name>.<column_name> AS <column_name>
FROM <table_name> JOIN <table_name> USING (<column_name>);
Example:
Here we will be considering the following two tables,
Table: PETS
PETID | ANIMAL | NAME | OWNERID |
11 | Dog | Rocky | 23 |
22 | Cat | Charles | 34 |
33 | Mouse | Mickey | 23 |
44 | Parrot | Honey | 56 |
55 | Rabbit | Smudge | 34 |
66 | Hamster | Fluffy | 56 |
Table: Owner
OWNERID | NAME |
23 | Tina |
34 | Tarun |
56 | Sammy |
Query: To Know the names of the owners of the pet.
SELECT Owner.name AS owners, PETS.name AS pet, PETS.Animal
FROM Owner JOIN PETS USING (ownerid);
Output:
OWNERS | PET | ANIMAL |
Tina | Rocky | Dog |
Tarun | Charles | Cat |
Tina | Mickey | Mouse |
Sammy | Honey | Parrot |
Tarun | Smudge | Rabbit |
Sammy | Fluffy | Hamster |
Note: At the point when we utilize the USING proviso in a join explanation, the join section isn't qualified with table Alias. Don't Alias it regardless of whether a similar segment is utilized somewhere else in the SQL explanation.
You can also enroll in an SQL certification course to learn more about writing queries with clause.