{"id":82,"date":"2014-08-15T19:44:30","date_gmt":"2014-08-15T14:14:30","guid":{"rendered":"http:\/\/pramodsingla.wordpress.com\/?p=82"},"modified":"2014-08-15T19:44:30","modified_gmt":"2014-08-15T14:14:30","slug":"sql-server-find-nth-highest-salary","status":"publish","type":"post","link":"https:\/\/pramodsingla.com\/?p=82","title":{"rendered":"SQL Server -Find Nth Highest Salary"},"content":{"rendered":"<p style=\"text-align:justify;\"><span style=\"color:#ff0000;\"><strong>Problem:<\/strong><\/span> Find Nth highest salary in Minimal time.<\/p>\n<p style=\"text-align:justify;\"><strong><span style=\"color:#008000;\">Solution:<\/span>\u00a0<\/strong>Interviewer often\u00a0asks\u00a0to find Nth Highest Salary in an\u00a0interview. Many solutions are available\u00a0on internet to do so. Couple of commonly\u00a0used solutions are\u00a0<a title=\"Solution 1\" href=\"http:\/\/beyondrelational.com\/blogs\/madhivanan\/archive\/2007\/08\/27\/find-nth-maximum-value.aspx\" target=\"_blank\" rel=\"noopener\">Solution 1<\/a>\u00a0&amp;\u00a0<a title=\"Solution 2\" href=\"http:\/\/blog.sqlauthority.com\/2008\/04\/02\/sql-server-find-nth-highest-salary-of-employee-query-to-retrieve-the-nth-maximum-value\/\" target=\"_blank\" rel=\"noopener\">Solution 2<\/a>.<\/p>\n<p style=\"text-align:justify;\">All these\u00a0solutions\u00a0are bit slow. But by using<em> <strong>Common Expression Tables (CTE)<\/strong><\/em> you can find the Nth highest salary very\u00a0efficiently and impress your interviewer.<\/p>\n<ul>\n<li><span style=\"color:#0000ff;\"><strong><span style=\"text-decoration:underline;\">Create a Sample Table and \u00a0Insert Sample Data in it:<\/span><\/strong><\/span><\/li>\n<\/ul>\n<p style=\"padding-left:30px;\">CREATE TABLE #T1 (ID1 INT, [Name] NVARCHAR(50), Salary INT)<\/p>\n<p style=\"padding-left:30px;\"><em>After creating the table, execute the above code to insert the data into the table as follows:<\/em><\/p>\n<p style=\"padding-left:30px;\">INSERT INTO #T1 VALUES (1, &#8216;pds&#8217;, 10000)<br \/>\nINSERT INTO #T1 VALUES (2, &#8216;vicky&#8217;, 10000)<br \/>\nINSERT INTO #T1 VALUES (3, &#8216;divyan&#8217;, 30000)<br \/>\nINSERT INTO #T1 VALUES (4, &#8216;deepak&#8217;, 40000)<br \/>\nINSERT INTO #T1 VALUES (5, &#8216;anu&#8217;, 50000)<br \/>\nINSERT INTO #T1 VALUES (6, &#8216;jai&#8217;, 60000)<br \/>\nINSERT INTO #T1 VALUES (7, &#8216;abhi&#8217;, 20000)<br \/>\nINSERT INTO #T1 VALUES (8, &#8216;disha&#8217;, 40000)<br \/>\nINSERT INTO #T1 VALUES (9, &#8216;anika&#8217;, 60000)<br \/>\nINSERT INTO #T1 VALUES (10, &#8216;vijay&#8217;, 50000)<\/p>\n<p style=\"padding-left:30px;\"><em>To find the highest salary we can simply use the <strong>Max function as\u00a0<\/strong>given below:<\/em><\/p>\n<p style=\"padding-left:30px;\">Select Max(Salary) as [Highest Salary] from T1<\/p>\n<ul>\n<li><span style=\"color:#0000ff;\"><strong><span style=\"text-decoration:underline;\">Following Query will\u00a0Find Nth Highest Salary in minimal time:<\/span><\/strong><\/span><\/li>\n<\/ul>\n<p style=\"text-align:left;\"><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0<\/em><em>declare @n int;<\/em><\/p>\n<p><em>\u00a0 \u00a0 \u00a0 \u00a0 set @n =4<\/em><\/p>\n<p><em>\u00a0 \u00a0 \u00a0 \u00a0 with cte as <\/em><\/p>\n<p><em>\u00a0 \u00a0 \u00a0 \u00a0(<\/em><\/p>\n<p><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT name, salary,<\/em><br \/>\n<em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 dense_rank() OVER( ORDER BY salary DESC) AS rowid<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0FROM\u00a0\u00a0 #t1 <\/em><br \/>\n<em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 )<\/em><\/p>\n<p><em>\u00a0 \u00a0 \u00a0SELECT *<\/em><br \/>\n<em>\u00a0 \u00a0 FROM\u00a0\u00a0 cte<\/em><br \/>\n<em>\u00a0 \u00a0 WHERE\u00a0 rowid = @n<\/em><\/p>\n<p><em>\u00a0 \u00a0 \u00a0<\/em><em><code>This will create row number for each row after it has been sorted by salary in the descending order.<\/code><\/em><\/p>\n<ul>\n<li><span style=\"color:#0000ff;\"><b><span style=\"text-decoration:underline;\">Comparison Stats with Other Solutions:<\/span><\/b><\/span><\/li>\n<\/ul>\n<p><a href=\"http:\/\/pramodsingla.files.wordpress.com\/2011\/11\/nth-highest-salary-performance.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-83\" title=\"Nth Highest Salary performance\" src=\"http:\/\/pramodsingla.files.wordpress.com\/2011\/11\/nth-highest-salary-performance.jpg\" alt=\"\" width=\"750\" height=\"563\" srcset=\"https:\/\/pramodsingla.com\/wp-content\/uploads\/2011\/11\/nth-highest-salary-performance.jpg 1057w, https:\/\/pramodsingla.com\/wp-content\/uploads\/2011\/11\/nth-highest-salary-performance-300x225.jpg 300w, https:\/\/pramodsingla.com\/wp-content\/uploads\/2011\/11\/nth-highest-salary-performance-1024x769.jpg 1024w, https:\/\/pramodsingla.com\/wp-content\/uploads\/2011\/11\/nth-highest-salary-performance-768x577.jpg 768w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Find Nth highest salary in Minimal time. Solution:\u00a0Interviewer often\u00a0asks\u00a0to find Nth Highest Salary in an\u00a0interview. Many solutions are available\u00a0on[&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[17],"tags":[119,159,162,167],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/posts\/82"}],"collection":[{"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=82"}],"version-history":[{"count":0,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/posts\/82\/revisions"}],"wp:attachment":[{"href":"https:\/\/pramodsingla.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}