您的位置:首页 > 数据库 > MySQL

leetcode 176. Second Highest Salary

2016-08-08 14:35 316 查看


176. Second Highest Salary

 

Question
Editorial Solution
 My Submissions

Total Accepted: 21825
Total Submissions: 95303
Difficulty: Easy

Write a SQL query to get the second highest salary from the 
Employee
 table.
+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

For example, given the above Employee table, the second highest salary is 
200
.
If there is no second highest salary, then the query should return 
null
.

Subscribe to see which companies asked this question

Have you met this question in a real interview? 
# Write your MySQL query statement below
select (select distinct (Salary) from Employee order by Salary Desc limit 1,1) as SecondHighestSalary


注意 order by  后面的Desc表示降序排列,asc表示升序排列

as

as不是给表里的字段取别名,而是给查询的结果字段取别名。其目的是让查询的结果展现更符合人们观看习惯,在多张表查询的时候可以直接的区别多张表的同名的字段。
比如:
1、selec name as “姓名” ,sex as "性别" from user
2、select u.name as “姓名”,o.name as "英文名" ,u.sex as "性别" from user u ,other o where u.id = o.id;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oj leetcode mysql