Find Engineering Employees Who Earn More Than Their Manager

Quick Overview

Write a SQL query that returns employees in the Engineering department who earn more than their manager, sorted by name. It tests self-joins on a manager hierarchy, combining a self-join with a lookup table, handling employees with no manager, and keeping duplicate names.

Find Engineering Employees Who Earn More Than Their Manager

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Find the names of employees in the Engineering department who earn more than their manager. `manager_id` references another employee, possibly in a different department. Return a single column `Employee`, ordered by employee name ascending. ```hint Two salaries per row Each comparison needs the employee's salary and the manager's salary side by side, and both live in the `employees` table. ``` ### Input Table(s) Table: `departments` | Column | Type | Meaning | |---|---|---| | id | INT | Department identifier (primary key) | | name | VARCHAR(100) | Department name, not null | Table: `employees` | Column | Type | Meaning | |---|---|---| | id | INT | Employee identifier (primary key) | | name | VARCHAR(100) | Employee name, not null, not necessarily unique | | salary | INT | Employee salary, not null | | department_id | INT | The employee's department (foreign key to `departments.id`) | | manager_id | INT | The employee's manager (foreign key to `employees.id`), NULL for top-level employees | Keys: `departments.id` and `employees.id` are primary keys. `employees.department_id` references `departments.id`, and `employees.manager_id` references `employees.id`. ### Output Contract - One column named `Employee`, holding the employee's `name`. - One row per qualifying employee. Two different employees with the same name both appear. - Sort by `Employee` ascending, then by the employee's `id` ascending so that equal names have a fixed order. - No rounding is involved. ### Example `departments` | id | name | |---|---| | 1 | Engineering | | 2 | Sales | `employees` | id | name | salary | department_id | manager_id | |---|---|---|---|---| | 1 | Alice | 90000 | 1 | NULL | | 2 | Bob | 100000 | 1 | 1 | | 3 | Charlie | 80000 | 1 | 1 | | 4 | Diana | 95000 | 2 | 1 | Expected output: | Employee | |---| | Bob | Bob works in Engineering and earns 100000, which is more than his manager Alice (90000). Charlie works in Engineering but earns less than Alice. Diana earns more than Alice but is in Sales, not Engineering. Alice has no manager, so there is nothing to compare her against. ### Constraints and Clarifications - `employees` has at most 10,000 rows and `departments` at most 100 rows. - `salary` is a non-negative integer and is never NULL. - `manager_id` is NULL for top-level employees. They have no manager and are not returned. - "More than" is strict. An employee who earns exactly the manager's salary is not returned. - Only the employee has to be in Engineering. The manager can be in any department. - An employee is in Engineering when the `name` of their department is exactly `'Engineering'`. - Employee names are not unique. Do not merge two employees who share a name.

Overview: Write a SQL query that returns employees in the Engineering department who earn more than their manager, sorted by name. It tests self-joins on a manager hierarchy, combining a self-join with a lookup table, handling employees with no manager, and keeping duplicate names.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Find the names of employees in the Engineering department who earn more than their manager. manager_id references another employee, possibly in a different department. Return a single column Employee, ordered by employee name ascending.

Input Table(s)

Table: departments

ColumnTypeMeaning
idINTDepartment identifier (primary key)
nameVARCHAR(100)Department name, not null

Table: employees

ColumnTypeMeaning
idINTEmployee identifier (primary key)
nameVARCHAR(100)Employee name, not null, not necessarily unique
salaryINTEmployee salary, not null
department_idINTThe employee's department (foreign key to departments.id)
manager_idINTThe employee's manager (foreign key to employees.id), NULL for top-level employees

Keys: departments.id and employees.id are primary keys. employees.department_id references departments.id, and employees.manager_id references employees.id.

Output Contract

  • One column named Employee , holding the employee's name .
  • One row per qualifying employee. Two different employees with the same name both appear.
  • Sort by Employee ascending, then by the employee's id ascending so that equal names have a fixed order.
  • No rounding is involved.

Example

departments

idname
1Engineering
2Sales

employees

idnamesalarydepartment_idmanager_id
1Alice900001NULL
2Bob10000011
3Charlie8000011
4Diana9500021

Expected output:

Employee
Bob

Bob works in Engineering and earns 100000, which is more than his manager Alice (90000). Charlie works in Engineering but earns less than Alice. Diana earns more than Alice but is in Sales, not Engineering. Alice has no manager, so there is nothing to compare her against.

Constraints and Clarifications

  • employees has at most 10,000 rows and departments at most 100 rows.
  • salary is a non-negative integer and is never NULL.
  • manager_id is NULL for top-level employees. They have no manager and are not returned.
  • "More than" is strict. An employee who earns exactly the manager's salary is not returned.
  • Only the employee has to be in Engineering. The manager can be in any department.
  • An employee is in Engineering when the name of their department is exactly 'Engineering' .
  • Employee names are not unique. Do not merge two employees who share a name.