SQL INNER JOIN Query Tutorial With Example | SQL JOIN Types
SQL INNER JOIN Query Tutorial With Example is today’s topic. SQL joins are used to combine records from two or more tables in a database. SQL Inner Join is used to select all rows from tables for the match between the columns in tables. SQL INNER JOIN is based on the concept of EQUI JOINS. EQUI JOINS are those who use a comparison operator (=) for combining records from two or more tables. When the condition is satisfied, column values for each matched pair of rows of two tables are combined into a result row. You can check out SQL LEFT JOIN tutorial on this blog as well.
SQL INNER JOIN Query Tutorial
The shaded part above Shows the common records between both the tables.
The inner join is one of the widely used SQL joins in SQL Database. SQL inner join clause allows us to query the data from two or more related tables. SQL INNER JOIN keyword selects records that have matching values in both tables.
The INNER JOIN keyword selects all the rows from both tables as long as there is a match between the columns. If there are records in the “tableA” table that do not have matches in “tableB,” these rows will not be shown!
Syntax of Inner Join
Select columns from Table_1 INNER JOIN Table_2 on Table_1.column = Table_2.column;
So, in the above statements columns represent the column names of the tables.
Table_1 and Table_2 are the names of tables.
And the condition i.e Table_1.column = Table_2.column is used to compare the columns which are common in both the tables.
In the above syntax, the query has retrieved data from both Table_1 and Table_2 tables:
- First, specify the main table (Table_1) in the FROM clause.
- The second table in an INNER JOIN clause (Table_2) and a join predicate. The only rows that cause a join predicate to evaluate to TRUE are included in a result set.
The INNER JOIN clause compares the each row of the table Table_1 with rows of Table_2 to find all the pairs of rows that satisfy a join predicate.
If a join predicate evaluates to TRUE, the column values of the matching rows of Table_1 and Table_2 are combined into a new row and included in the result set.
Let’s see an example for the above statements.
Now, the query for inner join is following.
Select Department.Dept_no, Employee.Emp_name, Employee.City, Employee.Salary from Department INNER JOIN Employee on Department.Emp_id = Employee.Emp_id
See the output below.
As you can see that the common records between the columns of employee and department table are shown.
JOIN Three Tables
See the following SQL INNER JOIN Query.
Select Department.Dept_no, Employee.Emp_name, Employee.City, Job.Job from((Department INNER JOIN Employee ON Department.Emp_id = Employee.Emp_id) INNER JOIN Job ON Department.Dept_no = Job.Dept_id);
Here, at first condition is checked between the department and employee table and after that, it is checked between the Department and Job table. Then the common records between all those tables are displayed.
We are displaying all columns of Employee and Department table using INNER JOIN.
Select * from Employee INNER JOIN Department ON Department.Emp_id = Employee.Emp_id;
See the below output.
- SQL is not Case Sensitive.
- While checking for equality condition, it is not necessary that column names of both the tables should be the same, it can be different. The only thing which is checked is a record within those particular columns.
- The INNER JOIN in SQL joins two tables according to the matching of specific criteria using a comparison operator.
Difference between INNER JOIN and OUTER JOIN
The INNER JOIN is such type of join that returns all the rows from both the participating tables where a key record of one table is equal to the key records of another table.
Inner Join required a comparison operator to match rows from the participating tables based on the common field or column of both the tables.
Whereas ab OUTER JOIN returns all the rows from the participating tables which satisfy the condition and also those rows which do not match the condition will appear in this operation. This result set can appear in three types of format –
The first one is the LEFT OUTER JOIN. This join output includes all the rows from the left table of the JOIN clause and unmatched rows from the right table with NULL values for selected columns.
The second one is the RIGHT OUTER JOIN. In this join, the output includes all the rows from the right of JOIN cause and unmatched rows from a left table with NULL values for selected columns.
The last one in FULL OUTER JOIN. This join includes the matching rows from the left and right tables of JOIN clause and the unmatched rows from the left and the right table with NULL values for selected columns.
Finally, SQL INNER JOIN Query Tutorial With Example | SQL JOIN Types is over.