SQL Joins | Inner Join | Left Join | Right Join | Full Join | Self Join
Hello, and welcome to the blog. In this blog we are going to talk about "Joins".
In the previous blog we have seen most important topic of Primary Key, Foreign Key and
now its time to join some of the tables.
So, In this blog we are going to talk about
- What is join and its purpose
- Type Of Joins
- Inner Join
- Left Join
- Right Join
- Full Join
- Self Join
Let's start with
1) What is join and its purpose
- There is a clause in SQL called Join. And its purpose is to combine rows from two or more tables which are connected with each other by a common column.
- When I say connected with each other by a common column I am talking about Foreign Key which we have covered in previous blog. Although if you haven't enforced foreign key for this blog but we do have a common column then also you can perform join operation.
- For explaining join I have created 2 table:
-
Employee Table: (Table Name = 'Employee1')
2) Types Of Join
- Inner Join
- Inner join is the most used join in SQL. In real life scenarios we use inner join almost 90% of time.
- Now let's talk about what actually happen in inner join is "It selects record that are having matching values in both table".
- let's see syntax and example for better understanding:
-
Syntax is :SELECT column_1, column_2FROM table_1INNER JOIN table_2ON table_1.column_name=table_2.column_name
-
Let's see a exampleSELECT FirstName, LastName, DepartmentNameFROM Employee1 eINNER JOIN Department d
-
Now let me explain what has happen now, we have 8 records in our 'Employee1' table out of which for 3 records 'Department column' is null, so null value doesn't represent any record from 'Department table' so inner join has eliminated those records.
-
For remaining 5 records value of 'Department column' mentioned in 'Employee1' table is available in Department table and represent a particular record so those records got merged.
- Left Join
- The left join return all the record from the the table mentioned first and matching records from the table mentioned later. We can also portrait it as all records from table mentioned at Left side of "=" sign and matching record from right table.
- If there is no matching record from right hand side table then the column from right hand side will show their values as null.
- Let's see syntax and example to understand better
-
SyntaxSELECT column_nameFROM table_1LEFT JOIN table_2ON table_1.column_name = table_2.column_name
-
Example :SELECT FirstName, LastName, , DepartmentNameFROM Employee1 eLEFT JOIN Department d
-
Records from 'Employee1' table which are having 'Department column' value null those are represent as null all other values which match 'Id' column of 'Department table' are represented with actual values.
- Right Join
- This one is opposite of Left Join.
- The right join return all the record from the the table mentioned second and matching records from the table mentioned earlier. We can also potrait it as all records from table mentioned at right side of "=" sign and matching record from left table.
- If there is a matching record from left hand side table then the column from left hand side will show their values as null.
- Let's see syntax and example to understand better
-
SyntaxSELECT column_nameFROM table_1RIGHT JOIN table_2ON table_1.column_name = table_2.column_name
-
ExampleSELECT FirstName, LastName, , DepartmentNameFROM Employee1 eRIGHT JOIN Department d
-
If we have a record in Department table not consumed by Employee1 table, then for those records values for column from 'Employee1' table is shown as null
- Full Join
- This one is a combination of left join and right join you can say.
- This will return all the records when there is a match in left hand side table or right hand side table.
- One most imporatnt thing to remeber is when you are using full join then the result-set you will get is going to be potentially large so the query will take time to get execute.
- Let's see synatx and example to understand it better
-
Syntax:SELECT column_nameFROM table_1FULL JOIN table_2ON table_1.coumn_name = table_2.column_name
-
ExampleSELECT FirstName, LastName, DepartmentNameFROM Employee1 eFULL JOIN Department d
-
As you can see we have total 9 records in our result.
-
First 8 are from 'Emploee1' table and last one is from 'Department' table
- Self Join
- As the name suggest self join is there when we join a table to itself.
- You guys must be wondering what is purpose of self join and why would I need to join a table to itself and what people will achieve by doing that.
- I will explain you this concept with example: We have a table with following fields:
- Id
- FirstName
- LastName
- Department
- Salary
- Designation
- Reporting Manager
- In this table the Id's we have mentioned in the 'ReportingManager' column are actually pointing to a record in the same table. The '1' in the 'ReportingManager column refers to the record in 'Employee1' table with 'Id' as '1'.
- Now let's say I want to show Employee Name and whom they report to
-
Now let's see a example:SELECT e1.FirstName, e2.FirstName as Reporting ManagerFROM Employee1 e1JOIN Employee1 e2
-
To understand it more clearly let's make a select query:SELECT Id, FirstName, ReportingManager
-
So here we have Mapped value from 'ReportingManager' column to value in 'Id' column of 'Employee1' table
Now, before ending this blog I want to show one more way of performing join
Example 1:
SELECT e1.FirstName, e2.FirstName as ReportingManager
FROM employee1 e1, employee1 e2
You are getting same result. I'll show you one more example:
SELECT e.FirstName, d. DepartmentName
FROM Employee1 e, Department d
And here we end our joins.
Congratulation SQL. See you in some another blog till then "Keep coding, keep learning because trust me you can make it
big".
"Have a great day"
Superbπππ
ReplyDeleteI completely understand the concept of joins , thanks you .
ReplyDeleteExcellent!!!!! π€
ReplyDeleteI appreciate it ,do some blogs on excel
ReplyDelete