--- title: 176. Second Highest Salary tags: Leetcode SQL,2021 --- # 【LeetCode】176. Second Highest Salary ## Description >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 query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null. +---------------------+ | SecondHighestSalary | +---------------------+ | 200 | +---------------------+ ## Code ``` # Write your MySQL query statement below SELECT (SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET 1) AS SecondHighestSalary ; ``` ### tags: LeetCode C# SQL >DISTINCT->過濾重複 >Order by條件是可以多個的 但假如以欄位名稱1排序後,有三個名稱重複一樣,oder by後可以再用,做區隔後繼續 欄位名稱2 加上 ASC or DESC排序,若排序後還是一樣無發比較高下的話可再第三個 欄位名稱3排序以此類推,但會越前面的排序為優先。 >OFFSET - FETCH 是 ORDER BY 子句的延伸功能。 使用 OFFSET 和 FETCH 限制傳回的資料列。 讓你可以過濾篩選特定範圍的資料列。 提供了對結果集的分頁處理功能。 可以指定跳過的行數,指定要取回的資料列筆數。 而且,OFFSET 和 FETCH 子句是依據 draft ANSI SQL:2011 標準。 因此,會比 TOP 子句具備更好的 SQL 語言相容性。 語法: ORDER BY {order_by_list} OFFSET {offset_value} ROW(S) FETCH FIRST|NEXT {fetch_value} ROW(S) ONLY >http://sharedderrick.blogspot.com/2012/06/t-sql-offset-fetch.html >