176. 第二高的薪水
外面再嵌套一层select使第二高的薪水为null时也能查询到。
limit offset,rows 代表距离初始行的偏移量,要记录的行数。
注意第一行的偏移量是0,这道题第二行的偏移量为1。
select (select distinct Salary from Employee
order by Salary desc limit 1,1) as SecondHighestSalary
177. 第N高的薪水
和上一题类似。
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
Set N = N-1;
RETURN (
# Write your MySQL query statement below.
select (select distinct Salary from Employee
order by Salary desc limit N,1) as SecondHighestSalary
);
END
178. 分数排名
现在给定五个成绩:99,99,85,80,75。
DENSE_RANK() 排名会得到:1,1,2,3,4。
RANK() 排名会得到:1,1,3,4,5。
ROW_NUMBER() 排名会得到:1,2,3,4,5。
语法:
DENSE_RANK() OVER ( PARTITION BY <expression> ORDER BY <expression> [ASC|DESC])
select Score, dense_rank() over (order by Score desc) as 'Rank' from Scores;
179. 最大数
class Solution {
public String largestNumber(int[] nums) {
String[] strs = new String[nums.length];
for (int i = 0; i < nums.length; i++) {
strs[i] = String.valueOf(nums[i]);
}
Arrays.sort(strs, (a, b) -> (b + a).compareTo(a + b));
if (strs[0].equals("0")) {
return "0";
} else {
StringBuilder res = new StringBuilder();
for (String str : strs) {
res.append(str);
}
return res.toString();
}
}
}
180. 连续出现的数字
select distinct l1.num as ConsecutiveNums from Logs l1,Logs l2 ,Logs l3
where l1.id = l2.id -1 and l2.id = l3.id -1 and
l1.num = l2.num and l2.num = l3.num