SQL Update query with group by clause

MysqlSql

Mysql Problem Overview


Name         type       Age
-------------------------------
Vijay          1        23
Kumar          2        26
Anand          3        29
Raju           2        23
Babu           1        21
Muthu          3        27
--------------------------------------

Write a query to update the name of maximum age person in each type into 'HIGH'.

And also please tell me, why the following query is not working

update table1 set name='HIGH' having age = max(age) group by type;

Mysql Solutions


Solution 1 - Mysql

I have changed the script from Derek and it works for me now:

UPDATE table1 AS t 
INNER JOIN 
(SELECT type,max(age) mage FROM table1 GROUP BY type) t1 
ON t.type = t1.type AND t.age = t1.mage 
SET name='HIGH'

Solution 2 - Mysql

You can't use group by directly in an update statement. It'll have to look more like this:

update t
set name='HIGH'
from table1 t
inner join (select type,max(age) mage from table1 group by type) t1
on t.type = t1.type and t.age = t1.mage;

Solution 3 - Mysql

Since I looked-up this response and found it a little bit confusing to read, I experimented to confirm that the following query does work, confirming Svetlana's highly-upvoted original post:

update archives_forum f
inner join ( select forum_id, 
    min(earliest_post) as earliest, 
    max(earliest_post) as latest 
  from archives_topic 
    group by forum_id 
  ) t 
  on (t.forum_id = f.id)
set f.earliest_post = t.earliest, f.latest_post = t.latest;

Now you know ... and so do I.

Solution 4 - Mysql

You can use a semi-join:

SQL> UPDATE table1 t_outer
  2     SET NAME = 'HIGH'
  3   WHERE age >= ALL (SELECT age
  4                       FROM table1 t_inner
  5                      WHERE t_inner.type = t_outer.type);
 
3 rows updated
 
SQL> select * from table1;
 
NAME             TYPE AGE
---------- ---------- ----------
HIGH                1 23
HIGH                2 26
HIGH                3 29
Raju                2 23
Babu                1 21
Muthu               3 27
 
6 rows selected

Your query won't work because you can't compare an aggregate and a column value directly in a group by query. Furthermore you can't update an aggregate.

Solution 5 - Mysql

try this

update table1 set name='HIGH' having age in(select max(age) from table1 group by type);

Solution 6 - Mysql

you can use the below code.

Update table1#
inner Join (Select max(age) as age, type from Table1 group by Table1) t ON table.age = t.age#
Set name = 'High'#

Solution 7 - Mysql

update table1 set Name='HIGH' where Age in(select max(Age) from table1)

Solution 8 - Mysql

UPDATE table1 SET name = 'HIGH' WHERE age IN (SELECT MAX(age) FROM table1 GROUP BY name)

Solution 9 - Mysql

You cannot use a GroupBy clause for an Update Statement. You will have to use a sub query during that time

Update table1
Set name = 'High'
From table1 
Join (Select max(age), type from Table1 group by Table1) t ON table1.age = t.age

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNageswaranView Question on Stackoverflow
Solution 1 - MysqlSvetlanaView Answer on Stackoverflow
Solution 2 - MysqlDerek KrommView Answer on Stackoverflow
Solution 3 - MysqlMike RobinsonView Answer on Stackoverflow
Solution 4 - MysqlVincent MalgratView Answer on Stackoverflow
Solution 5 - MysqlPratikView Answer on Stackoverflow
Solution 6 - Mysqluser3339750View Answer on Stackoverflow
Solution 7 - MysqlrajView Answer on Stackoverflow
Solution 8 - MysqlMuhammad Aftab JavedView Answer on Stackoverflow
Solution 9 - MysqlVinayView Answer on Stackoverflow