銆怢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 寰幆璇彞 rowWrite 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涓殑鐢ㄦ硶的主要内容,如果未能解决你的问题,请参考以下文章