Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I have simple question:

I have a postgresql database: Scores(score integer).

How would I get the highest 10 scores the fastest?

UPDATE:

I will be doing this query multiple times and am aiming for the fastest solution.

1 Answer

0 votes
by (40.7k points)

You can use the limit, and try the code given below: 

Query:

select *

from scores

order by score desc

limit 10

Note: If performance is not in the priority then you can look for an index on the score. 

In version 8.4 and above, you can use the standard (SQL:2008) and fetch first like this:

Query:

select *

from scores

order by score desc

fetch first 10 rows only

Related questions

0 votes
0 answers
0 votes
1 answer
asked Dec 17, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
asked Jul 26, 2019 in SQL by Tech4ever (20.3k points)

Browse Categories

...