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

How to automatically deploy Linux system using PXE

Table of contents Background Configuring DHCP Edi...

Differences and comparisons of storage engines in MySQL

MyISAM storage engine MyISAM is based on the ISAM...

Basic learning and experience sharing of MySQL transactions

A transaction is a logical group of operations. E...

Write a publish-subscribe model with JS

Table of contents 1. Scene introduction 2 Code Op...

Implementation of Docker deployment of Tomcat and Web applications

1. Download docker online yum install -y epel-rel...

JavaScript clicks the button to generate a 4-digit random verification code

This article example shares the specific code of ...

Web designers also need to learn web coding

Often, after a web design is completed, the desig...

Detailed explanation of Vue two-way binding

Table of contents 1. Two-way binding 2. Will the ...

Make your website run fast

Does performance really matter? Performance is im...

How to build Apr module for tomcat performance optimization

Preface Tomcat is a widely used Java web containe...

Tutorial on installing MYSQL5.7 from OEL7.6 source code

First, download the installation package from the...

How to use nginx to intercept specified URL requests through regular expressions

nginx server nginx is an excellent web server tha...

CSS element hiding principle and display:none and visibility:hidden

1. CSS element hiding <br />In CSS, there ar...