Vous êtes sur la page 1sur 4

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 CREATE TABLE employees (


2
3 emp_id int(11) NOT NULL,
emp_name varchar(45) DEFAULT NULL,
4
mng_id int(11) DEFAULT NULL,
5
PRIMARY KEY (emp_id)
6
)
7

Insert Dummy Records:

1 INSERT INTO employees (emp_id,emp_name,mng_id) VALUES (1,'Joe','2');


2 INSERT INTO employees (emp_id,emp_name,mng_id) VALUES
(2,'Green',NULL);
3 INSERT INTO employees (emp_id,emp_name,mng_id) VALUES (3,'Jen','2');
4 INSERT INTO employees (emp_id,emp_name,mng_id) VALUES (4,'Brown','1');
5 INSERT INTO employees (emp_id,emp_name,mng_id) VALUES (5,'Harry','1');
6 INSERT INTO employees (emp_id,emp_name,mng_id) VALUES (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.

29/479

ROWID Pseudocolumn
For each row in the database, the ROWID pseudocolumn returns the address of the row. Oracle Database
rowid values contain information necessary to locate a row:

 The data object number of the object

 The data block in the datafile in which the row resides

 The position of the row in the data block (first row is 0)

 The datafile in which the row resides (first file is 1). The file number is relative to the tablespace.
Usually, a rowid value uniquely identifies a row in the database. However, rows in different tables that
are stored together in the same cluster can have the same rowid.

Values of the ROWID pseudocolumn have the datatype ROWID or UROWID. Please refer to "ROWID
Datatype" and "UROWID Datatype" for more information.

Rowid values have several important uses:

 They are the fastest way to access a single row.

 They can show you how the rows in a table are stored.

 They are unique identifiers for rows in a table.

You should not use ROWID as the primary key of a table. If you delete and reinsert a row with the Import
and Export utilities, for example, then its rowid may change. If you delete a row, then Oracle may
reassign its rowid to a new row inserted later.

Although you can use the ROWID pseudocolumn in the SELECT and WHERE clause of a query, these
pseudocolumn values are not actually stored in the database. You cannot insert, update, or delete a
value of the ROWID pseudocolumn.

Example This statement selects the address of all rows that contain data for employees in department
20:

SELECT ROWID, last_name


FROM employees
WHERE department_id = 20;

SELECT * /*This is the outer query part */


FROM Employee Emp1
WHERE (N-1) = ( /* Subquery starts here */
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)

 select sal from


 (select emp.*,
 dense_rank() over (order by sal desc) rank
 from emp)
where rank=3;

Vous aimerez peut-être aussi