{"id":1028,"date":"2026-10-30T12:16:01","date_gmt":"2026-10-30T12:16:01","guid":{"rendered":"https:\/\/blog.debuggersspace.com\/?p=1028"},"modified":"2026-10-30T14:12:02","modified_gmt":"2026-10-30T14:12:02","slug":"write-a-query-to-find-the-second-highest-salary-from-an-employee-table","status":"publish","type":"post","link":"https:\/\/blog.debuggersspace.com\/index.php\/2026\/10\/30\/write-a-query-to-find-the-second-highest-salary-from-an-employee-table\/","title":{"rendered":"Write a query to find the second highest salary from an Employee table."},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>2 Minute, 8 Second                <\/div>\n\n            <\/div>\n<p class=\"wp-block-paragraph\">Almost every SQL round asks this. Give one working query, then show you understand ties, the case where there is no second salary, and how to get the Nth highest.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Assume this table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE Employee (\n    id     INT PRIMARY KEY,\n    name   VARCHAR(50),\n    salary INT\n);\n-- salaries: 90000, 75000, 75000, 60000<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The second highest <em>distinct<\/em> salary here is 75000.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Subquery with MAX (works everywhere)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT MAX(salary) AS second_highest\nFROM Employee\nWHERE salary &lt; (SELECT MAX(salary) FROM Employee);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The inner query finds 90000; the outer query takes the largest salary below it. If there is only one distinct salary it returns <code>NULL<\/code> rather than no row, which is usually what the interviewer wants.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. DENSE_RANK (best for the Nth highest)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DISTINCT salary\nFROM (\n    SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk\n    FROM Employee\n) ranked\nWHERE rnk = 2;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Change <code>2<\/code> to N for the Nth highest. To return the employees who earn it, select <code>name, salary<\/code> instead and drop <code>DISTINCT<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. LIMIT and OFFSET (MySQL, PostgreSQL)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DISTINCT salary\nFROM Employee\nORDER BY salary DESC\nLIMIT 1 OFFSET 1;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>DISTINCT<\/code> matters: without it, duplicate top salaries would make this return the top salary again. This returns no row (not <code>NULL<\/code>) when there is no second salary.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. TOP (SQL Server)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT TOP 1 salary\nFROM (SELECT DISTINCT TOP 2 salary FROM Employee ORDER BY salary DESC) t\nORDER BY salary ASC;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">ROW_NUMBER vs RANK vs DENSE_RANK<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For salaries 90000, 75000, 75000, 60000:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>salary  ROW_NUMBER  RANK  DENSE_RANK\n90000   1           1     1\n75000   2           2     2\n75000   3           2     2\n60000   4           4     3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ROW_NUMBER<\/code> would wrongly call the second 75000 the third highest, and <code>RANK<\/code> skips 3, so asking for rank 3 returns nothing. <code>DENSE_RANK<\/code> gives the answer people usually mean by &#8220;Nth highest salary&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common mistakes<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using <code>ORDER BY salary DESC LIMIT 1, 1<\/code> without <code>DISTINCT<\/code>.<\/li>\n\n\n<li>Using <code>ROW_NUMBER<\/code> when salaries can tie.<\/li>\n\n\n<li>Forgetting that <code>NULL<\/code> salaries are ignored by <code>MAX<\/code> but sort differently by database in <code>ORDER BY<\/code>.<\/li>\n\n\n<li>Not saying which database the syntax is for.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Follow-up questions to expect<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Find the highest salary in each department.<\/li>\n\n\n<li>Find employees who earn more than their manager.<\/li>\n\n\n<li>Write it without a subquery or window function (a self join with COUNT).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em>This answer was written by Debugger&#8217;s Space as practice material. It is one of the questions asked in most Indian IT services interviews (TCS, Infosys, Wipro, HCLTech, Tech Mahindra, LTIMindtree, Accenture, Cognizant, Capgemini, IBM). See each company&#8217;s hiring steps and practice questions on <a href=\"https:\/\/www.debuggersspace.com\/companies\">Debugger&#8217;s Space<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Four ways to find the second highest salary in SQL (MAX subquery, DENSE_RANK, LIMIT\/OFFSET and TOP), how to handle ties and missing rows, and how to get the Nth highest.<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[263],"tags":[234,236,235,267,231,237,229,233,265,228,232,280,230],"class_list":["post-1028","post","type-post","status-publish","format-standard","hentry","category-common-interview-questions","tag-accenture","tag-capgemini","tag-cognizant","tag-dbms-interview-questions","tag-hcltech","tag-ibm","tag-infosys","tag-ltimindtree","tag-sql-interview-questions","tag-tcs","tag-tech-mahindra","tag-window-functions","tag-wipro"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Himanshu Namdeo","author_link":"https:\/\/blog.debuggersspace.com\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"Four ways to find the second highest salary in SQL (MAX subquery, DENSE_RANK, LIMIT\/OFFSET and TOP), how to handle ties and missing rows, and how to get the Nth highest.","_links":{"self":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/comments?post=1028"}],"version-history":[{"count":1,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1028\/revisions"}],"predecessor-version":[{"id":1033,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1028\/revisions\/1033"}],"wp:attachment":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media?parent=1028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/categories?post=1028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/tags?post=1028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}