銆怢eetCode鍒烽銆慡QL-Second Highest Salary 鍙婃墿灞曚互鍙奜racle涓殑鐢ㄦ硶

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆怢eetCode鍒烽銆慡QL-Second Highest Salary 鍙婃墿灞曚互鍙奜racle涓殑鐢ㄦ硶相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e9%9a%be%e7%82%b9' title='闅剧偣'>闅剧偣   鍙栨暟   des   鍒嗛〉   xxxxx   蹇呴』   strong   寰幆璇彞   row   

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                 |
+---------------------+

 

Subscribe to see which companies asked this question.

 

缈昏瘧锛?/span>

棰樼洰鐨勬剰鎬濇槸锛屽湪Employee琛ㄤ腑鎵惧埌绗簩澶х殑Salary瀛楁锛岀劧鍚庝互鍒悕鈥淪econdHighestSalary 鈥濈殑褰㈠紡杈撳嚭

 

瑙i鎬濊矾锛?/span>

鏈鏈変袱绉嶆€濊矾銆?/span>

涓€绉嶆槸鎵惧埌鎵惧埌鏈€澶у€硷紝鐒跺悗鎴戜滑鍙栨瘮鏈€澶у€煎皬涓€鐐圭殑鏈€澶у€笺€?/p>

SQL锛?/p>

SELECT Max(Salary) as SecondHighestSalary FROM Employee WHERE Salary< ( SELECT  Max(Salary) FROM Employee);

 

 Max()鍑芥暟琛ㄧず杩斿洖鏈€澶у€硷紝濡傛灉娌℃湁鍒欒繑鍥濶ULL銆?/p>

Salary<( SELECT Max(Salary) FROM Employee) 鍒欐槸瀛愭煡璇㈢殑姒傚康銆?/p>

渚嬪 Select * From Table_A Where key in (a,b);  濡傛灉鍏朵腑(a,b)鐨勬暟鎹潵婧愪簬Table_B鎴栬€匱able_A琛ㄣ€傛垜浠氨鍙互浣跨敤瀛愭煡璇紝灏?a,b)鐨勬暟鎹浛鎹负鎴戜滑鏌ヨ鍒扮殑鏁版嵁銆?/p>

