{"id":213,"date":"2017-12-07T09:22:51","date_gmt":"2017-12-07T09:22:51","guid":{"rendered":"https:\/\/blinksql.com\/?p=213"},"modified":"2017-12-07T09:22:51","modified_gmt":"2017-12-07T09:22:51","slug":"to-find-the-nth-highest-and-least-salary","status":"publish","type":"post","link":"https:\/\/blinksql.com\/?p=213","title":{"rendered":"To Find the nth highest and least salary"},"content":{"rendered":"<p><strong>1. To Find the nth highest salary<br \/>\n<\/strong><br \/>\nEg: General query for all Database<br \/>\n<strong>SELECT DISTINCT salary FROM emp X WHERE n =<br \/>\n    ( SELECT COUNT(DISTINCT salary) FROM emp WHERE salary &gt;=X.salary )<\/strong><br \/>\nReplace n with the given number. for example to get the 3rd highest salary<br \/>\n<strong>SELECT DISTINCT salary FROM emp X WHERE 3 =<br \/>\n( SELECT COUNT(DISTINCT salary) FROM emp WHERE salary &gt;=X.salary )<\/strong><br \/>\n<img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/blinksql.com\/wp-content\/uploads\/2017\/12\/nth-highest-salary.png\" alt=\"nth highest salary\" width=\"957\" height=\"220\" class=\"alignnone size-full wp-image-214\" \/><\/p>\n<p><strong>In Postgresql:<\/strong><br \/>\nsimly we can use offest and limit<br \/>\n<strong>Query:select salary from emp order by salary desc offset n limit 1;<br \/>\n<\/strong><br \/>\n-&gt; Here replace the n with the given number-1. for example to get the 3rd highest salary we have to give 2.<br \/>\n<img decoding=\"async\" src=\"https:\/\/blinksql.com\/wp-content\/uploads\/2017\/12\/nth-salary-using-offset.png\" alt=\"nth salary using offset\" width=\"573\" height=\"106\" class=\"alignnone size-full wp-image-215\" \/><\/p>\n<p><strong>1.To Find the nth least salary<br \/>\n<\/strong><br \/>\n<strong>Query:select salary from emp order by salary asc offset 2 limit 1<br \/>\n<\/strong><br \/>\n-&gt; Here replace the n with the given number-1. for example to get the 3rd least salary we have to give 2.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blinksql.com\/wp-content\/uploads\/2017\/12\/least-salary.png\" alt=\"least salary\" width=\"587\" height=\"107\" class=\"alignnone size-full wp-image-216\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. To Find the nth highest salary Eg: General query for all Database SELECT DISTINCT salary FROM emp X WHERE n = ( SELECT COUNT(DISTINCT salary) FROM emp WHERE salary&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":""},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.10 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>To Find the nth highest and least salary - Blink SQL<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blinksql.com\/?p=213\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"To Find the nth highest and least salary - Blink SQL\" \/>\n<meta property=\"og:description\" content=\"1. To Find the nth highest salary Eg: General query for all Database SELECT DISTINCT salary FROM emp X WHERE n = ( SELECT COUNT(DISTINCT salary) FROM emp WHERE salary&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blinksql.com\/?p=213\" \/>\n<meta property=\"og:site_name\" content=\"Blink SQL\" \/>\n<meta property=\"article:published_time\" content=\"2017-12-07T09:22:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blinksql.com\/wp-content\/uploads\/2017\/12\/nth-highest-salary.png\" \/>\n<meta name=\"author\" content=\"Hannie\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@harika_sanaka\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Hannie\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blinksql.com\/?p=213#article\",\"isPartOf\":{\"@id\":\"https:\/\/blinksql.com\/?p=213\"},\"author\":{\"name\":\"Hannie\",\"@id\":\"https:\/\/blinksql.com\/#\/schema\/person\/f04b99304f59e14956e768e764d2afd4\"},\"headline\":\"To Find the nth highest and least salary\",\"datePublished\":\"2017-12-07T09:22:51+00:00\",\"dateModified\":\"2017-12-07T09:22:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blinksql.com\/?p=213\"},\"wordCount\":153,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/blinksql.com\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blinksql.com\/?p=213#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blinksql.com\/?p=213\",\"url\":\"https:\/\/blinksql.com\/?p=213\",\"name\":\"To Find the nth highest and least salary - Blink SQL\",\"isPartOf\":{\"@id\":\"https:\/\/blinksql.com\/#website\"},\"datePublished\":\"2017-12-07T09:22:51+00:00\",\"dateModified\":\"2017-12-07T09:22:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/blinksql.com\/?p=213#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blinksql.com\/?p=213\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blinksql.com\/?p=213#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blinksql.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"To Find the nth highest and least salary\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blinksql.com\/#website\",\"url\":\"https:\/\/blinksql.com\/\",\"name\":\"Blink SQL\",\"description\":\" Thinking SQL\",\"publisher\":{\"@id\":\"https:\/\/blinksql.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blinksql.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/blinksql.com\/#organization\",\"name\":\"Blink SQL\",\"url\":\"https:\/\/blinksql.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blinksql.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blinksql.com\/wp-content\/uploads\/2022\/11\/cropped-BlinkSQL-removebg.png\",\"contentUrl\":\"https:\/\/blinksql.com\/wp-content\/uploads\/2022\/11\/cropped-BlinkSQL-removebg.png\",\"width\":280,\"height\":172,\"caption\":\"Blink SQL\"},\"image\":{\"@id\":\"https:\/\/blinksql.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/blinksql.com\/#\/schema\/person\/f04b99304f59e14956e768e764d2afd4\",\"name\":\"Hannie\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blinksql.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c5876b3497d2c2eb98256b7c183220c1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c5876b3497d2c2eb98256b7c183220c1?s=96&d=mm&r=g\",\"caption\":\"Hannie\"},\"sameAs\":[\"http:\/\/blinksql.com\",\"https:\/\/twitter.com\/harika_sanaka\"],\"url\":\"https:\/\/blinksql.com\/?author=2\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"To Find the nth highest and least salary - Blink SQL","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:\/\/blinksql.com\/?p=213","og_locale":"en_US","og_type":"article","og_title":"To Find the nth highest and least salary - Blink SQL","og_description":"1. To Find the nth highest salary Eg: General query for all Database SELECT DISTINCT salary FROM emp X WHERE n = ( SELECT COUNT(DISTINCT salary) FROM emp WHERE salary&hellip;","og_url":"https:\/\/blinksql.com\/?p=213","og_site_name":"Blink SQL","article_published_time":"2017-12-07T09:22:51+00:00","og_image":[{"url":"https:\/\/blinksql.com\/wp-content\/uploads\/2017\/12\/nth-highest-salary.png"}],"author":"Hannie","twitter_card":"summary_large_image","twitter_creator":"@harika_sanaka","twitter_misc":{"Written by":"Hannie","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blinksql.com\/?p=213#article","isPartOf":{"@id":"https:\/\/blinksql.com\/?p=213"},"author":{"name":"Hannie","@id":"https:\/\/blinksql.com\/#\/schema\/person\/f04b99304f59e14956e768e764d2afd4"},"headline":"To Find the nth highest and least salary","datePublished":"2017-12-07T09:22:51+00:00","dateModified":"2017-12-07T09:22:51+00:00","mainEntityOfPage":{"@id":"https:\/\/blinksql.com\/?p=213"},"wordCount":153,"commentCount":0,"publisher":{"@id":"https:\/\/blinksql.com\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blinksql.com\/?p=213#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blinksql.com\/?p=213","url":"https:\/\/blinksql.com\/?p=213","name":"To Find the nth highest and least salary - Blink SQL","isPartOf":{"@id":"https:\/\/blinksql.com\/#website"},"datePublished":"2017-12-07T09:22:51+00:00","dateModified":"2017-12-07T09:22:51+00:00","breadcrumb":{"@id":"https:\/\/blinksql.com\/?p=213#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blinksql.com\/?p=213"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blinksql.com\/?p=213#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blinksql.com\/"},{"@type":"ListItem","position":2,"name":"To Find the nth highest and least salary"}]},{"@type":"WebSite","@id":"https:\/\/blinksql.com\/#website","url":"https:\/\/blinksql.com\/","name":"Blink SQL","description":" Thinking SQL","publisher":{"@id":"https:\/\/blinksql.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blinksql.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/blinksql.com\/#organization","name":"Blink SQL","url":"https:\/\/blinksql.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blinksql.com\/#\/schema\/logo\/image\/","url":"https:\/\/blinksql.com\/wp-content\/uploads\/2022\/11\/cropped-BlinkSQL-removebg.png","contentUrl":"https:\/\/blinksql.com\/wp-content\/uploads\/2022\/11\/cropped-BlinkSQL-removebg.png","width":280,"height":172,"caption":"Blink SQL"},"image":{"@id":"https:\/\/blinksql.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/blinksql.com\/#\/schema\/person\/f04b99304f59e14956e768e764d2afd4","name":"Hannie","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blinksql.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c5876b3497d2c2eb98256b7c183220c1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c5876b3497d2c2eb98256b7c183220c1?s=96&d=mm&r=g","caption":"Hannie"},"sameAs":["http:\/\/blinksql.com","https:\/\/twitter.com\/harika_sanaka"],"url":"https:\/\/blinksql.com\/?author=2"}]}},"_links":{"self":[{"href":"https:\/\/blinksql.com\/index.php?rest_route=\/wp\/v2\/posts\/213"}],"collection":[{"href":"https:\/\/blinksql.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blinksql.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blinksql.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blinksql.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=213"}],"version-history":[{"count":0,"href":"https:\/\/blinksql.com\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions"}],"wp:attachment":[{"href":"https:\/\/blinksql.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blinksql.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blinksql.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}