{"id":17427,"date":"2024-12-18T05:29:35","date_gmt":"2024-12-18T05:29:35","guid":{"rendered":"https:\/\/www.pickl.ai\/blog\/?p=17427"},"modified":"2025-07-18T13:33:58","modified_gmt":"2025-07-18T08:03:58","slug":"find-second-highest-salary-in-sql","status":"publish","type":"post","link":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/","title":{"rendered":"How Do You Find the Second Highest Salary in SQL?"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: Master SQL techniques like subqueries, DISTINCT, and ranking functions to find the second-highest salary. Handle duplicates, nulls, and empty tables effectively. Optimise performance with proper indexing and SQL dialect compatibility to build robust, efficient queries for diverse datasets.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Introduction\" >Introduction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Understanding_the_Problem\" >Understanding the Problem<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Common_Pitfalls\" >Common Pitfalls<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Approaches_to_Solve_the_Problem\" >Approaches to Solve the Problem<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Using_Subquery\" >Using Subquery<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Using_DISTINCT_and_LIMIT\" >Using DISTINCT and LIMIT<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Using_ROW_NUMBER_Window_Function\" >Using ROW_NUMBER() (Window Function)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Using_RANK_and_DENSE_RANK\" >Using RANK() and DENSE_RANK()<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Comparison_of_Approaches\" >Comparison of Approaches<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Performance_Considerations\" >Performance Considerations<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Subquery\" >Subquery<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#DISTINCT_and_LIMIT\" >DISTINCT and LIMIT<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#ROW_NUMBER\" >ROW_NUMBER()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#RANK_and_DENSE_RANK\" >RANK() and DENSE_RANK()<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Advantages_and_Limitations\" >Advantages and Limitations<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Edge_Cases_and_Best_Practices\" >Edge Cases and Best Practices<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Handling_Empty_Tables\" >Handling Empty Tables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Managing_Null_Salaries\" >Managing Null Salaries<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Ensuring_Compatibility_with_Different_SQL_Dialects\" >Ensuring Compatibility with Different SQL Dialects<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Wrapping_Up\" >Wrapping Up<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#What_is_the_Best_Query_to_find_the_Second-Highest_Salary_in_SQL\" >What is the Best Query to find the Second-Highest Salary in SQL?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#How_do_you_Handle_Duplicate_Salaries_when_finding_the_Second-Highest_Salary_in_SQL\" >How do you Handle Duplicate Salaries when finding the Second-Highest Salary in SQL?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#How_do_you_Optimise_Performance_when_finding_the_Second-Highest_Salary_in_SQL\" >How do you Optimise Performance when finding the Second-Highest Salary in SQL?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 id=\"introduction\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span><strong>Introduction<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Identifying the second-highest salary in <a href=\"https:\/\/pickl.ai\/blog\/introduction-to-sql-for-data-science\/\">SQL<\/a> is a common yet essential query in <a href=\"https:\/\/pickl.ai\/blog\/dbms-architecture\/\">database management<\/a>. It plays a significant role in real-world scenarios, such as analysing employee compensation or filtering rankings.&nbsp;<\/p>\n\n\n\n<p>Unlike retrieving the highest salary, finding the second highest involves handling duplicates, null values, or missing records, which adds complexity to the solution. This blog aims to provide a clear understanding of various methods to solve this challenge, including subqueries, window functions, and ranking techniques.&nbsp;<\/p>\n\n\n\n<p>By the end, you\u2019ll learn efficient strategies to write robust SQL queries and handle edge cases effectively.<\/p>\n\n\n\n<p><strong>Key Takeaways<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use subqueries, DISTINCT with LIMIT, or window functions like ROW_NUMBER() and RANK().<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pickl.ai\/blog\/unlocking-the-power-of-rank-function\/\">Ranking functions<\/a> like DENSE_RANK() ensure accurate results with duplicate salaries.<\/li>\n\n\n\n<li>Always exclude null values in the Salary column to prevent errors.<\/li>\n\n\n\n<li>Index the Salary column and avoid unnecessary table scans.<\/li>\n\n\n\n<li>Adapt queries to database-specific syntax for compatibility and efficiency.<\/li>\n<\/ul>\n\n\n\n<h2 id=\"understanding-the-problem\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Understanding_the_Problem\"><\/span><strong>Understanding the Problem<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You typically work with a database table containing employee salary information to find the second-highest salary. A common structure for this table is as follows:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>EmployeeID<\/strong><strong><\/strong><\/td><td><strong>Name<\/strong><\/td><td><strong>Salary<\/strong><\/td><\/tr><tr><td>1<\/td><td>Alice<\/td><td>50000<\/td><\/tr><tr><td>2<\/td><td>Bob<\/td><td>70000<\/td><\/tr><tr><td>3<\/td><td>Charlie<\/td><td>70000<\/td><\/tr><tr><td>4<\/td><td>Diana<\/td><td>60000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The Salary column is critical for this query, as it stores the values you analyse. This column often contains numeric data, and its contents can vary\u2014some salaries may repeat, some may be null, or the table may have no rows. These nuances demand careful handling to ensure the query works correctly in all scenarios.<\/p>\n\n\n\n<h3 id=\"common-pitfalls\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Common_Pitfalls\"><\/span><strong>Common Pitfalls<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>When writing SQL queries for the second-highest salary, developers often encounter several challenges:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Handling Duplicate Salaries: <\/strong>If two or more employees earn the same salary, the second-highest value might appear multiple times. For example, in the table above, the second-highest salary is $60,000, but duplicate $70,000 entries can confuse queries if not addressed properly. Using DISTINCT or ranking functions like RANK() or DENSE_RANK() can help avoid this problem.<\/li>\n\n\n\n<li><strong>Null Values in the Salary Column:<\/strong> Some databases store null values for employees with no defined salary. These nulls can interfere with calculations if not explicitly filtered out. Always include a condition like WHERE Salary IS NOT NULL to eliminate such anomalies.<\/li>\n\n\n\n<li><strong>Empty Tables: <\/strong>If the table has no data, your query must execute gracefully without errors. Adding checks or default values ensures the query doesn\u2019t fail in such cases.<\/li>\n<\/ul>\n\n\n\n<p>Understanding these pitfalls prepares you to write robust and efficient queries.<\/p>\n\n\n\n<h2 id=\"approaches-to-solve-the-problem\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Approaches_to_Solve_the_Problem\"><\/span><strong>Approaches to Solve the Problem<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Finding the second-highest salary in a dataset can be achieved using multiple <a href=\"https:\/\/medium.com\/@techsuneel99\/20-advanced-sql-techniques-with-practical-examples-b47490d9896d\" rel=\"nofollow\">SQL techniques<\/a>, each suited for different scenarios and complexities. From basic subqueries to advanced window functions, these approaches allow you to address the problem effectively. Below, we explore each method with a step-by-step explanation and examples.<\/p>\n\n\n\n<h3 id=\"using-subquery\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_Subquery\"><\/span><strong>Using Subquery<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Subqueries are one of the simplest ways to find the second-highest salary. In this method, you use a nested query to identify the maximum salary below the highest.<\/p>\n\n\n\n<p><strong>Explanation of Subquery Logic:<\/strong><strong><br><\/strong>The logic involves finding the maximum salary first and then excluding it to identify the second-highest. The outer query retrieves the maximum salary from the filtered result of the subquery.<\/p>\n\n\n\n<p><strong>Example Query:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXebIpFzLXEgxYCjwTgPJGr0TKDz1F9WTIJ70ZsN_STIWzqwGaRVZDuOwkWYv7sR7kSbwtd3iG2BuUkxzCB8HECrRu3e7hd8T1iT6njDO-1ctsgEX0lv2wTcIWxH3fhTOZSe8JRjbw?key=PxuGL-xnSadhOBjR_31k-kuv\" alt=\"Query to find the second-highest salary using subquery.\"\/><\/figure>\n\n\n\n<p>Here\u2019s how it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The inner query (SELECT MAX(Salary) FROM Employee) identifies the highest salary.<\/li>\n\n\n\n<li>The outer query filters salaries that are less than the highest and calculates the maximum among them.<\/li>\n<\/ul>\n\n\n\n<p>This straightforward approach may not handle edge cases like duplicate salaries efficiently.<\/p>\n\n\n\n<h3 id=\"using-distinct-and-limit\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_DISTINCT_and_LIMIT\"><\/span><strong>Using DISTINCT and LIMIT<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This approach involves sorting the distinct salaries in descending order and selecting the second row using LIMIT.<\/p>\n\n\n\n<p><strong>Step-by-Step Breakdown:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Eliminate duplicate salaries using DISTINCT.<\/li>\n\n\n\n<li>Sort the distinct salaries in descending order.<\/li>\n\n\n\n<li>Use LIMIT to fetch the second-highest value.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example Query:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXd1y0jY3M5YEdenHdRUvuj5SAHyw2mYxZC43Lh_YxUE2ou1sLodPFdhs6scn6tiKqVoDS8dW_ookSQksMHsCCLsdhVw3e887d1rgcXh8yztIS6fLt0mCvqkImHFvB4kLXLA_xVQYg?key=PxuGL-xnSadhOBjR_31k-kuv\" alt=\"Query to find the second-highest salary using DISTINCT and LIMIT.\"\/><\/figure>\n\n\n\n<p>Explanation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>DISTINCT ensures unique salary values are considered.<\/li>\n\n\n\n<li>ORDER BY Salary DESC arranges salaries from highest to lowest.<\/li>\n\n\n\n<li>LIMIT 1 OFFSET 1 skips the highest salary and fetches the second.<\/li>\n<\/ul>\n\n\n\n<p>This compact method works well for smaller datasets but may struggle with performance on larger tables.<\/p>\n\n\n\n<h3 id=\"using-row_number-window-function\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_ROW_NUMBER_Window_Function\"><\/span><strong>Using ROW_NUMBER() (Window Function)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Window functions like ROW_NUMBER() provide a powerful way to rank rows based on a specific column, enabling you to identify the second-highest salary directly.<\/p>\n\n\n\n<p><strong>Introduction to Window Functions:<\/strong><strong><br><\/strong>Window functions operate on a defined &#8220;window&#8221; of data, allowing you to calculate row numbers, ranks, or aggregates within that window.<\/p>\n\n\n\n<p><strong>Example Query:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXdYIPeIJW1gXgvrYjW6BL9oLypu6E_AQIKYqk289gcEM0k0zQkOxV-HDpOaufmAKFHNh9aKuOhUdZ8HT8GqvKklgMMaK7GCnFrVU8mk7SRvJ2Cpts-SmqPK-464cZMEEoKxpp8n?key=PxuGL-xnSadhOBjR_31k-kuv\" alt=\"Query to find the second-highest salary using ROW_NUMBER().\"\/><\/figure>\n\n\n\n<p>Explanation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The ROW_NUMBER() function assigns a unique rank to each salary based on descending order.<\/li>\n\n\n\n<li>The WITH clause creates a temporary table (RankedSalaries) with ranked rows.<\/li>\n\n\n\n<li>The outer query fetches the salary where the rank is 2.<\/li>\n<\/ul>\n\n\n\n<p>This method handles duplicates effectively and provides flexibility for further analysis.<\/p>\n\n\n\n<h3 id=\"using-rank-and-dense_rank\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_RANK_and_DENSE_RANK\"><\/span><strong>Using RANK() and DENSE_RANK()<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Ranking functions like RANK() and DENSE_RANK() are ideal for handling duplicate salaries while identifying the second-highest value.<\/p>\n\n\n\n<p><strong>Explanation of Rank-Based Methods:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>RANK(): Assigns ranks, but skips ranks if duplicates exist.<\/li>\n\n\n\n<li>DENSE_RANK(): Assigns consecutive ranks, even if duplicates exist.<\/li>\n<\/ul>\n\n\n\n<p><strong>Differences Between RANK() and DENSE_RANK():<\/strong><strong><br><\/strong>If two employees share the highest salary, RANK() will skip the next rank (e.g., 1, 1, 3), whereas DENSE_RANK() assigns consecutive ranks (e.g., 1, 1, 2).<\/p>\n\n\n\n<p><strong>Example Query:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXc-2qxL0eDiWwpdyVy2_mHIims6RHW2Z3mg8DGBZpkNo5PVrMBGSeJffZUjy6jYce-DYwte3TxI-lrzuO8L9IXwQz4DkOzjvXp3PjyIGsW-PWDugLqtZ9o0TzAOBC4rqc6aHfHR?key=PxuGL-xnSadhOBjR_31k-kuv\" alt=\"Query to find the second-highest salary using RANK().\"\/><\/figure>\n\n\n\n<p>Explanation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The RANK() function assigns a rank to each salary based on descending order.<\/li>\n\n\n\n<li>The query filters rows where the rank is 2, ensuring the correct result.<\/li>\n<\/ul>\n\n\n\n<p>This approach is robust for datasets with duplicate salaries, making it a preferred choice for complex scenarios.<\/p>\n\n\n\n<p>Each of these methods has its strengths. Choose the one that best fits your dataset size, structure, and business requirements.<\/p>\n\n\n\n<h2 id=\"comparison-of-approaches\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Comparison_of_Approaches\"><\/span><strong>Comparison of Approaches<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Choosing the best approach to find the second-highest salary depends on the dataset size, structure, and specific requirements. Each method has unique characteristics that affect performance and ease of use. Below, we compare these approaches based on performance considerations, advantages, and limitations.<\/p>\n\n\n\n<h3 id=\"performance-considerations\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Performance_Considerations\"><\/span><strong>Performance Considerations<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The performance of SQL queries depends heavily on dataset size and database optimisation. Some methods work well for small tables, while others excel with larger datasets or frequent query execution. Understanding how each approach interacts with indexing, sorting, and scanning helps determine the best fit for your scenario.<\/p>\n\n\n\n<h4 id=\"subquery\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Subquery\"><\/span><strong>Subquery<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>This approach is straightforward, but it can perform poorly on large datasets due to the nested query. The database needs to scan the table multiple times, which increases execution time.<\/p>\n\n\n\n<h4 id=\"distinct-and-limit\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"DISTINCT_and_LIMIT\"><\/span><strong>DISTINCT and LIMIT<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>This method is efficient for smaller datasets because it sorts and skips unnecessary rows with OFFSET. However, sorting distinct values on large datasets can become resource-intensive.<\/p>\n\n\n\n<h4 id=\"row_number\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"ROW_NUMBER\"><\/span><strong>ROW_NUMBER()<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Window functions like ROW_NUMBER() are optimised in modern databases. While they work well on large datasets, performance may degrade if the table has many rows without proper indexing.<\/p>\n\n\n\n<h4 id=\"rank-and-dense_rank\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"RANK_and_DENSE_RANK\"><\/span><strong>RANK() and DENSE_RANK()<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>These ranking functions are ideal for handling duplicates, but like ROW_NUMBER(), they can become slow on large datasets if indexes are missing or queries are overly complex.<\/p>\n\n\n\n<h3 id=\"advantages-and-limitations\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Advantages_and_Limitations\"><\/span><strong>Advantages and Limitations<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Each approach has distinct advantages that make it suitable for specific use cases. However, no method is universally perfect, and limitations such as complexity or inefficiency in certain conditions can impact your choice. Let\u2019s explore the strengths and drawbacks of subqueries, DISTINCT, window functions, and ranking methods in detail.<\/p>\n\n\n\n<p><strong>Subquery<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Advantages<\/strong>: Easy to write and understand.<\/li>\n\n\n\n<li><strong>Limitations<\/strong>: Inefficient for large datasets and lacks flexibility for advanced use cases.<\/li>\n<\/ul>\n\n\n\n<p><strong>DISTINCT and LIMIT<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Advantages:<\/strong> Compact query with straightforward logic.<\/li>\n\n\n\n<li><strong>Limitations:<\/strong> Performance drops with large datasets due to sorting and distinct value computation.<\/li>\n<\/ul>\n\n\n\n<p><strong>ROW_NUMBER()<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Advantages<\/strong>: Handles ranking robustly and allows for further analysis beyond the second-highest salary.<\/li>\n\n\n\n<li><strong>Limitations:<\/strong> Requires familiarity with window functions; indexing is crucial for good performance.<\/li>\n<\/ul>\n\n\n\n<p><strong>RANK() and DENSE_RANK()<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Advantages: <\/strong>Handles duplicate salaries effectively; more precise when ranking is needed.<\/li>\n\n\n\n<li><strong>Limitations:<\/strong> Slightly more complex to implement; performance relies heavily on indexing.<\/li>\n<\/ul>\n\n\n\n<p>Each method has trade-offs. For large datasets, ranking functions or window methods are better. For smaller datasets, subqueries or DISTINCT approaches may suffice.<\/p>\n\n\n\n<h2 id=\"edge-cases-and-best-practices\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Edge_Cases_and_Best_Practices\"><\/span><strong>Edge Cases and Best Practices<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>When working with SQL queries to find the second-highest salary, handling edge cases is critical to ensure accuracy and robustness.&nbsp;<\/p>\n\n\n\n<p>You can build queries that work seamlessly across various scenarios by addressing potential issues such as empty tables, null salaries, and SQL dialect compatibility. Below, we explore these challenges and share best practices to handle them effectively.<\/p>\n\n\n\n<h3 id=\"handling-empty-tables\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Handling_Empty_Tables\"><\/span><strong>Handling Empty Tables<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>One typical edge case is querying a table with no data. If the table is empty, queries like MAX() or ranking functions may return errors or unexpected results. To handle this, you should incorporate checks that gracefully return a default value, such as NULL or 0, when no data exists.<\/p>\n\n\n\n<p><strong>Best Practice:<\/strong> Use conditional logic to handle empty tables. For instance:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXd_4qKh5gxNxYBlECTDIJKzO-3yzyFrW85Hc9om8-ppdYOu42SUNKAEOnr4QxE_lVoZRHp8n-rVmViL96TsBLD4NzYs2GzM1BhgNCrffCXbHT00BIIaDTlcainNXJkEGZOlU45z?key=PxuGL-xnSadhOBjR_31k-kuv\" alt=\"Query to handle empty tables for the second-highest salary.\"\/><\/figure>\n\n\n\n<p>If the table has no rows, this query will naturally return NULL. To refine the user experience further, you can wrap the query with logic to return a meaningful message or default value in the application layer.<\/p>\n\n\n\n<h3 id=\"managing-null-salaries\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Managing_Null_Salaries\"><\/span><strong>Managing Null Salaries<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The Salary column in many databases may contain NULL values, representing employees without a defined salary. These nulls can disrupt calculations and rankings, so it\u2019s important to exclude them explicitly.<\/p>\n\n\n\n<p><strong>Best Practice:<\/strong> Filter out null values before processing. Add a WHERE clause to ignore nulls:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXc-GonKRx-kjXIKHvYxhbjJuVu1V4JB2W84lHemD76OM1dXvAKf6GUP212_bgPkfkrkkCc9GpcYWJ4b2cBiSctV3h_vQKfrTUOVODsyNObgH4rN75OaFnf6AlM-os2Kv-sxlFWqPg?key=PxuGL-xnSadhOBjR_31k-kuv\" alt=\"Query to exclude null salaries for accurate results.\"\/><\/figure>\n\n\n\n<p>This ensures only valid salaries are considered, preventing errors or inaccurate rankings.<\/p>\n\n\n\n<h3 id=\"ensuring-compatibility-with-different-sql-dialects\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Ensuring_Compatibility_with_Different_SQL_Dialects\"><\/span><strong>Ensuring Compatibility with Different SQL Dialects<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>SQL syntax and functions can vary between databases like MySQL, PostgreSQL,<a href=\"https:\/\/www.pickl.ai\/blog\/how-to-drop-a-database-in-sql-server\/\"> SQL Server<\/a>, and Oracle. For example, some dialects don\u2019t support window functions like ROW_NUMBER() or use different methods to limit rows (LIMIT in MySQL vs. FETCH FIRST in SQL Server).<\/p>\n\n\n\n<p><strong>Best Practice:<\/strong> Write queries that align with the SQL dialect of your database. Alternatively, adopt a universally supported approach, like subqueries, when working with multiple systems.<\/p>\n\n\n\n<p>Example of a SQL Server-compatible query:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXcwstjdT7W6_kxe6rTv6erPQVtCGvZnGQK2yPSIWKl46XjLViV2b_lvpcGSdWy36vkjfCu-iL9I5_2R4N4pZsh5xNsDZlin1l9jiilYaQ7qGp6y_ehI_pGS80srT03V9IsEukw5?key=PxuGL-xnSadhOBjR_31k-kuv\" alt=\"SQL Server-compatible query using ROW_NUMBER().\"\/><\/figure>\n\n\n\n<p>For databases that lack window functions, use alternative logic, like nested queries, to achieve the same results.<\/p>\n\n\n\n<p>You can create reliable, efficient SQL queries across various datasets and environments by addressing these edge cases and following best practices. This ensures your solution remains robust, adaptable, and easy to maintain.<\/p>\n\n\n\n<h2 id=\"wrapping-up\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Wrapping_Up\"><\/span><strong>Wrapping Up<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Finding the second-highest salary in SQL is an essential skill for database management. By mastering various techniques\u2014subqueries, DISTINCT, LIMIT, ROW_NUMBER(), RANK(), and DENSE_RANK()\u2014you can efficiently handle edge cases like duplicates, nulls, and empty tables. Each approach offers unique advantages, making choosing one suited to your dataset and requirements vital.&nbsp;<\/p>\n\n\n\n<p>Proper indexing and awareness of SQL dialect differences further optimise performance. Whether working on employee compensation analysis or ranking systems, implementing these strategies ensures robust and scalable queries. With best practices, you\u2019ll tackle this challenge seamlessly, enhancing your SQL querying and problem-solving expertise.<\/p>\n\n\n\n<h2 id=\"frequently-asked-questions\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span><strong>Frequently Asked Questions<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 id=\"what-is-the-best-query-to-find-the-second-highest-salary-in-sql\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_the_Best_Query_to_find_the_Second-Highest_Salary_in_SQL\"><\/span><strong>What is the Best Query to find the Second-Highest Salary in SQL?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The best query depends on your dataset. Use ROW_NUMBER() or RANK() for large datasets with duplicates. For smaller tables, DISTINCT with LIMIT or a subquery works well. Proper indexing enhances performance regardless of the approach.<\/p>\n\n\n\n<h3 id=\"how-do-you-handle-duplicate-salaries-when-finding-the-second-highest-salary-in-sql\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_do_you_Handle_Duplicate_Salaries_when_finding_the_Second-Highest_Salary_in_SQL\"><\/span><strong>How do you Handle Duplicate Salaries when finding the Second-Highest Salary in SQL?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Use RANK() or DENSE_RANK() functions. These rank salaries based on their order, handling duplicates effectively. RANK() skips rank for ties, while DENSE_RANK() assigns consecutive ranks, ensuring accurate results even with repeated salaries.<\/p>\n\n\n\n<h3 id=\"how-do-you-optimise-performance-when-finding-the-second-highest-salary-in-sql\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_do_you_Optimise_Performance_when_finding_the_Second-Highest_Salary_in_SQL\"><\/span><strong>How do you Optimise Performance when finding the Second-Highest Salary in SQL?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Index the Salary column to speed up sorting and ranking. For efficiency, modern databases use window functions like ROW_NUMBER(). Avoid scanning entire tables by filtering null values and excluding unnecessary columns in the query.<\/p>\n","protected":false},"excerpt":{"rendered":"Learn how to find the second-highest salary in SQL using subqueries, DISTINCT, and ranking functions.\n","protected":false},"author":4,"featured_media":17432,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[613],"tags":[3606],"ppma_author":[2169,2184],"class_list":{"0":"post-17427","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-sql","8":"tag-second-highest-salary-in-sql"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Find the Second Highest Salary in SQL<\/title>\n<meta name=\"description\" content=\"how to find the second-highest salary in SQL using subqueries, DISTINCT, and ranking functions. Explore best practices and handle duplicates.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How Do You Find the Second Highest Salary in SQL?\" \/>\n<meta property=\"og:description\" content=\"how to find the second-highest salary in SQL using subqueries, DISTINCT, and ranking functions. Explore best practices and handle duplicates.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/\" \/>\n<meta property=\"og:site_name\" content=\"Pickl.AI\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-18T05:29:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-18T08:03:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pickl.ai\/blog\/wp-content\/uploads\/2024\/12\/image8.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Neha Singh, Anubhav Jain\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Neha Singh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/find-second-highest-salary-in-sql\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/find-second-highest-salary-in-sql\\\/\"},\"author\":{\"name\":\"Neha Singh\",\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/#\\\/schema\\\/person\\\/2ad633a6bc1b93bc13591b60895be308\"},\"headline\":\"How Do You Find the Second Highest Salary in SQL?\",\"datePublished\":\"2024-12-18T05:29:35+00:00\",\"dateModified\":\"2025-07-18T08:03:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/find-second-highest-salary-in-sql\\\/\"},\"wordCount\":1926,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/find-second-highest-salary-in-sql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/image8.png\",\"keywords\":[\"Second Highest Salary in SQL\"],\"articleSection\":[\"SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/find-second-highest-salary-in-sql\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/find-second-highest-salary-in-sql\\\/\",\"url\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/find-second-highest-salary-in-sql\\\/\",\"name\":\"Find the Second Highest Salary in SQL\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/find-second-highest-salary-in-sql\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/find-second-highest-salary-in-sql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/image8.png\",\"datePublished\":\"2024-12-18T05:29:35+00:00\",\"dateModified\":\"2025-07-18T08:03:58+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/#\\\/schema\\\/person\\\/2ad633a6bc1b93bc13591b60895be308\"},\"description\":\"how to find the second-highest salary in SQL using subqueries, DISTINCT, and ranking functions. Explore best practices and handle duplicates.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/find-second-highest-salary-in-sql\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/find-second-highest-salary-in-sql\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/find-second-highest-salary-in-sql\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/image8.png\",\"contentUrl\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/image8.png\",\"width\":1200,\"height\":628,\"caption\":\"How Do You Find the Second Highest Salary in SQL?\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/find-second-highest-salary-in-sql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL\",\"item\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/category\\\/sql\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How Do You Find the Second Highest Salary in SQL?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/\",\"name\":\"Pickl.AI\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/#\\\/schema\\\/person\\\/2ad633a6bc1b93bc13591b60895be308\",\"name\":\"Neha Singh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pickl.ai\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/06\\\/avatar_user_4_1717572961-96x96.jpg3d1a0d35d7a1a929f4a120e9053cbdb5\",\"url\":\"https:\\\/\\\/pickl.ai\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/06\\\/avatar_user_4_1717572961-96x96.jpg\",\"contentUrl\":\"https:\\\/\\\/pickl.ai\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/06\\\/avatar_user_4_1717572961-96x96.jpg\",\"caption\":\"Neha Singh\"},\"description\":\"I\u2019m a full-time freelance writer and editor who enjoys wordsmithing. The 8 years long journey as a content writer and editor has made me relaize the significance and power of choosing the right words. Prior to my writing journey, I was a trainer and human resource manager. WIth more than a decade long professional journey, I find myself more powerful as a wordsmith. As an avid writer, everything around me inspires me and pushes me to string words and ideas to create unique content; and when I\u2019m not writing and editing, I enjoy experimenting with my culinary skills, reading, gardening, and spending time with my adorable little mutt Neel.\",\"url\":\"https:\\\/\\\/www.pickl.ai\\\/blog\\\/author\\\/nehasingh\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Find the Second Highest Salary in SQL","description":"how to find the second-highest salary in SQL using subqueries, DISTINCT, and ranking functions. Explore best practices and handle duplicates.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/","og_locale":"en_US","og_type":"article","og_title":"How Do You Find the Second Highest Salary in SQL?","og_description":"how to find the second-highest salary in SQL using subqueries, DISTINCT, and ranking functions. Explore best practices and handle duplicates.","og_url":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/","og_site_name":"Pickl.AI","article_published_time":"2024-12-18T05:29:35+00:00","article_modified_time":"2025-07-18T08:03:58+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.pickl.ai\/blog\/wp-content\/uploads\/2024\/12\/image8.png","type":"image\/png"}],"author":"Neha Singh, Anubhav Jain","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Neha Singh","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#article","isPartOf":{"@id":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/"},"author":{"name":"Neha Singh","@id":"https:\/\/www.pickl.ai\/blog\/#\/schema\/person\/2ad633a6bc1b93bc13591b60895be308"},"headline":"How Do You Find the Second Highest Salary in SQL?","datePublished":"2024-12-18T05:29:35+00:00","dateModified":"2025-07-18T08:03:58+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/"},"wordCount":1926,"commentCount":0,"image":{"@id":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pickl.ai\/blog\/wp-content\/uploads\/2024\/12\/image8.png","keywords":["Second Highest Salary in SQL"],"articleSection":["SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/","url":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/","name":"Find the Second Highest Salary in SQL","isPartOf":{"@id":"https:\/\/www.pickl.ai\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#primaryimage"},"image":{"@id":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pickl.ai\/blog\/wp-content\/uploads\/2024\/12\/image8.png","datePublished":"2024-12-18T05:29:35+00:00","dateModified":"2025-07-18T08:03:58+00:00","author":{"@id":"https:\/\/www.pickl.ai\/blog\/#\/schema\/person\/2ad633a6bc1b93bc13591b60895be308"},"description":"how to find the second-highest salary in SQL using subqueries, DISTINCT, and ranking functions. Explore best practices and handle duplicates.","breadcrumb":{"@id":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#primaryimage","url":"https:\/\/www.pickl.ai\/blog\/wp-content\/uploads\/2024\/12\/image8.png","contentUrl":"https:\/\/www.pickl.ai\/blog\/wp-content\/uploads\/2024\/12\/image8.png","width":1200,"height":628,"caption":"How Do You Find the Second Highest Salary in SQL?"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pickl.ai\/blog\/find-second-highest-salary-in-sql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pickl.ai\/blog\/"},{"@type":"ListItem","position":2,"name":"SQL","item":"https:\/\/www.pickl.ai\/blog\/category\/sql\/"},{"@type":"ListItem","position":3,"name":"How Do You Find the Second Highest Salary in SQL?"}]},{"@type":"WebSite","@id":"https:\/\/www.pickl.ai\/blog\/#website","url":"https:\/\/www.pickl.ai\/blog\/","name":"Pickl.AI","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pickl.ai\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.pickl.ai\/blog\/#\/schema\/person\/2ad633a6bc1b93bc13591b60895be308","name":"Neha Singh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pickl.ai\/blog\/wp-content\/uploads\/2024\/06\/avatar_user_4_1717572961-96x96.jpg3d1a0d35d7a1a929f4a120e9053cbdb5","url":"https:\/\/pickl.ai\/blog\/wp-content\/uploads\/2024\/06\/avatar_user_4_1717572961-96x96.jpg","contentUrl":"https:\/\/pickl.ai\/blog\/wp-content\/uploads\/2024\/06\/avatar_user_4_1717572961-96x96.jpg","caption":"Neha Singh"},"description":"I\u2019m a full-time freelance writer and editor who enjoys wordsmithing. The 8 years long journey as a content writer and editor has made me relaize the significance and power of choosing the right words. Prior to my writing journey, I was a trainer and human resource manager. WIth more than a decade long professional journey, I find myself more powerful as a wordsmith. As an avid writer, everything around me inspires me and pushes me to string words and ideas to create unique content; and when I\u2019m not writing and editing, I enjoy experimenting with my culinary skills, reading, gardening, and spending time with my adorable little mutt Neel.","url":"https:\/\/www.pickl.ai\/blog\/author\/nehasingh\/"}]}},"jetpack_featured_media_url":"https:\/\/www.pickl.ai\/blog\/wp-content\/uploads\/2024\/12\/image8.png","authors":[{"term_id":2169,"user_id":4,"is_guest":0,"slug":"nehasingh","display_name":"Neha Singh","avatar_url":"https:\/\/pickl.ai\/blog\/wp-content\/uploads\/2024\/06\/avatar_user_4_1717572961-96x96.jpg","first_name":"Neha","user_url":"","last_name":"Singh","description":"I\u2019m a full-time freelance writer and editor who enjoys wordsmithing. The 8 years long journey as a content writer and editor has made me relaize the significance and power of choosing the right words. Prior to my writing journey, I was a trainer and human resource manager. WIth more than a decade long professional journey, I find myself more powerful as a wordsmith. As an avid writer, everything around me inspires me and pushes me to string words and ideas to create unique content; and when I\u2019m not writing and editing, I enjoy experimenting with my culinary skills, reading, gardening, and spending time with my adorable little mutt Neel."},{"term_id":2184,"user_id":17,"is_guest":0,"slug":"anubhavjain","display_name":"Anubhav Jain","avatar_url":"https:\/\/pickl.ai\/blog\/wp-content\/uploads\/2024\/05\/avatar_user_17_1715317161-96x96.jpg","first_name":"Anubhav","user_url":"","last_name":"Jain","description":"I am a dedicated data enthusiast and aspiring leader within the realm of data analytics, boasting an engineering background and hands-on experience in the field of data science. My unwavering commitment lies in harnessing the power of data to tackle intricate challenges, all with the goal of making a positive societal impact. Currently, I am gaining valuable insights as a Data Analyst at TransOrg, where I've had the opportunity to delve into the vast potential of machine learning and artificial intelligence in providing innovative solutions to both businesses and learning institutions."}],"_links":{"self":[{"href":"https:\/\/www.pickl.ai\/blog\/wp-json\/wp\/v2\/posts\/17427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pickl.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pickl.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pickl.ai\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pickl.ai\/blog\/wp-json\/wp\/v2\/comments?post=17427"}],"version-history":[{"count":3,"href":"https:\/\/www.pickl.ai\/blog\/wp-json\/wp\/v2\/posts\/17427\/revisions"}],"predecessor-version":[{"id":23287,"href":"https:\/\/www.pickl.ai\/blog\/wp-json\/wp\/v2\/posts\/17427\/revisions\/23287"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pickl.ai\/blog\/wp-json\/wp\/v2\/media\/17432"}],"wp:attachment":[{"href":"https:\/\/www.pickl.ai\/blog\/wp-json\/wp\/v2\/media?parent=17427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pickl.ai\/blog\/wp-json\/wp\/v2\/categories?post=17427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pickl.ai\/blog\/wp-json\/wp\/v2\/tags?post=17427"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.pickl.ai\/blog\/wp-json\/wp\/v2\/ppma_author?post=17427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}