Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5846661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:36:30+00:00 2026-05-22T12:36:30+00:00

How can you select the top n max values from a table? For a

  • 0

How can you select the top n max values from a table?

For a table like this:

column1  column2
   1       foo
   2       foo
   3       foo
   4       foo
   5       bar
   6       bar
   7       bar
   8       bar

For n=2, the result needs to be:

3    
4    
7    
8    

The approach below selects only the max value for each group.

SELECT max(column1) FROM table GROUP BY column2

Returns:

4
8
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:36:30+00:00Added an answer on May 22, 2026 at 12:36 pm

    For n=2 you could

    SELECT max(column1) m 
    FROM table t
    GROUP BY column2
    UNION
    SELECT max(column1) m
    FROM table t
    WHERE column1 NOT IN (SELECT max(column1) 
                          WHERE column2 = t.column2)
    

    for any n you could use approaches described here to simulate rank over partition.

    EDIT:
    Actually this article will give you exactly what you need.

    Basically it is something like this

    SELECT t.*
    FROM
       (SELECT grouper,
              (SELECT val 
               FROM table li
               WHERE li.grouper = dlo.grouper
               ORDER BY
                     li.grouper, li.val DESC
               LIMIT 2,1) AS mid
       FROM 
          (
          SELECT DISTINCT grouper
          FROM table
          ) dlo 
       ) lo, table t
    WHERE t.grouper = lo.grouper
          AND t.val > lo.mid
    

    Replace grouper with the name of the column you want to group by and val with the name of the column that hold the values.

    To work out how exactly it functions go step-by-step from the most inner query and run them.

    Also, there is a slight simplification – the subquery that finds the mid can return NULL if certain category does not have enough values so there should be COALESCE of that to some constant that would make sense in the comparison (in your case it would be MIN of domain of the val, in article it is MAX).

    EDIT2:
    I forgot to mention that it is the LIMIT 2,1 that determines the n (LIMIT n,1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code where you can select the date you like to attend
I can select all the distinct values in a column in the following ways:
I know I can SELECT 5 AS foo and get the resultset: foo 5
I am building a table using the DataGridView where a user can select items
I have a varchar column that has data like this top<somenumber> so the word
I'm using a SELECT statement in T-SQL on a table similar to this: SELECT
I have a query that looks like this: Insert Into tblX (a,b,c) Select x
I have some following set of data from where I want to select Top
I have this big table called StateChanges (1.9 million rows) from a third party
I need to get this sql query working: SELECT TOP 15 id, thumb, width,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.