- 博客(13)
- 收藏
- 关注
原创 [LeetCode][Database]题解合集
学完MySql,就兴冲冲跑去做LeetCode上的数据库题,还未做完早已汗颜。题目的众多要求到了自己写的时候,就什么也想不起来,只好上网查各种分析和答案,最终做出来将近一半都是别人的想法。 不过总归是完成了,每道题都在博客里记下了思路和题解,在此立个总集吧。[LeetCode](按通过率从高到低)181 Employees Earning More Than Their Managers
2015-04-11 14:02:38 521
原创 [LeetCode][Database]Department Top Three Salaries
题目来源https://leetcode.com/problems/department-top-three-salaries/ 这道题和之前的Department Highest Salary差不多,只是从每个部门最高工资换成输出每个部门前三名的工资。实在做不出来,后来查到一个很不错的想法。 选出所在部门工资超过他的不超过三人的员工,代码如下: select D.Name, E.Name,
2015-04-11 13:46:29 399
原创 [LeetCode][Database]Nth Hightest Salary
题目来源:https://leetcode.com/problems/nth-highest-salary/输出第n高的工资(像之前那道排名的,重复的只算一个排名) 这是做到目前为止第一次套上函数(代入N)。 这道题是看题解抄的,选择不重复的Salary从Employee中。按Salary从大到小排序,然后通过LIMIT M, 1打出结果的第M行第1列 (似乎N-1不能直接使用,只好在外面定义
2015-04-11 13:26:45 378
原创 [LeetCode][Database]Delete Duplicate Emails
题目来源:https://leetcode.com/problems/delete-duplicate-emails/ 题目要求删除Email相同的Person,并且保留Id最小的那一个那么我们直接用delete语句: delete a from Person a join Person b where a.Email = b.Email and a.Id > b.Id 通过 不知道为什么
2015-04-11 13:13:32 519
原创 [LeetCode][Database]Department Highest Salary
题目来源:https://leetcode.com/problems/department-highest-salary/ 题目意思是,将员工与部门联系起来,输出每个部门工资最高的员工。我的想法不大优美,选择将员工与部门join起来通过员工的DepartmentId和部门的Id。然后记temp为不同部门id和它的最高工资。 最后算出哪些员工在此部门且他的工资为该部门最高工资:select
2015-04-11 13:05:55 391
原创 [LeetCode][Database]Rank Scores
题目来源:https://leetcode.com/problems/rank-scores/ 题目要求很清晰了,要求按分数排序,并输出排名。 相同分数的排在一起,并且不要有空洞,就像样例给出的,有两个并列第一的4.00,3.85分依然算作第二。这道题已经超出我的知识储量了。。只好上网查题解,模仿着写出来了: select s.Score, count(r.Score) as Rank fr
2015-04-11 12:07:22 701
原创 [LeetCode][Database]Consecutive Numbers
题目来源:https://leetcode.com/problems/consecutive-numbers/ 输出连续三次同样的数字(Id连续,Num相同) 一开始当然还是想用之前的做法,连着join三次: select distinct a.Num from Logs a join Logs b join Logs c on a.Id+2 = b.Id+1 = c.Id and a.N
2015-04-11 10:55:49 346
原创 [LeetCode][Database]Second Highest Salary
题目来源:https://leetcode.com/problems/second-highest-salary/For example, given the above Employee table, the second highest salary is 200. If there is no second highest salary, then the query should retur
2015-04-11 10:35:34 449
原创 [LeetCode][Database]Rising Temperature
题目来源:https://leetcode.com/problems/rising-temperature/给出天气表,列出所有当天气温高于昨天的Id。 关键在于TO_DAYS(data)函数,可以给出从年份0开始,到data日期的天数,是个相当方便的函数。 所以我们将a join b条件是a的日期少一天且气温低,选出所有此类条件的b.Id即可:select b.Id from Weather
2015-04-11 10:28:58 653
原创 [LeetCode][Database]Customers Who Never Order
以上例子给出的答案应该是: +————–+ | Customers | +————–+ | Henry | | Max | +—————+这道题照样是用left join可以解决的,具体用法可参见http://www.w3school.com.cn/sql/sql_join_left.asp上面的解释根据题意我们要保证列出所有的顾客名,然后连接上所有的订单,
2015-04-11 10:13:33 466
原创 [LeetCode][Database]Combine Two Tables
以上为数据库表的介绍,有Person与Address两种,前者以PersonId为主码,后者以AddressId为主码。 题目要求为: Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is
2015-04-11 09:56:46 464
原创 [LeetCode][Database]Duplicate Emails
题目要求查询并输出所有不同的Email值,也是道基础题了,不过通过率为38.8%。看来还有好多人和我一样,对Mysql的细节不是很了解啊。我也在Person为空时出过错。最终通过的代码如下:# Write your MySQL query statement belowSELECT DISTINCT a.Email FROM Person a, Person b WHERE a.Ema
2015-04-11 09:47:55 374
原创 [LeetCode][Database]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 | +—-+——-+
2015-04-11 09:43:19 428
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人