1. 程式人生 > >leetcode176---Second Highest Salary

leetcode176---Second Highest Salary

一、問題描述:
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

.

二、問題求解

(1)方法一

#所謂的“第二高”是指去重後的僅次於最高值的分數
#因此只需藉助於MAX關鍵詞即可
select MAX(Salary)
from Employee
where Salary < (select MAX(Salary) from Employee);

(2)方法二

#distinct,order by 和limit 組合使用
select IFNULL(
    (select  distinct Salary as SecondHighSalary 
     from Employee order by Salary desc 
     limit 1
, 1), NULL );

三、DISTINCT語句去重複記錄

在MySQL查詢中,可能會包含重複值。這並不成問題,不過,有時您也許希望僅僅列出不同(distinct)的值。

關鍵詞 DISTINCT 用於返回唯一不同的值,就是去重。用法:

SELECT DISTINCT * FROM tableName

DISTINCT 這個關鍵字來過濾掉多餘的重複記錄只保留一條。

相關推薦

leetcode176---Second Highest Salary

一、問題描述: Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | Salary | +----+-----

Leetcode176. Second Highest Salary(MYSQL limit,offset 區別)

原題 Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | Salary | +----+--------+

176. Second Highest Salary(Easy)

tco targe query rip con -s second count rom Source of the question Write a SQL query to get the second highest salary from the Employee t

176. Second Highest Salary

max color class statement select mysql ble clas cond Write a SQL query to get the second highest salary from the Employee table. +----+-

找第二大的數SQL-Second Highest Salary

des rom tin .com begin get pre sql 最大 1: 找小於最大的最大的 select max(Salary) from Employee where Salary<(select MAX(Salary) from Employe

sql leetcode 176. Second Highest Salary

Write a SQL query to get the second highest salary from the Employeetable. +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2

LeetCode-第二高的薪水(second-highest-salary)

第二高的薪水 難度 簡單 更多LeetCode答案歡迎大家關注Github: https://github.com/lxyer/LeetCodeAnswer 編寫一個 SQL 查詢,獲取 Employee 表中第二高的薪水(Salary) 。 +----+---

176. Second Highest Salary SQL查詢語句中的 limit offset

題目: 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 ab

leetcode 2--Second Highest Salary

https://leetcode.com/problems/second-highest-salary/description/ Write a SQL query to get the second highest salary from the Employee table. +—

176.Second Highest Salary

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

LeetCode Second Highest Salary

Problem Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | Salary | +----

[LeetCode] Second Highest Salary 第二高薪水

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

Second Highest Salary

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

Second Highest Salary(選擇第二高的工資)

要求: For example, given the above Employee table, the query should return 200 as the second highest

【LeetCode】Second Highest Salary && Nth Highest Salary

Total Accepted: 1030 Total Submissions: 4309 My Submissions Question Solution Write a SQL query to get the second highest salary from th

176. Second Highest Salary(Leetcode)

文章出處:http://www.cnblogs.com/grandyang/p/5348961.html Write a SQL query to get the second highest salary from the Employee table. +----+

LeetCode-Algorithms #002 Add Two Numbers, Database #176 Second Highest Salary

LeetCode-Algorithms #002 Add Two Numbers 給定兩個非空的以連結串列結構表示的非負整數, 這個結構長這樣: 1 public class ListNode { 2 int val; 3 ListNode next; 4 ListNode(int

leetcode 176. Second Highest Salary

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

【Leetcode】176. Second Highest Salary (Easy)

1.題目Write a SQL query to get the second highest salary from the Employee table.+----+--------+ | Id | Salary | +----+--------+ | 1 | 100

[理解leetcode解法]176. Second Highest Salary

176. Second Highest Salary #題目: Write a SQL query to get the second highest salary from theEmploy