题目来自:
https://leetcode-cn.com/problems/second-highest-salary/submissions/
AC代码如下:
SELECT
(SELECT DISTINCT Salary AS SecondHighestSalary
FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1) AS SecondHighestSalary;
- DISTINCT
会将查询后若干行完全相同的结果删除,只保留一行
若执行:
select distinct name,id from user;
得到的结果为name和id都不同时重复的记录
要是想得到只在name上不重复的记录可以执行:
select id,name from user group by name;
- ORDER BY | DESC
将查询结果按照列来排序,默认由小到大,加DESC则表示由大到小
如果要先排序某一列,再排序另外一列,只需把另外一列列名加进来就可:
SELECT * FROM stu ORDER BY Cno,Sno;
若要先Cno升序,Sno降序
SELECT * FROM stu ORDER BY Cno,Sno DESC;
- LIMIT | OFFSET
limit代表需要查询数据的个数
offset为查询数据时跳过数据的个数
可组合使用