Vous êtes sur la page 1sur 2

Employee Id, Employee Name and Manager Id Self Join

SQL Query as Interview Question


February 10, 2014 Bikash Shaw Leave a comment Go to comments
One of the most common SQL interview question related to Self Join is that one Employee table
with three columns Employee Id, Employee Name and Manager Id.
The interviewer may ask you questions like the following:
1. Find the employees who are managers.
2. Find the managers with the count of subordinates
3. Find the managers with the count of subordinates greater than 1 (or any other number)
Lets try resolve this with the basic understanding what is Self Join?: A self join is a join in
which a table is joined with itself (which is also called Unary relationships), specially when the
table has a FOREIGN KEY which references its own PRIMARY KEY. To join a table itself means that
each row of the table is combined with itself and with every other row of the table.
W3Reaource
At first create an Employee table and insert some dummy records into it. Please refer to the
CREATE and INSERT statements below:
Create Employee Table:
1
2
3
4
5
6
7

CREATE TABLE employees (


emp_id int(11) NOT NULL,
emp_name varchar(45) DEFAULT NULL,
mng_id int(11) DEFAULT NULL,
PRIMARY KEY (emp_id)
)

Insert Dummy Records:


1 INSERT INTO employees
2 INSERT INTO employees
3 INSERT INTO employees
4 INSERT INTO employees
5 INSERT INTO employees
INSERT INTO employees
6

(emp_id,emp_name,mng_id)
(emp_id,emp_name,mng_id)
(emp_id,emp_name,mng_id)
(emp_id,emp_name,mng_id)
(emp_id,emp_name,mng_id)
(emp_id,emp_name,mng_id)

VALUES
VALUES
VALUES
VALUES
VALUES
VALUES

(1,'Joe','2');
(2,'Green',NULL);
(3,'Jen','2');
(4,'Brown','1');
(5,'Harry','1');
(6,'Jenny','4');

1. SQL Statement for Find the employees who are managers:


1 SELECT DISTINCT e.emp_id AS 'mng_id', e.emp_name AS 'mng_name'
2 FROM employees e, employees m WHERE e.emp_id = m.mng_id
2. In this query you can see the join condition e.emp_id = m.mng_id where both e and m
aliases are of Employees table.
3. SQL Statement for Find the managers with the count of subordinates:
1 SELECT COUNT(emp.emp_id) AS 'emp_count', mng.mng_name AS 'mng_name'
2 FROM employees emp,
3 (SELECT DISTINCT e.emp_id AS 'mng_id', e.emp_name AS 'mng_name'
4 FROM employees e, employees m
5 WHERE e.emp_id = m.mng_id) mng
6 WHERE emp.mng_id = mng.mng_id
7 GROUP BY mng.mng_id
4. In this query we are getting all the managers names with their subordinates counts.
5. SQL Statement for Find the managers with the count of subordinates greater than
1 (or any other number):
1 SELECT COUNT(emp.emp_id) AS 'emp_count', mng.mng_name AS 'mng_name'
2 FROM employees emp,
3 (SELECT DISTINCT e.emp_id AS 'mng_id', e.emp_name AS 'mng_name'
4 FROM employees e, employees m
5 WHERE e.emp_id = m.mng_id) mng
6 WHERE emp.mng_id = mng.mng_id
7 GROUP BY mng.mng_id HAVING COUNT(emp.emp_id) > 1;
6. In this query we need to just add additional Having clause to find out all the managers
who have more than specific number of employees.
I hope this post would be helpful for you. Please feel free to leave your comments below in case
if you have any suggestion or if you have better optimized solution.

Vous aimerez peut-être aussi