Matthew Smart
Matthew Smart

Reputation: 1319

find top entries in MYSQL database

In my database each row has a column for average rating.

Now lets say I have thousands of rows with a variety of averages such as 4.45 4.78 3.21 2.13 4.91

How would I get the rows with the top 3 highest average?

Upvotes: 1

Views: 101

Answers (3)

Manashvi Birla
Manashvi Birla

Reputation: 2843

Order the avg by DESC order to find largest avergaes and then LIMIT 5 to find the top 5 avg

SELECT avg FROM table_name ORDER BY avg DESC LIMIT 5;

Upvotes: 0

Hiren
Hiren

Reputation: 1435

Syntax for TOP Clause:

SELECT column_name(s) FROM table_name LIMIT number;

SELECT *
FROM Table order by column  Desc
LIMIT 5;

or u can use

SELECT TOP 2 * FROM Table;

but it will give you top 2 based on your primary key

Upvotes: 0

potashin
potashin

Reputation: 44581

You can order rows in descending with order by average_rating desc and limit output to the top 3 results:

select average_rating
from tbl
order by average_rating desc
limit 3

Upvotes: 2

Related Questions