In my previous video of mysql series I have discussed about cross join in MySQL
You can check the video from the above I button
Today I am going to discuss about what is self join in mysql with example and where it is used so without any further delay let's get started
What is self join in MySQL?
A self join allows to join a table to itself. Since MySQL does not have specific self join syntax,so self join is performed via a regular join such as left join or inner join.
Since same table is referenced within a single query, so there is a need to use table aliases to assign the table a temporary name when the same table is referenced for the second time
Where Self join is used?
Hierarchical data: When dealing with hierarchical structures, such as organizational charts or category trees, to retrieve parent-child relationships.
Versioning or tracking changes: For tracking changes in data, like version history or order tracking, by comparing records in the same table.
Recursive queries: To perform recursive operations, like finding all descendants or ancestors of a particular record.
Network or graph data: For working with data where nodes or entities have connections to other nodes in the same table.
MySQL self join syntax:-
SELECT *
FROM table_name AS t1
JOIN table_name AS t2 ON t1.column_name = t2.column_name;
So let's see this with some examples
Today I am using customers table under company 22
Self join using inner join
Since it's self join so we will use only 1 table but we will give 2 different alias to identify both identical tables
Select all the required column names
From customers C1
Inner join customers C2
On C1.Id = C2.Id;
Specify the selected columns
c1.EMPLOYEENAME AS emp_name, c1.AGE AS emp_age,c2.SALARY AS emp_salary
Select and run the query to see the result
You can also use left join and right join to achieve self join
that's all for today Hope you all will get some help from this video
If it's so then don't forget to subscribe my channel Code with Neha and press the Bell Icon for Regular Updates
See you in the next video till then Stay Safe Stay Happy
#selfjoin
#selfjoininmysql
#selfjoininsql
#mysqlselfjoin
#sqlselfjoin
#selfjoinusinginnerjoin
#sqljoins
#mysqldatabase
#mysqltutorialforbeginners
#mysqlserver
#SQLJOINS
#JoinsinSQL
#SQLJoinsTutorial
#SQLJOINSTutorialforbeginners
#SQLJOINQuerieswithExamples
#introductiontojoininsql
#Joininmysql
#sqljoins
#sqltypesofjoins
#sqljoinstutorialforbeginners
#sqljoinswithexample
#sqljoinsexamples
#SQLqueriesusingJoins
#SQLJoinqueries
#joins
#joinsindbms
#typesofjoinsindbms
#needofjoins
#joinsexample
#joinvssubquery
#typesofjoins