Skip to content

Ozar.net Developer Blog

Programming in PHP, C, C#, ASP.NET, Java, Objective-C, SQL also for MS SQL Server, Oracle & MySQL Development

Menu
  • Home
  • About
  • Tutorials
  • Contact
  • Privacy
Menu

T-SQL: How do I find the greatest values in a column?

Posted on March 31, 2010April 1, 2010 by OD

How can write a SELECT statement that finds the n greatest values in a column? From what i’ve read, the TOP statement only returns the first n rows, it doesn’t take any regard to the values. For example, if a have a column with ages, i want to be able to find the 5 oldest people.

3 thoughts on “T-SQL: How do I find the greatest values in a column?”

  1. Victor says:
    March 31, 2010 at 12:46 PM

    You can use the ORDER BY directive to sort by columns then use TOP to grab what you need. Don’t forget that you can also sort ascending or descending.

    Log in to Reply
  2. KegC says:
    March 31, 2010 at 12:59 PM

    if you’ve got an age column you can find the 5 oldest by sorting so that high ages come first and then grabbing just the first 5 records

    select top 5 * from People
    order by Age desc

    or

    or if you’ve got a date of birth column, you can sort that so that the earliest date comes first and grab the first 5 records from there

    select top 5 * from People
    order by DateOfBirth asc

    Log in to Reply
  3. Prajakta says:
    March 31, 2010 at 1:51 PM

    This is simple

    to find top 5 people who are oldest

    1. Order you select clause here it will be in ascending order b y Date Of birth and the older the date the older the person.

    2. select top 5 from the records set

    Table Structure: Table Name t_People

    People_ID
    Name
    Date_Of _Birth

    SQL:

    Select Top 5 Name
    From t_People
    Order by Date_Of _Birth Asc

    Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • NSTableView with Core Data Tutorial using NSFetchedResultsController
  • CodeIgniter Wizard – a new PHP CRUD Code Generator for macOS
  • Installing Oracle Database 10g Express Edition and Changing the Default HTTP Port
  • Xcode Tutorial 2 – Putting iAd into your App (The Apple Approved Way)
  • Cocoa Programming: A Quick-Start Guide for Developers

Blogroll

  • All Systems GO
  • GOZmosis
  • Maclord's Blog
  • Ozar.net

Tags

.NET 2008 application ASP.NET basic Beginning best books business Business Objects C# Database Databases Development Edition Guide iPhone Java Java EE learn Learning Lesson Microsoft MS SQL Server MySQL Objective-C Programming objects Oracle Part PHP PL/SQL Professional Programming Server Software SQL T-SQL TSQL Tutorial Tutorials Using Video Visual Visual Basic .NET Web Programming Tutorials
© 2026 Ozar.net Developer Blog | Powered by Minimalist Blog WordPress Theme

Powered by
►
Necessary cookies enable essential site features like secure log-ins and consent preference adjustments. They do not store personal data.
None
►
Functional cookies support features like content sharing on social media, collecting feedback, and enabling third-party tools.
None
►
Analytical cookies track visitor interactions, providing insights on metrics like visitor count, bounce rate, and traffic sources.
None
►
Advertisement cookies deliver personalized ads based on your previous visits and analyze the effectiveness of ad campaigns.
None
►
Unclassified cookies are cookies that we are in the process of classifying, together with the providers of individual cookies.
None
Powered by