LeetCode 数据库181,182,183,184,185整理

来源:互联网 发布:石醒宇 知乎 编辑:程序博客网 时间:2024/05/21 06:26

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      |+----------+

SQL脚本:

SELECT E1.Name AS EmployeeFROM Employee E1, Employee E2WHERE E1.ManagerId = E2.IdAND E1.Salary > E2.Salary

182. Duplicate Emails

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

+----+---------+| Id | Email   |+----+---------+| 1  | a@b.com || 2  | c@d.com || 3  | a@b.com |+----+---------+

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

+---------+| Email   |+---------+| a@b.com |+---------+

Note: All emails are in lowercase.

SQL脚本

SELECT EmailFROM PersonGROUP BY EmailHAVING COUNT(Email) > 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       |+-----------+

SQL脚本:

SELECT Customers.Name AS CustomersFROM (Customers LEFT OUTER JOIN Orders on Customers.Id=Orders.CustomerId)WHERE Orders.Id IS NULL;

184. Department Highest Salary

The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.

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

The Department table holds all departments of the company.

+----+----------+| Id | Name     |+----+----------+| 1  | IT       || 2  | Sales    |+----+----------+

Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.

+------------+----------+--------+| Department | Employee | Salary |+------------+----------+--------+| IT         | Max      | 90000  || Sales      | Henry    | 80000  |+------------+----------+--------+

SQL脚本

SELECT d.Name as Department, e.Name as Employee, e.SalaryFROM Department d, Employee e,    (SELECT MAX(Salary) as maxSal, DepartmentId FROM Employee GROUP BY DepartmentId) tempWHEREe.DepartmentId = d.Id ANDe.Salary = temp.maxSal ANDe.DepartmentId = temp.DepartmentId;

185. Department Top Three Salaries

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

+----+-------+--------+--------------+| Id | Name  | Salary | DepartmentId |+----+-------+--------+--------------+| 1  | Joe   | 70000  | 1            || 2  | Henry | 80000  | 2            || 3  | Sam   | 60000  | 2            || 4  | Max   | 90000  | 1            || 5  | Janet | 69000  | 1            || 6  | Randy | 85000  | 1            |+----+-------+--------+--------------+

The Department table holds all departments of the company.

+----+----------+| Id | Name     |+----+----------+| 1  | IT       || 2  | Sales    |+----+----------+

Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.

+------------+----------+--------+| Department | Employee | Salary |+------------+----------+--------+| IT         | Max      | 90000  || IT         | Randy    | 85000  || IT         | Joe      | 70000  || Sales      | Henry    | 80000  || Sales      | Sam      | 60000  |+------------+----------+--------+

SQL脚本:

SELECT d.Name AS Department, e.Name AS Employee, e.SalaryFROM Department d, Employee eWHERE (SELECT COUNT(DISTINCT(Salary)) FROM Employee ee WHERE ee.Salary > e.Salary AND ee.DepartmentId = e.DepartmentId) < 3AND e.DepartmentId = d.IdORDER by e.DepartmentId, e.Salary DESC;
原创粉丝点击