When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs



















internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Business Systems Analyst - Clearing - SQL Server - ASP - VB (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

The SQL Guru Answers your Questions...


Today's question comes from Shibu T.:

Dear Mr. SQLGURU,
How do I get the second/third/fourth largest from a column of a single table?

Shibu Thomas

Shibu,

In SQL 6.5:

SET ROWCOUNT 5
SELECT foo
FROM  Table
ORDER BY foo DESC

In SQL 7.0:

SELECT TOP 5 foo
FROM  Table
ORDER BY foo DESC

(the 6.5 solution will also work under 7.0)

These queries will return the top X (in this case, 5) largest values of foo. If you're looking for a specific value, say the second largest, then you could use a cursor or a temp table to get to the correct value.

For more information on TOP and ROWCOUNT be sure to check out the following articles here on 4Guys:

You can also learn more about Microsoft SQL Server 2005's ranking functions at Returning Ranked Results with Microsoft SQL Server 2005.

Sean


Read Other SQL Guru Questions


Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers