QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › Interview tasks

Task: employees who earn more than their manager.

Employees who earn more than their manager are found with a self join: the table is taken twice under aliases and joined on m.id = e.manager_id. The filter e.salary > m.salary goes in WHERE. INNER JOIN fits, since an employee without a manager has nobody to compare with, and a NULL salary makes the comparison unknown. One result row is checked against the table, because a reversed join gives a plausible but wrong answer.

SQL · Tasks

The manager and the subordinate sit in the same table. How to compare their salaries?

In the employees table a manager is an employee too: the manager_id column points to the id of another row of the same table. To compare an employee's salary with the manager's, both rows must be in one row of the result. A self join does this: it joins the table to itself.

idnamesalarymanager_id
1Maria5000NULL
2Taras40001
3Olena45002
4Petro30002
5IrynaNULL2

How it works and why. One table is taken twice under different aliases: e plays the employee, m plays the manager. Without aliases the DBMS could not tell which of the two copies a column belongs to. The join condition m.id = e.manager_id reads as "the manager is the row whose id is stored in the employee's manager_id". The filter e.salary > m.salary sits in WHERE and works on the already joined pairs.

SELECT e.name AS employee, e.salary,
       m.name AS manager,  m.salary AS manager_salary
FROM employees e
JOIN employees m ON m.id = e.manager_id
WHERE e.salary > m.salary;
-- Olena | 4500 | Taras | 4000

Self join, then WHERE

After JOIN
employeesalmanagermsal
Taras4000Maria5000
Olena4500Taras4000
Petro3000Taras4000
IrynaNULLTaras4000
After WHERE
employeesalmanagermsal
Olena4500Taras4000
Figure 1. Maria never entered the join: she has no manager. Iryna fell out at the comparison: NULL > 4000 is UNKNOWN.

INNER JOIN is appropriate here: an employee without a manager (Maria) cannot earn more than him, so losing that row is correct. A LEFT JOIN would be needed for another question: "show all employees and their managers' names where they exist".

Example: swapped keys. A typical self-join mistake is to swap the sides of the condition. The query below looks right, runs and returns two rows, but it answers a different question.

SELECT e.name AS employee, e.salary,
       m.name AS manager,  m.salary AS manager_salary
FROM employees e
JOIN employees m ON m.manager_id = e.id   -- reversed
WHERE e.salary > m.salary;
-- Maria | 5000 | Taras | 4000
-- Taras | 4000 | Petro | 3000

Diagnosis: now e is the manager and m is his subordinate, but the columns are labelled "employee" and "manager". The result lists managers who earn more than their subordinates, which is the majority in any company, so it looks plausible. The check is simple: take one row of the result and look in the table whether the employee's manager_id really points to the named manager.

Limits and alternatives. The same can be written with a correlated subquery: WHERE e.salary > (SELECT m.salary FROM employees m WHERE m.id = e.manager_id). For an employee without a manager the subquery gives NULL and the row drops out the same way. A self join compares only with the direct manager. If the question is about any boss in the chain, recursion is needed: WITH RECURSIVE. NULL in salary makes the comparison unknown: an employee with an unspecified salary is not in the answer, and this is better said out loud than decided silently.

Common mistakes.

  • No aliases: an error "table name … specified more than once", and with an alias on only one copy, "column reference … is ambiguous".
  • A reversed join condition: a plausible result about managers instead of subordinates.
  • The condition e.manager_id = m.manager_id: comparison with colleagues, not with the manager.
  • LEFT JOIN without need: rows with a NULL manager salary drop out in WHERE anyway.

Check yourself

Why is Iryna (salary = NULL) not in the answer, and should she be added?

Answer

NULL > 4000 gives UNKNOWN, and WHERE keeps only true conditions. She should not be added: an unknown salary does not prove it is greater. But the very existence of an employee with no salary may be a data defect, and that is worth reporting separately.

What does the query with ON m.manager_id = e.id instead of ON m.id = e.manager_id return on our data?

Answer

Two pairs where "e" is actually the manager: Maria 5000 against Taras 4000 and Taras 4000 against Petro 3000. The pair Taras against Olena (4000 against 4500) fails the filter, and Iryna gives UNKNOWN. The result is wrong in meaning although not empty.

In an interview. Junior: It is a self join: I join the employees table to itself, where the manager is another row by manager_id, and compare the salaries. Middle/Senior: INNER JOIN fits here, because an employee without a manager cannot earn more than him, and a NULL salary gives an unknown comparison. After writing it I check one result row against the table to make sure the keys are not swapped. For an arbitrary chain of bosses I use WITH RECURSIVE.

More articles in this chapter