tags:

views:

435

answers:

7

I have created a letter game in java and i need to include a high score function into it. Using Microsoft access database through JDBC. The table in the database contains the following

Table name Easy

fields
Name
Score 
Time

In need an SQL statement that displays the top 5 scores in the order highest score and lowest time. eg. if there are two players who scored 20 the player with the smaller time should be on top but if there is a player who scored 21, he should be on top regardless of the time.

+7  A: 
select top 5 
    name,
    score,
    time

from easy

order by score desc, time asc
Adam Robinson
No problem; don't forget to mark your accepted answer!
Adam Robinson
A: 
Select top 5 Name
From Easy
order by score desc, time asc
Jeremy
A: 
SELECT TOP 5 Score, Time
FROM Easy
ORDER BY Score DESC, Time
DJ
A: 

Other folks have suggested the SELECT TOP 5... solutions, but fwiw, this syntax is nonstandard SQL supported by Microsoft SQL Server and Sybase. If you use another brand of database, it won't work. You should be edit your question and specify what brand and version of database you're using, because the answer could be different based on that information.

Bill Karwin
The second sentence in the question says "Using Microsoft access database through JDBC."
Paul Tomblin
Very well! I added that tag.
Bill Karwin
A: 
select top5 Score, Time from Easy order byScore desc, Time
Schildmeijer
+1  A: 

Not that you asked, but you might be interested to know that the answer is different with different databases. For instance, in Oracle that would be

select
    name,
    score,
    time
from easy
where rownum <= 5
order by score desc, time asc

while in PostgreSQL, it would be

select
    name,
    score,
    time
from easy
order by score desc, time asc
limit 5
Paul Tomblin
I believe your Postgre answer would also work for MySQL.
Matt Grande
+3  A: 

The table is named Easy, which seems to indicate that it's for players who play on Easy mode. I'd recommend adding a column for difficulting and just having one big HighScores table.

SELECT TOP 5 
  name, score, time
FROM HighScores
WHERE difficulty = 'Easy'
ORDER BY score desc, time asc
Matt Grande