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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:00:14+00:00 2026-05-11T15:00:14+00:00

Here’s a real noobish MySQL query problem I’m having. I have a high score

  • 0

Here’s a real noobish MySQL query problem I’m having.

I have a high score table in a game I’m writing. The high score DB records a name, level, and score achieved. There are many near duplicates in the db. For example:

Name | Level | Score | Timestamp (key) Bob    2       41    | 1234567.890 Bob    3       15    | 1234568.890 Bob    3       20    | 1234569.890 Joe    2       40    | 1234561.890 Bob    3       21    | 1234562.890 Bob    3       21    | 1234563.890 

I want to return a ‘highest level achieved’ high score list, with an output similar to:

Name | Level | Score Bob    3       21 Joe    2       40 

The SQL Query I currently use is:

SELECT *, MAX(level) as level  FROM highscores  GROUP BY name ORDER BY level DESC, score DESC LIMIT 5 

However this doesn’t quite work. The ‘Score’ field output always seems to be randomly pulled from the group, instead of taking the corresponding score for the highest level achieved. Eg:

Name | Level | Score Bob    3       41 Joe    2       40 

Bob never got 41 points on level 3! How can I fix this?

  • 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. 2026-05-11T15:00:15+00:00Added an answer on May 11, 2026 at 3:00 pm

    You’ll need to use a subquery to pull the score out.

    select distinct     name,      max(level) as level,     (select max(score) from highscores h2          where h2.name = h1.name and h2.level = h1.level) as score from highscores h1  group by name  order by level desc, score desc 

    Cheers,

    Eric

    It irks me that I didn’t take the time to explain why this is the case when I posted the answer, so here goes:

    When you pull back everything (*), and then the max level, what you’ll get is each record sequentially, plus a column with the max level on it. Note that you’re not grouping by score (which would have given you Bob 2 41, and Bob 3 21–two records for our friend Bob).

    So, how the heck do we fix this? You need to do a subquery to additionally filter your results, which is what that (select max(score)…) is. Now, for each row that reads Bob, you will get his max level (3), and his max score at that level (21). But, this still gives us however many rows Bob has (e.g.-if he has 5 rows, you’ll get 5 rows of Bob 3 21). To limit this to only the top score, we need to use a DISTINCT clause in the select statement to only return unique rows.

    UPDATE: Correct SQL (can’t comment on le dorfier’s post):

    SELECT h1.Name, h1.Level, MAX(h1.Score)     FROM highscores h1     LEFT OUTER JOIN highscores h2 ON h1.name = h2.name AND h1.level < h2.level     LEFT OUTER JOIN highscores h3 ON h1.name = h3.name AND h2.level = h3.level AND h1.score < h3.score     WHERE h2.Name IS NULL AND h3.Name IS NULL     GROUP BY h1.Name, h1.Level 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the issue I am having: I have a large query that needs
Here's a problem I ran into recently. I have attributes strings of the form
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes
Here's an interesting problem. On a recently installed Server 2008 64bit I opened IE
Here we go again, the old argument still arises... Would we better have a
Here is a link my example of the misaligned table rows Click preview in
Here’s a problem I’ve really been struggling with. I need to merge two sorted

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.