SQL implementation of LeetCode (181. Employees earn more than managers)

SQL implementation of LeetCode (181. Employees earn more than managers)

[LeetCode] 181.Employees Earning More Than Their Managers

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+

Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.

+----------+
|Employee |
+----------+
| Joe |
+----------+

This question gives us an Employee table, which contains the salary information of employees and their managers. Managers are also employees, and their manager ID is empty. Let's find out the employees whose salary is higher than their managers. Then it is a very simple comparison problem. We can generate two instance objects to interpolate through ManagerId and Id, and then restrict the condition that one Salary is greater than the other:

Solution 1:

SELECT e1.Name FROM Employee e1
JOIN Employee e2 ON e1.ManagerId = e2.Id
WHERE e1.Salary > e2.Salary;

We can also skip Join and directly write all the conditions into where:

Solution 2:

SELECT e1.Name FROM Employee e1, Employee e2
WHERE e1.ManagerId = e2.Id AND e1.Salary > e2.Salary;

References:

https://leetcode.com/discuss/88189/two-straightforward-way-using-where-and-join

This is the end of this article about SQL implementation of LeetCode (181. Employees earn more than managers). For more relevant SQL implementation of employees earning more than managers, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • SQL implementation of LeetCode (196. Delete duplicate mailboxes)
  • SQL implementation LeetCode (185. Top three highest salaries in the department)
  • SQL implementation of LeetCode (184. The highest salary in the department)
  • SQL implementation of LeetCode (183. Customers who have never placed an order)
  • SQL implementation of LeetCode (182. Duplicate mailboxes)
  • SQL implements LeetCode (180. Continuous numbers)
  • C++ implementation of LeetCode (179. Maximum number of combinations)
  • SQL implementation of LeetCode (197. Rising temperature)

<<:  How to use CSS to fill the parent container div with img images and adjust the container size

>>:  How to make an input text box change length according to its content

Recommend

Advanced Usage Examples of mv Command in Linux

Preface The mv command is the abbreviation of mov...

js to achieve simple drag effect

This article shares the specific code of js to ac...

Windows 10 1903 error 0xc0000135 solution [recommended]

Windows 10 1903 is the latest version of the Wind...

Docker Data Storage Volumes Detailed Explanation

By default, the reading and writing of container ...

Enterprise-level installation tutorial using LAMP source code

Table of contents LAMP architecture 1.Lamp Introd...

Conditional comments to determine the browser (IE series)

<!--[if IE 6]> Only IE6 can recognize <![...

MySQL InnoDB MRR Optimization Guide

Preface MRR is the abbreviation of Multi-Range Re...

How to set up the use of Chinese input method in Ubuntu 18.04

In the latest version of Ubuntu, users no longer ...

An article to understand the execution process of MySQL query statements

Preface We need to retrieve certain data that mee...

Explanation of several ways to run Tomcat under Linux

Starting and shutting down Tomcat under Linux In ...

Nginx load balancing algorithm and failover analysis

Overview Nginx load balancing provides upstream s...

You Probably Don’t Need to Use Switch Statements in JavaScript

Table of contents No switch, no complex code bloc...