Pay Query SQL Query

I came up with the following query, which is not so difficult to solve, but still I think there might be a better approach for this. Any sentences? Thanks in advance.

I have the following table

tb_Salary

Salary EmpID DeptID
---------------------
1 1 20000
2 1 20000
3 2 30000
4 2 800
5 & ​​nbsp; 2,200

I want to increase the salary of rupees employees. 1000, only when the average salary in the department is more than 35000.

Is this possible using a single update request?

I did it as follows. But it seems that this is not a reasonable decision.

tb_Salary t1
SET t1.Salary = t1.Salary + 1000
35000 < ( AVG (t2.Salary) tb_Salary t2 WHERE t2.DeptID = t1.DeptID)

+3
2

- .

+2
UPDATE  tb_salary
    SET tb_salary.salary = tb_salary + 1000
FROM    tb_salary
        INNER JOIN
        (SELECT   avg(salary) AS avg_salary,
                  deptID
         FROM     tb_salary
         GROUP BY tb_salary.deptID
         HAVING   avg(salary) > 10000) AS salary_increase
        ON salary_increase.deptID = tb_salary.deptID;    

, . "", .

+3

Source: https://habr.com/ru/post/1760723/


All Articles