<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://codeforeternity.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx</link><description>One of my favorite technical interview question on SQL Server is &amp;quot;What query would you write to find the second highest column value in a table?&amp;quot; The question seems easy, however most developers fail to answer to this. If we had a table named</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#407</link><pubDate>Wed, 24 Feb 2010 03:32:59 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:407</guid><dc:creator>Pradeep</dc:creator><description>&lt;p&gt;Thanks Raj&lt;/p&gt;
&lt;p&gt;I was looking for such answer from long time.&lt;/p&gt;
&lt;p&gt;Its simple and amazing.&lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=407" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#406</link><pubDate>Thu, 18 Feb 2010 16:46:19 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:406</guid><dc:creator>Sharath</dc:creator><description>&lt;p&gt;Can you please explain about Dence Rank function please..&lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=406" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#404</link><pubDate>Fri, 29 Jan 2010 15:53:54 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:404</guid><dc:creator>M.</dc:creator><description>&lt;p&gt;-get All with the subquery in one statement :&lt;/p&gt;
&lt;p&gt;the productId and the product name&lt;/p&gt;
&lt;p&gt;the number of the customers whose buy this product&lt;/p&gt;
&lt;p&gt;the number of the Employess whose sale this product&lt;/p&gt;
&lt;p&gt;the max price that the product was sold with it&lt;/p&gt;
&lt;p&gt;the max quntity that the product was sold in one order &lt;/p&gt;
&lt;p&gt;the category name&lt;/p&gt;
&lt;p&gt;the supplier name&lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=404" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#403</link><pubDate>Fri, 29 Jan 2010 15:53:10 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:403</guid><dc:creator>Mostafa Adam</dc:creator><description>&lt;p&gt;-get All with the subquery in one statment&lt;/p&gt;
&lt;p&gt;1-the productId and the product name&lt;/p&gt;
&lt;p&gt;2-the number of the customers whose buy this product&lt;/p&gt;
&lt;p&gt;3-the number of the Employess whose sale this product&lt;/p&gt;
&lt;p&gt;4-the max price that the product was sold with it&lt;/p&gt;
&lt;p&gt;5-the max quntity that the product was sold in one order &lt;/p&gt;
&lt;p&gt;6-the category name&lt;/p&gt;
&lt;p&gt;7-the supplier name&lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=403" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#402</link><pubDate>Fri, 29 Jan 2010 06:36:38 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:402</guid><dc:creator>Murugaveni.K</dc:creator><description>&lt;p&gt;Please send how to code to find the highest record from a field while the number is added with any string.&lt;/p&gt;
&lt;p&gt;for example &amp;quot;BIS001,BIS002,BIS009,BIS010,BIS003&amp;quot;...i need ans hear is BIS010&lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=402" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#401</link><pubDate>Fri, 29 Jan 2010 06:36:35 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:401</guid><dc:creator>Murugaveni.K</dc:creator><description>&lt;p&gt;Please send how to code to find the highest record from a field while the number is added with any string.&lt;/p&gt;
&lt;p&gt;for example &amp;quot;BIS001,BIS002,BIS009,BIS010,BIS003&amp;quot;...i need ans hear is BIS010&lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=401" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#390</link><pubDate>Tue, 22 Dec 2009 12:54:41 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:390</guid><dc:creator>Sudhakar Nune</dc:creator><description>&lt;p&gt;To get second highest salory from tbale in SQL developer use below qry :: &lt;/p&gt;
&lt;p&gt;select * from (select distinct sal,rownum rn from Table_nam order by sal desc ) where rn =2 &lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=390" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#369</link><pubDate>Wed, 16 Sep 2009 04:25:09 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:369</guid><dc:creator>Harnish Prajapati</dc:creator><description>&lt;p&gt;select top 1 Ename,salary from (select top 2 Ename,salary from tblsalary order by salary desc) a&lt;/p&gt;
&lt;p&gt;order by salary asc&lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=369" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#368</link><pubDate>Wed, 16 Sep 2009 04:22:52 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:368</guid><dc:creator>Harnish Prajapati</dc:creator><description>&lt;p&gt;select top 1 Ename,salary from (select top 2 Ename,salary from tblsalary order by salary desc) a&lt;/p&gt;
&lt;p&gt;order by salary asc&lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=368" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#367</link><pubDate>Sat, 12 Sep 2009 06:20:10 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:367</guid><dc:creator>Karthik</dc:creator><description>&lt;p&gt;select a.salespersonid,salesquota from Sales.SalesPerson A where 3=(select count(distinct&lt;/p&gt;
&lt;p&gt;B.salesquota) from Sales.SalesPerson B where B.salesquota&amp;gt;=A.salesquota)&lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=367" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#366</link><pubDate>Wed, 02 Sep 2009 18:05:41 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:366</guid><dc:creator>jigneshthumar@yahoo.com</dc:creator><description>&lt;p&gt;SELECT *&lt;/p&gt;
&lt;p&gt;FROM Employee&lt;/p&gt;
&lt;p&gt;WHERE Sal = (&lt;/p&gt;
&lt;p&gt;SELECT Sal&lt;/p&gt;
&lt;p&gt;FROM Employee&lt;/p&gt;
&lt;p&gt;GROUP BY Sal D&lt;/p&gt;
&lt;p&gt;LIMIT 1 , 1 )&lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=366" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#356</link><pubDate>Tue, 14 Jul 2009 19:25:00 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:356</guid><dc:creator>Ian Fila</dc:creator><description>&lt;p&gt;How would you do this with two fields ?&lt;/p&gt;
&lt;p&gt;I have a bunch of records, uniqueid.&lt;/p&gt;
&lt;p&gt;The higher the uniqueid the newer the record.&lt;/p&gt;
&lt;p&gt;I want to extract all the records with the highest uniqueid. &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt;eg.&lt;/p&gt;
&lt;p&gt;abc,123&lt;/p&gt;
&lt;p&gt;abc,124&lt;/p&gt;
&lt;p&gt;abc,125&lt;/p&gt;
&lt;p&gt;I want to extract only abc,125.&lt;/p&gt;
&lt;p&gt;Thanks &lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=356" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#350</link><pubDate>Thu, 02 Jul 2009 23:21:27 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:350</guid><dc:creator>IanSmith</dc:creator><description>&lt;p&gt;Two issues:&lt;/p&gt;
&lt;p&gt;If I have &lt;/p&gt;
&lt;p&gt;1000&lt;/p&gt;
&lt;p&gt;2000&lt;/p&gt;
&lt;p&gt;3000&lt;/p&gt;
&lt;p&gt;4000&lt;/p&gt;
&lt;p&gt;5000&lt;/p&gt;
&lt;p&gt;5000&lt;/p&gt;
&lt;p&gt;1) By my interpretation, the third highest salary is that salery earned by the third highest paid employee. That would be 4000, not 3000. All these efforts to eliminate ties don't belong here. Nor does Dense Rank.&lt;/p&gt;
&lt;p&gt;2) If you ask what is the 7th highest salary in this list, many of these techniques will fail.&lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=350" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#349</link><pubDate>Thu, 02 Jul 2009 07:57:05 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:349</guid><dc:creator>SPK</dc:creator><description>&lt;p&gt;You can accomplish this using the RANK function as well. The query is given below:&lt;/p&gt;
&lt;p&gt;SELECT * FROM &lt;/p&gt;
&lt;p&gt;(SELECT salary, RANK () OVER(ORDER BY salary DESC) AS rank FROM emp GROUP BY salary) AS e&lt;/p&gt;
&lt;p&gt;WHERE e.rank = 2 &lt;/p&gt;
&lt;p&gt;NOTE: You can just change the value of the number (1,2,3 etc) to get the first, sceond, third..highest column value.&lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=349" width="1" height="1"&gt;</description></item><item><title>re: T-SQL Query to Find the Second Highest Column Value in a Table</title><link>http://codeforeternity.com/blogs/technology/archive/2008/01/10/t-sql-query-to-find-the-second-highest-column-value-in-a-table.aspx#348</link><pubDate>Thu, 02 Jul 2009 07:55:51 GMT</pubDate><guid isPermaLink="false">6581de12-b79f-4db0-af9f-717dfd7c7876:348</guid><dc:creator>SPK</dc:creator><description>&lt;p&gt;You can accomplish this using the RANK function as well. The query is given below:&lt;/p&gt;
&lt;p&gt;SELECT * FROM &lt;/p&gt;
&lt;p&gt;(SELECT salary, RANK () OVER(ORDER BY salary DESC) AS rank FROM emp GROUP BY salary) AS e&lt;/p&gt;
&lt;p&gt;WHERE e.rank = 2 &lt;/p&gt;
&lt;p&gt;NOTE: You can just change the value of the number (1,2,3 etc) to get the first, sceond, third..highest column value.&lt;/p&gt;
&lt;img src="http://codeforeternity.com/aggbug.aspx?PostID=348" width="1" height="1"&gt;</description></item></channel></rss>