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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:22:11+00:00 2026-06-06T14:22:11+00:00

Possible Duplicate: SQL ORDER BY total within GROUP BY UPDATE: I’ve found my solution,

  • 0

Possible Duplicate:
SQL ORDER BY total within GROUP BY

UPDATE: I’ve found my solution, which I’ve posted here. Thanks to everyone for your help!


I’m developing a Facebook application which requires a leaderboard. Scores and time taken to complete the game are recorded and these are organised by score first, then in the case of two identical scores, the time is used. If a user has played multiple times, their best score is used.

The lower the score, the better the performance in the game.

My table structure is:

id
facebook_id - (Unique Identifier for the user)
name
email
score
time - (time to complete game in seconds)
timestamp - (unix timestamp of entry)
date - (readable format of timestamp)
ip

The query I thought would work is:

SELECT *
FROM entries
ORDER BY score ASC, time ASC
GROUP BY facebook_id

The problem I’m having is in some cases it’s pulling in the user’s first score in the database, not their highest score. I think this is down to the GROUP BY statement. I would have thought the ORDER BY statement would have fixed this, but apparently not.

For example:

----------------------------------------------------------------------------
|  ID  |       NAME       |  SCORE  |  TIME  |  TIMESTAMP  |  DATE  |  IP  |
----------------------------------------------------------------------------
|  1   |  Joe Bloggs      |  65     |   300  | 1234567890  |  XXX   |  XXX |
----------------------------------------------------------------------------
|  2   |  Jane Doe        |  72     |   280  | 1234567890  |  XXX   |  XXX |
----------------------------------------------------------------------------
|  3   |  Joe Bloggs      |  55     |   285  | 1234567890  |  XXX   |  XXX |
----------------------------------------------------------------------------
|  4   |  Jane Doe        |  78     |   320  | 1234567890  |  XXX   |  XXX |
----------------------------------------------------------------------------

When I use the query above, I get the following result:

 1. Joe Bloggs - 65 - 300 - (Joes First Entry, not his best entry) 
 2. Jane Doe - 72 - 280

I would have expected…

 1. Joe Bloggs - 55 - 285 - (Joe's best entry)
 2. Jane Doe - 72 - 280 

It’s like the Group By is ignoring the Order – and just overwriting the values.

Using MIN(score) with the group by selects the lowest score, which is correct – however it merges the time from the users first record in the database, so often returns incorrectly.

So, how can I select a user’s highest score and the associated time, name, etc and order the results by score, then time?

Thanks in advance!

  • 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-06-06T14:22:13+00:00Added an answer on June 6, 2026 at 2:22 pm

    One approach would be this:

    SELECT entries.facebook_id, MIN(entries.score) AS score, MIN(entries.time) AS time
    FROM entries
        INNER JOIN (
            SELECT facebook_id, MIN(score) AS score
            FROM entries
            GROUP BY facebook_id) highscores
        ON highscores.facebook_id = entries.facebook_id
        AND entries.score = highscores.score
    GROUP BY entries.facebook_id
    ORDER BY MIN(entries.score) ASC, MIN(entries.time) ASC
    

    If you need more information from the entries table, you can then use this as a subquery, and join again on the information presented (facebook_id, score, time) to get one row per user.

    You need to aggregate twice, is the crux of this; once to find the minimum score for the user, and again to find the minimum time for that user and score. You could reverse the order of the aggregation, but I would expect that this will filter most quickly and thus be most efficient.

    You might also want to check which is faster, aggregating the second time: using the minimum score or grouping using the score as well.

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

Sidebar

Related Questions

Possible Duplicate: Ordering by the order of values in a SQL IN() clause With
Possible Duplicate: SQL Select within 24 hours? I am using regular SQL to attempt
Possible Duplicate: SQL Server: Only last entry in GROUP BY I have a table
Possible Duplicate: SQL Count records within a month using a unix timestamp I have
Possible Duplicate: SQL JOIN: is there a difference between USING, ON or WHERE? Which
Possible Duplicate: SQL Delete: can't specify target table for update in FROM clause I'm
Possible Duplicate: SQL Query to Select First/Top N records Here is my code so
Possible Duplicate: Ordering by the order of values in a SQL IN() clause I
Possible Duplicate: SQL: Find the max record per group I have a table with
Possible Duplicate: Ordering by the order of values in a SQL IN() clause I

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.