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 486135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:28:21+00:00 2026-05-13T01:28:21+00:00

Here’s a simplified example of what I’m talking about: Table: students exam_results _____________ ____________________________________

  • 0

Here’s a simplified example of what I’m talking about:

Table: students      exam_results
_____________       ____________________________________
| id | name |       | id | student_id | score |   date |
|----+------|       |----+------------+-------+--------|
|  1 | Jim  |       |  1 |          1 |    73 | 8/1/09 | 
|  2 | Joe  |       |  2 |          1 |    67 | 9/2/09 |
|  3 | Jay  |       |  3 |          1 |    93 | 1/3/09 |
|____|______|       |  4 |          2 |    27 | 4/9/09 |
                    |  5 |          2 |    17 | 8/9/09 |
                    |  6 |          3 |   100 | 1/6/09 |
                    |____|____________|_______|________|

Assume, for the sake of this question, that every student has at least one exam result recorded.

How would you select each student along with their highest score? Edit: …AND the other fields in that record?

Expected output:

_________________________
| name | score |   date |
|------+-------|--------|
|  Jim |    93 | 1/3/09 |
|  Joe |    27 | 4/9/09 |
|  Jay |   100 | 1/6/09 |
|______|_______|________|

Answers using all types of DBMS are welcome.

  • 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-13T01:28:21+00:00Added an answer on May 13, 2026 at 1:28 am

    Answering the EDITED question (i.e. to get associated columns as well).

    In Sql Server 2005+, the best approach would be to use a ranking/window function in conjunction with a CTE, like this:

    with exam_data as
    (
        select  r.student_id, r.score, r.date,
                row_number() over(partition by r.student_id order by r.score desc) as rn
        from    exam_results r
    )
    select  s.name, d.score, d.date, d.student_id
    from    students s
    join    exam_data d
    on      s.id = d.student_id
    where   d.rn = 1;
    

    For an ANSI-SQL compliant solution, a subquery and self-join will work, like this:

    select  s.name, r.student_id, r.score, r.date
    from    (
                select  r.student_id, max(r.score) as max_score
                from    exam_results r
                group by r.student_id
            ) d
    join    exam_results r
    on      r.student_id = d.student_id
    and     r.score = d.max_score
    join    students s
    on      s.id = r.student_id;
    

    This last one assumes there aren’t duplicate student_id/max_score combinations, if there are and/or you want to plan to de-duplicate them, you’ll need to use another subquery to join to with something deterministic to decide which record to pull. For example, assuming you can’t have multiple records for a given student with the same date, if you wanted to break a tie based on the most recent max_score, you’d do something like the following:

    select  s.name, r3.student_id, r3.score, r3.date, r3.other_column_a, ...
    from    (
                select  r2.student_id, r2.score as max_score, max(r2.date) as max_score_max_date
                from    (
                            select  r1.student_id, max(r1.score) as max_score
                            from    exam_results r1
                            group by r1.student_id
                        ) d
                join    exam_results r2
                on      r2.student_id = d.student_id
                and     r2.score = d.max_score
                group by r2.student_id, r2.score
            ) r
    join    exam_results r3
    on      r3.student_id = r.student_id
    and     r3.score = r.max_score
    and     r3.date = r.max_score_max_date
    join    students s
    on      s.id = r3.student_id;
    

    EDIT: Added proper de-duplicating query thanks to Mark’s good catch in comments

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

Sidebar

Related Questions

Here is the code in a function I'm trying to revise. This example works
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id
Here's my code: NSDate *currDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter
here is the html <table> <tr> <td class=break>mono</td> </tr> <tr> <td>c1</td> <td>c2</td> <td>c3</td> </tr>
Here are some facts about my app followed by a question My app has
Here are the tables I have: Table A which has entries with item and
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
here is what I am dealing with: I am given a table (Visit) with
Here's how my DataTable looks like(2 columns: Name and Code ): Name Code name1

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.