How to increase your salary by 0.1% of those with lower salary than your department's AVG
I was able to take the average salary by department:
SELECT cod_departamento, AVG(VAL_SALARIO)
FROM FUNCIONARIO
GROUP BY COD_DEPARTAMENTO;
To increase the salary of FUNCIONÁRIOS
can be done as follows:
1 - Let's see which records will change:
Accordingtotheirdepartment,1,2and3arerequiredrespectivelylessthan~3603,2392and~1973asa%oftheirwagestoincreaseby10%.
2nd-NowweneedtodoVAL_SALARIO
intheregistry,thiscanbedonethisway:
UPDATEFUNCIONARIOSFUNCSETFUNC.VAL_SALARIO=FUNC.VAL_SALARIO*1.10WHEREFUNC.VAL_SALARIO<(SELECTAVG(FUNCIO.VAL_SALARIO)FROMFUNCIONARIOSFUNCIOWHEREFUNCIO.COD_DEPARTAMENTO=FUNC.COD_DEPARTAMENTOGROUPBYFUNCIO.COD_DEPARTAMENTO);
NoticethatthesecrethereistheUPDATE
wegivetheALIAS
whicharethesame,onewecallTABELAS
andanotherwecallFUNC
todifferentiateonefromtheother,FUNCIO
willbecomparedintheFUNC
table.
So,weupdated4records.
What are the 4 previously spelled out: FUNCIO
.