MySQL Self Join: INNER JOIN a Table to Itself

A self join uses the same table twice with different aliases. Here the employee table is joined to itself so each employee can be matched to the row representing that employee's manager.

INNER JOIN supplies the matching behavior; aliases such as e and m distinguish the two roles played by the same table.

INNER join SQL command is mostly used to join one table to it self. The biggest advantage of doing this is to get linking information from the same table.

The best example of INNER join will be employee table where we will keep the employee and its manager as a single record. This way by linking to the table it self we will generate a report displaying it as two linked tables. Each record will have one additional field storing the data of the manager by keeping the employee ID and we will use M_ID ( manager ID ) to link with main employee ID. This way we will link two virtual tables generated from one main table. Here is the table. You can download /copy the sql dump file to create your own MySQL table for testing.
Main tableManagersEmployee
idnamem_id
1John2
2Greek Tor3
3Alex JohnNULL
4Mike tour1
5Brain J3
6Ronald3
7Kin4
8Herod3
9Alen2
10Ronne1
idemp_name
2Greek Tor
3Alex John
1John
3Alex John
3Alex John
4Mike tour
3Alex John
2Greek Tor
1John
idemp_name
1John
2Greek Tor
3Alex John
4Mike tour
5Brain J
6Ronald
7Kin
8Herod
9Alen
10Ronne
Main Table (emp ): Table with id , name and m_id. Each employ has one unique id and one m_id ( manager id which is part of id field )

Note that we have only one table main table and other two Managers and Employee reports are generated out of the main table only.

In the table you can see every record has one manager id field known as m_id. We have used the unique id of the employee in the m_id field to mark who is the manager for the employee.

Employee Manager report

Now let us use inner join to create one report to display who is the manager of which employee. Check this SQL
SELECT e.id, e.name AS employee, m.name AS manager
FROM emp AS e
INNER JOIN emp AS m ON m.id = e.m_id
ORDER BY e.id;
id emp_name manager
1JohnGreek Tor
2Greek TorAlex John
4Mike tourJohn
5Brain JAlex John
6RonaldAlex John
7KinMike tour
8HerodAlex John
9AlenGreek Tor
10RonneJohn
The INNER JOIN omits Alex John because that row has no manager. Use a LEFT JOIN when you want employees without managers to remain in the result.

INNER JOIN with DISTINCT Query


To generate the manager table we have used this SQL ( List all the managers )
SELECT DISTINCT m.id, m.name AS manager
FROM emp AS e
INNER JOIN emp AS m ON m.id = e.m_id
ORDER BY m.id;

Read More on Distinct Query .

CREATE TABLE EMP (
  id INT NOT NULL AUTO_INCREMENT,
  name VARCHAR(25) NOT NULL,
  m_id INT NULL,
  PRIMARY KEY (id),
  INDEX IDX_EMP_MANAGER (m_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO EMP (id, name, m_id) VALUES
(1, 'John', 2),
(2, 'Greek Tor', 3),
(3, 'Alex John', NULL),
(4, 'Mike tour', 1),
(5, 'Brain J', 3),
(6, 'Ronald', 3),
(7, 'Kin', 4),
(8, 'Herod', 3),
(9, 'Alen', 2),
(10, 'Ronne', 1);

SQL RIGHT JOIN LEFT JOIN SQL CROSS JOIN

JOIN types · LEFT JOIN · Multiple-table LEFT JOIN · DISTINCT




Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer