Self join in mysql employee manager example
In this blog post, we will learn self join of MySQL using the employee and manager example. In other words, A self join involves using a single table twice in a query, with each instance assigned a different alias. This enables comparisons or relationships between different rows within the same table.
In table we will create a column manager_id which specifies the manager of an employee and we will compare the employees manager_id column with id column of another instance of employee table.
Create employees table
CREATE TABLE `employees` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`manager_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `manager_id` (`manager_id`),
CONSTRAINT `employees_ibfk_1` FOREIGN KEY (manager_id) REFERENCES employees(id)
);
Insert data into employees table
insert into employees (name, manager_id) values('John', null), ('Diana', 1), ('David', 1);
Output :
Query OK, 3 rows affected (0.38 sec) Records: 3 Duplicates: 0 Warnings: 0
Self Join Statment to get Employees and their manager name
select e.id, e.name , m.name as manager from employees e left join employees m on e.manager_id = m.id;
Output
+----+-------+---------+ | id | name | manager | +----+-------+---------+ | 1 | John | NULL | | 2 | Diana | John | | 3 | David | John | +----+-------+---------+ 3 rows in set (0.00 sec)
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Sql
- ERROR 3780 (HY000): Referencing column 'order_id' and referenced column 'id' in foreign key constraint 'order_items_ibfk_1' are incompatible.
- Database connection using mySQLi with object-oriented
- Get create table Query from existing table in phpMyAdmin
- How to retrieve data from two tables with one SQL statement
- Cannot drop index 'fk_role_id': needed in a foreign key constraint
- SQL query to delete all rows older than 30 days
- Clear the terminal screen in MySQL within the Windows powershell
- Get data in random order with limit from table using mysql
- How to create view in MySQL database
- Create Index in MySQL with example
- How to drop function in MySQL ?
- How to show database tables in mysql using command line
- SQL query to delete records older than 6 months
- How to get the list of all constraints in MySQL database
- MySQL create procedure example
- How to create a new table from existing table in SQL
- Get comma separated ids from table using MySQL
- Change existing MySQL table column id to autoincrement
- SQLSTATE[01000]: Warning: 1265 Data truncated for column 'visibility' at row 1
- How to check column exists or not in table using MySQL
- Insert multiple rows in a single MySQL query
- How to get yesterday's date in MySQL ?
- How to Rename column name in MySql ?
- Get number of connections being used by a specific user in MySQL
- How to get a list of MySQL views?