LeetCode 数据库181,182,183,184,185整理
2017-07-02 18:33
176 查看
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 Employee FROM Employee E1, Employee E2 WHERE E1.ManagerId = E2.Id AND 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 Email FROM Person GROUP BY Email HAVING 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 Customers FROM (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.Salary FROM Department d, Employee e, (SELECT MAX(Salary) as maxSal, DepartmentId FROM Employee GROUP BY DepartmentId) temp WHERE e.DepartmentId = d.Id AND e.Salary = temp.maxSal AND e.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.Salary FROM Department d, Employee e WHERE (SELECT COUNT(DISTINCT(Salary)) FROM Employee ee WHERE ee.Salary > e.Salary AND ee.DepartmentId = e.DepartmentId) < 3 AND e.DepartmentId = d.Id ORDER by e.DepartmentId, e.Salary DESC;
相关文章推荐
- Ectra Credit 180,181,182,183,184,185,186,187,188,189,190
- leetcode_sql_3,181,182,183
- LeetCode Database(181-185)
- LeetCode 数据库262,601整理
- leetcode 184,185 Department Top Three Salaries/HIGHEST Salaries
- 学习ADO和ODBC访问数据库的一些思路整理
- oracle笔记整理五[创建数据库]
- C#中数据库操作相关我给整理的一个类供大家使用完善
- ACEESS数据库字段类型搜集整理测试通过
- SQL Server 文档整理 - 2 清空所有数据库表记录的存储过程
- 数据库暴库整理篇[收藏]
- 最新MDB数据库操作大全(自己搜集整理基本上很全了)
- 数据库设计指南[整理]
- 数据库暴库整理篇
- ACEESS数据库字段类型搜集整理测试通过
- JSP访问数据库的问题整理
- SQL Server 2000 (SP4)笔记整理(一):数据库
- 一次数据库的整理的sql语句
- 常用数据库JDBC连接写法(整理)
- 数据库暴库整理篇