Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select get entire row corresponding to max in MySQL Group

Tags:

mysql

When I use Max to find the maximum value of a field in a particular MySQL Group after using GROUP BY, is it possible to get the entire row which contains the maximum value?

like image 640
Brian Avatar asked Apr 06 '10 11:04

Brian


People also ask

How do you find the maximum value of GROUP BY?

MySQL MAX() function with GROUP BY retrieves maximum value of an expression which has undergone a grouping operation (usually based upon one column or a list of comma-separated columns).

Can we use select * with GROUP BY?

Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause. The original idea was to create the table in beginning of the query, so the (SELECT * FROM #TBL) could be used on the query itself, instead of defining the names on each GROUP BY.

How do I find the maximum value of a row 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

I wonder if this would work:

select * from table where id in (select distinct max(id) from table group by name)
like image 121
Riho Avatar answered Nov 02 '22 04:11

Riho