SQL Inner Join

SQL Inner Join keyword selects all rows from both tables as long as there is a match between the columns in both tables.

 

Inner join returns only those records/rows that match or exists in both the database tables.

SQL Syntax :

SELECT column1, column2
FROM table1
INNER JOIN table2
ON table1.column=table2.column;

SQL Inner Join

 

The INNER JOIN in SQL joins two tables according to the matching of a certain criteria using a comparison operator.

 

Query – Find the Employee name from Employees table and allocated department name to the Employee.

SQL> Select employees.First_Name , Departments.Department_name

From Employees JOIN Departments

Where employees.dept_id = Departments.id;

 

In the above example, We are calling the First_Name from Employees table and Department Name form Departments table and Joining the both of the tables with condition department id in the Employees table equal to ID of departments table.

So this query will give us the Name of the employees and allowed department made to an employee.

 

Join with the using Clause

In the Inner join, we can use the using keyword to perform the Join instead of an equal operator. There are some rules with using keyword, let’s discuss them –

  • We can use Using If several columns have the same names but the data types do not match, Join clause can be modified with the USING clause to specify the columns that should be used for the Join.

  • Use the USING clause to match only one column where more than one column matches.

  • Do not use a table name or alias in the referenced columns.

Example – Suppose we have two tables EMPLOYEES and DEPARTMENTS  in both tables we have a common column named DEPARTMENT_ID, which is the foreign key in EMPLOYEES table and Primary key in DEPARTMENTS table.

 

Let’s find the Employee Name from EMPLOYEES table and Department Name and Department Id from the DEPARTMENTS table.

SQL> SELECT employees.employee_id, Departements.Department_Name, Departments.Department_Id

FROM employees JOIN Departments

 

Using (Department_Id);

0 Comment

Leave a Reply