Skip to content

Latest commit

History

History
116 lines (97 loc) 路 2.54 KB

181. Employees Earning More Than Their Managers.md

File metadata and controls

116 lines (97 loc) 路 2.54 KB

1. Description

SQL Schema:
CREATE TABLE IF NOT EXISTS Employee (
    id int,
    name varchar(255),
    salary int,
    managerId int
) TRUNCATE TABLE Employee
INSERT INTO
    Employee (id, name, salary, managerId)
VALUES
    ('1', 'Joe', '70000', '3')
INSERT INTO
    Employee (id, name, salary, managerId)
VALUES
    ('2', 'Henry', '80000', '4')
INSERT INTO
    Employee (id, name, salary, managerId)
VALUES
    ('3', 'Sam', '60000', 'None')
INSERT INTO
    Employee (id, name, salary, managerId)
VALUES
    ('4', 'Max', '90000', 'None')

Table: Employee

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
| salary      | int     |
| managerId   | int     |
+-------------+---------+

id is the primary key column for this table.
Each row of this table indicates the ID of an employee, their name, salary, and the ID of their manager.

Write an SQL query to find the employees who earn more than their managers.

Return the result table in any order.

The query result format is in the following example.

Example 1:

Input:
Employee table:
+----+-------+--------+-----------+
| id | name  | salary | managerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | Null      |
| 4  | Max   | 90000  | Null      |
+----+-------+--------+-----------+
Output:
+----------+
| Employee |
+----------+
| Joe      |
+----------+
Explanation: Joe is the only employee who earns more than his manager.

2. Solutions

Solution 1: Language: SQL Using WHERE

  • Tuesday, 29 November, 2022
  • Runtime: 636 ms, faster than 34.55% of MySQL online submissions for Employees Earning More Than Their Managers.
SELECT
    employees.name AS Employee
FROM
    Employee AS employees,
    Employee AS managers
WHERE (
    employees.managerId = managers.id
    AND
    employees.salary > managers.salary
);

Solution 2: Language: SQL Using JOIN

  • Tuesday, 29 November, 2022
  • Runtime: 676 ms, faster than 30.70% of MySQL online submissions for Employees Earning More Than Their Managers.
SELECT
    employees.name AS Employee
FROM
    Employee AS employees
JOIN
    Employee AS managers
ON (
    employees.managerId = managers.id
    AND
    employees.salary > managers.salary
);