Select * From Table_A Where key in (Select a,b From Table_B  Where Inr=鈥榅XXXXXX鈥?;

鈥渁s XXX鈥濆垯琛ㄧず灏嗚緭鍑虹殑鏁版嵁浠モ€淴XX鈥濅负鍒悕鐨勬柟寮忚緭鍑恒€?/p>

銆€

杩樻湁涓€绉嶆柟娉曞氨鏄紝鎴戜滑灏嗘暟鎹繘琛屾帓搴忥紝鐒跺悗鍙栫浜屼綅鏁般€?/p>

杩欑鏂规硶鐨勯毦鐐瑰湪浜庯細1銆佹暟鎹彲鑳戒細鏈夐噸澶嶉」銆?/p>

銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€2銆侀渶瑕佸厛鎺掑簭鍐嶅彇鏁帮紝鎵€浠ヤ笉鑳界敤between 鏂规硶鍥犱负between鏂规硶蹇呴』瑕佸湪order by 涔嬪墠銆?/p>

銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€3銆佹暣涓〃閮芥槸鍚屼竴涓暟鎹殑鏋佺鎯呭喌銆?/p>

銆€銆€瑕佽В鍐充互涓?涓毦鐐广€傛垜浠鍏堜娇鐢―ISTINCT鏂规硶灏嗘暟鎹殑閲嶅椤硅繃婊ゅ彧鍓╀竴鏉★紝鐒跺悗浣跨敤LIMIT鍒嗛〉鐨勬柟娉曞彇绗簩涓€硷紝瀵逛簬鎯呭喌3涓殑鏋佺鎯呭喌锛屾垜浠娇鐢?IFNULL鏉ヨ繘琛屽垽鏂紝濡傛灉娌″彇鍒板€煎氨杩斿洖NULL銆傞渶瑕佹敞鎰忕殑鏄紝LIMIT鏄疢ySQL涓殑鐢ㄦ硶锛孫racle鏄笉鏀寔LIMIT鐨勩€?/p>

SQL锛?/p>

SELECT IFNULL( (SELECT distinct Salary  FROM Employee order by Salary desc limit 1,1),null)as SecondHighestSalary;

鍏朵腑LIMIT 鐨勭敤娉曟槸杩欐牱鐨勶細

LIMIT m,n      "m"琛ㄧず鍋忕Щ閲忥紝琛ㄧず浠庣鍑犱綅寮€濮嬪彇銆?n"琛ㄧず闀垮害锛屽嵆闇€瑕佸彇澶氬皯浣嶃€傞渶瑕佹敞鎰忕殑鏄亸绉婚噺鐨勭涓€浣嶄负0锛屾墍浠?LIMIT 1,1"琛ㄧず浠庣浜屼綅寮€濮嬪彇闀垮害涓?鐨勬暟涔熷氨鏄浜屼綅鏁般€?LIMIT 0,n"鎴栬€?LIMIT n "鍒欒〃绀轰粠绗竴浣嶅紑濮嬪彇锛屼竴鐩村彇鍒扮n浣嶄负姝€?LIMIT m,-1"琛ㄧず浠庣m浣嶅紑濮嬪彇锛屼竴鐩村彇鍒拌〃鐨勬湯灏俱€?/p>

distinct 琛ㄧず灏嗘煡璇㈠埌鐨勭粨鏋滆繃婊ら噸澶嶉」锛屽彧淇濈暀涓€椤广€?/p>

IFNULL(Exp1,Exp2)锛岀被浼间竴涓笁鍏冭〃杈惧紡锛屽鏋淓xp1涓嶄负绌哄垯杩斿洖Exp1鐨勭粨鏋滈泦锛屽鏋滀负绌哄氨杩斿洖Exp2.

 

鏂规硶浜岀殑鍏抽敭鐐瑰湪浜庢帓搴忓拰鍙栨暟锛屾帓搴忔垜浠櫎浜嗕娇鐢∣RDER BY 鏂规硶涔嬪锛岃繕鍙互浣跨敤姣旇緝鐨勬柟寮忔帓搴忋€?/p>

SQL锛?/p>

SELECT DISTINCT(e2.Salary) FROM Employee e1,Employee e2 WHERE e2.Salary>=e1.Salary;

浣跨敤鈥?gt;=鈥濈殑鏂瑰紡锛屾槸涓轰簡閬垮厤Salary瀛楁鏈?鍊艰€屽鑷存棤娉曟瘮杈冦€?/p>

锛?span style="color: #888888">sql绌哄€艰〃绀猴細

鍦ㄥ厑璁哥┖鍊肩殑鎯呭喌涓嬶紝绌哄€兼槸NULL锛屾寚娌℃湁濉啓杩囨暟鎹紱
鍦ㄤ笉鍏佽涓虹┖鐨勬儏鍐典笅锛屾暟鍊煎瀷瀛楁榛樿涓?锛?/p>

闇€瑕佹敞鎰忕殑鏄紝杩欑鎺掑簭鏂瑰紡闈炲父闈炲父鐨勬秷鑰楃郴缁熻祫婧愶紝鍦ㄥ疄闄呯敓浜т腑浣跨敤杩欑鏂瑰紡鏉ユ帓搴忔槸涓嶈礋璐d换鐨勮涓猴紙瑙?a title="sql澶氳〃鏌ヨ楠岃瘉" href="https://www.cnblogs.com/MarsDing/p/9856272.html" target="_blank">https://www.cnblogs.com/MarsDing/p/9856272.html锛夛紝鎵€浠ュ闈炲繀瑕佸敖閲忎笉瑕佷娇鐢ㄨ繖绉嶆柟寮忚€屼娇鐢ㄦ洿蹇嵎娑堣€楁洿灏忕殑鐨凮rder by銆?/p>

鐜板湪宸茬粡鎺掑ソ搴忎簡锛岄偅涔堟帴涓嬫潵鎴戜滑瑕佽繘琛屽彇鏁般€傚彇鏁板彲浠ヤ娇鐢ㄤ笂闈㈢殑LIMIT鏂规硶銆?/p>

LIMIT鏂规硶锛?/p>

SELECT IFNULL((SELECT DISTINCT(e2.Salary) FROM Employee e1,Employee e2 WHERE e2.Salary>e1.Salary LIMIT 1,1),NULL) as SecondHighestSalary;

鍗筹紝灏嗭細order by Salary desc 鏇挎崲涓?nbsp;WHERE e2.Salary>e1.Salary 

闄ゆ涔嬪杩樻湁鍏朵粬鏂规硶鍚楋紵鎴戜滑鐭ラ亾锛孲ELECT璇彞瀹為檯涓婃槸涓惊鐜鍙ワ紝鏃㈢劧鏄釜寰幆璇彞鑰屼笖宸茬粡鎺掑ソ搴忥紝閭d箞涔熷氨鏄濡傛灉鎴戜滑瑕佸彇绗簩澶х殑鏁版嵁锛屾垜浠彲浠ュ彇褰撲粬寰幆鍒扮浜屾鏃剁殑閭d竴琛屻€傛€庝箞鍙栧埌閭d竴琛屽憿銆傚杩嘑or寰幆閮界煡閬擄紝瑕佸厛瀵硅繖涓惊鐜繘琛岃鏁帮紝鐒跺悗寰幆鍒版垜浠渶瑕佺殑閭d竴娆″啀鍙栧€笺€傞偅涔圫QL涓€庝箞璁℃暟鍛€傜瓟妗堟槸浣跨敤COUNT鏂规硶銆?/p>

COUNT鏂规硶

SELECT銆€e1.Salary FROM Employee e1 WHERE 2=(Select Count(distinct(e2.Salary)) from Employee e2 where e2.Salary >= e1.Salary)  as SecondHighestSalary;

涓婇潰涓ょ鏂规硶涔熷彲浠ョ敤浜庯紝鍙栦换鎰忕N浣嶆暟鎹椂銆?/p>

COUNT鏂规硶涔嬫墍浠ヤ笉鑳戒娇鐢∣rder by锛屾槸鍥犱负Order by鏄缁撴灉杩涜鎺掑簭锛岃€屾垜浠渶瑕佺殑鏄凡鎺掑簭濂界殑缁撴灉銆?/p>

 濡傛灉瑕佷娇鐢∣rder by 鏂规硶锛岄渶瑕佸啀鍔犱竴灞傚瓙鏌ヨ銆?/p>

COUNT-Order By 鏂规硶

SELECT銆€Salary FROM Employee  WHERE 2=(Select Count(distinct(Salary)) from (Select Salary From Employee order by Salary desc))  as SecondHighestSalary;

.

鐢变簬Oracle涓笉鑳戒娇鐢↙IMIT锛屾墍浠ユ垜浠敤rownum鏉ュ鐞嗘闂

Select Salary from (select amt ,rownum n From (Select distinct amt from Employee order by Salary desc)) where n=2;

鍏朵腑鐨刵=2鍙互鏇挎崲涓轰换鎰忔鏁存暟锛屽嵆涓虹N浣嶃€?/p>

鎬荤粨锛氬涓€涓瑕侀€氳繃涓嶅悓瑙掑害鎬濊€冨苟浼樺寲瑙g瓟

杞浇浜庯細https://www.cnblogs.com/contixue/p/7057025.html

以上是关于銆怢eetCode鍒烽銆慡QL-Second Highest Salary 鍙婃墿灞曚互鍙奜racle涓殑鐢ㄦ硶的主要内容,如果未能解决你的问题,请参考以下文章

銆怢eetCode銆戝姩鎬佽鍒掞紙涓嬬瘒鍏?9棰橈級

鍖楀ぇ绠楁硶澶х鐨凩eetCode鍒烽妯℃澘.pdf鏉ヤ簡锛?/h1>

銆岄潰璇曞繀闂€峫eetcode楂橀棰樼簿閫?/a>

銆怉OP tips銆慡tand on Giants' Shoulders

leetcode鍒烽19

銆怢inux銆慻it銆乺epo銆乻vn銆乤db浠ュ強鍏朵粬甯哥敤鍛戒护