leetcode_sql_3,181,182,183

Posted coskaka

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode_sql_3,181,182,183相关的知识,希望对你有一定的参考价值。

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.

 

# Write your MySQL query statement below
SELECT A.Name Employee
FROM Employee A,Employee B
WHERE A.ManagerId=B.Id AND A.Salary >B.Salary;

 

182. Duplicate Emails

Write a SQL query to find all duplicate emails in a table named Person.

+----+---------+
| Id | Email   |
+----+---------+
| 1  | [email protected] |
| 2  | [email protected] |
| 3  | [email protected] |
+----+---------+

For example, your query should return the following for the above table:

+---------+
| Email   |
+---------+
| [email protected] |
+---------+

Note: All emails are in lowercase.

 

# Write your MySQL query statement below
SELECT Distinct(a.Email)
FROM Person a,Person b
WHERE a.Id<>b.Id and a.Email=b.Email

or

select Email
from Person
group by Email
having count(*) > 1

 

183. Customers Who Never Order

Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.

Table: Customers.

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+

Table: Orders.

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

Using the above tables as example, return the following:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+

 SELECT A.Name from Customers A LEFT JOIN Orders B on a.Id = B.CustomerId WHERE b.CustomerId is NULL;

 

select c.Name from Customers c
where (select count(*) from Orders o where o.customerId=c.id)=0

 

select c.Name from Customers c
where not exists (select * from Orders o where o.customerId=c.id)

 

 

 

 











以上是关于leetcode_sql_3,181,182,183的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-MySQL练习2(180/181/1777/182/196/197/1179)(行转列)(datediff/timestampdiff)

leetcode_sql_2

leetcode_sql_4,196

leetcode_sql_1

Leetcode & Java & SQL#面试题16.11 / 175 / 176 / 181 / 182

pySpark 使用键/值从 RDD 创建 DataFrame