心得:
找出該資料表內第二高薪水的筆數,若無則回傳null。
問題:
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 returnnull
.
答案:
- Sub Query
# Write your MySQL query statement below SELECT MAX(a.Salary) AS SecondHighestSalary FROM Employee AS a WHERE a.Salary < (SELECT MAX(z.Salary) FROM Employee AS z)
DISTINCT
> 不重複# Write your MySQL query statement below SELECT ( SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1, 1 ) AS SecondHighestSalary
GROUP BY
> 不重複# Write your MySQL query statement below SELECT ( SELECT Salary FROM Employee GROUP BY Salary ORDER BY Salary DESC LIMIT 1, 1 ) AS SecondHighestSalary