MySQL: How to get Max Value for each group?

In this post, I am giving you a full demonstration on finding max value for each group of records in MySQL.

Today morning, I was working on one report and requirement is to find max value records for each particular group of data.

I know, You guys are thinking that this is a very simple solution using MAX() and GROUP BY clause.
But many of database developers have always questioned about: Why we cannot add any other columns in SELECT list which are not part of aggregation or the GROUP BY clause?

Below is a one solution, you can find MAX record without using GROUP BY clause and you can also add other columns in the SELECT list.

First, Create a table with sample data:

Find max salary values for each department:

The Result:

MySQL Max Value For Each Group


Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.

I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.

More from dbrnd.com

  • Find top N records for each group in MySQLAugust 30, 2015 Find top N records for each group in MySQL In this post, I am giving you a full demonstration on finding top N records for each group in MySQL.Today morning, I was working on one […]
  • Find duplicate records in MySQLSeptember 18, 2015 Find duplicate records in MySQL In this post, I demonstrate different way to find duplicate records in MySQL. I have found some alternative solution to find duplicate records […]
  • MySQL: How to import XML File Data into a Table?September 22, 2016 MySQL: How to import XML File Data into a Table? In this post, I am going to import XML file data into a table of MySQL Database.MySQL provides LOAD XML INFILE to import XML file data into […]

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz