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

  • Home
  • SEARCH
  • 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 700473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:30:07+00:00 2026-05-14T03:30:07+00:00

I have a table in which users store scores and other information about said

  • 0

I have a table in which users store scores and other information about said score (for example notes on score, or time taken etc). I want a mysql query that finds each users personal best score and it’s associated notes and time etc.

What I have tried to use is something like this:

SELECT *, MAX(score) FROM table GROUP BY (user)

The problem with this is that whilst you can extra the users personal best from that query [MAX(score)], the returned notes and times etc are not associated with the maximum score, but a different score (specifically the one contained in *). Is there a way I can write a query that selects what I want? Or will I have to do it manually in PhP?

  • 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-14T03:30:08+00:00Added an answer on May 14, 2026 at 3:30 am

    You can join with a sub query, as in the following example:

    SELECT   t.*,
             sub_t.max_score
    FROM     table t
    JOIN     (SELECT   MAX(score) as max_score,
                       user
              FROM     table
              GROUP BY user) sub_t ON (sub_t.user = t.user AND
                                       sub_t.max_score = t.score);
    

    The above query can be explained as follows. It starts with:

    SELECT t.* FROM table t;
    

    … This by itself will obviously list all the contents of the table. The goal is to keep only the rows that represent a maximum score of a particular user. Therefore if we had the data below:

    +------------------------+
    | user | score |  notes  | 
    +------+-------+---------+
    |    1 |    10 |  note a |
    |    1 |    15 |  note b |    
    |    1 |    20 |  note c |
    |    2 |     8 |  note d |
    |    2 |    12 |  note e |
    |    2 |     5 |  note f |
    +------+-------+---------+
    

    …We would have wanted to keep just the “note c” and “note e” rows.

    To find the rows that we want to keep, we can simply use:

    SELECT MAX(score), user FROM table GROUP BY user;
    

    Note that we cannot get the notes attribute from the above query, because as you had already noticed, you would not get the expected results for fields not aggregated with an aggregate function, like MAX() or not part of the GROUP BY clause. For further reading on this topic, you may want to check:

    • Debunking GROUP BY Myths
    • How does MySQL decide which id to return in group by clause?
    • Why does MySql allow “group by” queries WITHOUT aggregate functions?

    Now we only need to keep the rows from the first query that match the second query. We can do this with an INNER JOIN:

    ...
    JOIN     (SELECT   MAX(score) as max_score,
                       user
              FROM     table
              GROUP BY user) sub_t ON (sub_t.user = t.user AND
                                       sub_t.max_score = t.score);
    

    The sub query is given the name sub_t. It is the set of all the users with the personal best score. The ON clause of the JOIN applies the restriction to the relevant fields. Remember that we only want to keep rows that are part of this subquery.

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

Sidebar

Related Questions

For example I have a table which stores details about properties. Which could have
I have a table of users which has a username column consisting of a
I have a table which is referenced by foreign keys on many other tables.
I have made a leaderboard table on my site, which returns the users in
I have a table User which has an identity column UserID , now what
I have a table of data sorted by date, from which a user can
I have a table which is full of arbitrarily formatted phone numbers, like this
I have a table which contains my server status create table ServerStatus ( ServerId
I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
I have a table which holds flight schedule data. Every schedule have an effective_from

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.