Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to use group by in update form ? in SQL

first i search name who teaches more than 2 courses

select i.name, count(*) teaches from instructor i
inner join teaches t on i.id = t.id
group by name
having count(*)>=2;

and it’s working so i want to raise salary with these results

update i set salary = salary*1.05
from instructor i
inner join teaches t on i.id = t.id
group by name having count(*)>=2;

but error in SQL syntax; how to fix it ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I would use an update join here:

UPDATE instructor t1
INNER JOIN
(
    SELECT i.id
    FROM instructor i
    INNER JOIN teaches t ON i.id = t.id
    GROUP BY i.id
    HAVING COUNT(*) > 1
) t2
    ON t2.id = t1.id
SET
    salary = 1.05 * salary;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading