Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select top 5 max values in mytable

Tags:

mysql

Please help me with Query in Mysql.. i am Having table contains lot of rows .now i want retrive the 5 rows from that table.

my requirement is top maximum 5 values in that table "column name is amount" i want select from that table.outof N records i need top max 5 records from table

Thanking you,

like image 407
santhosh Avatar asked Apr 13 '11 09:04

santhosh


People also ask

How do you select the top 3 maximum value in SQL?

To get the maximum value from three different columns, use the GREATEST() function. Insert some records in the table using insert command. Display all records from the table using select statement.

How do you select the top 2 maximum value in SQL?

2nd highest value in SQL using Max() function SELECT MAX (ProductID) FROM Production. WorkOrder WHERE ProductID NOT IN (SELECT MAX (ProductID) FROM Production. WorkOrder);

How do you select the highest value in each group in SQL?

How do you get max for each group in SQL? To find the maximum value of a column, use the MAX() aggregate function; it takes a column name or an expression to find the maximum value. In our example, the subquery returns the highest number in the column grade (subquery: SELECT MAX(grade) FROM student ).


1 Answers

Just order the rows by (descending) amount and take the top 5:

SELECT amount FROM mytable ORDER BY amount DESC LIMIT 5

Note that this will result in a full table scan unless you have an index on the amount column. This could affect performance if the number of rows in the table is very large (i.e. many thousands).

like image 175
Alnitak Avatar answered Dec 28 '22 21:12

Alnitak