I'm trying to give the most profitable employee in this lab a 9% raise.
I wrote this query to find the most profitable employee:
SELECT *
FROM (
SELECT SALESPERSONS.EmpID
, SALESPERSONS.Ename
, ((SUM(NVL(ORDERITEMS.Qty, 0) * NVL(INVENTORY.Price, 0)) - NVL(SALESPERSONS.Salary, 0))) AS Profit
FROM SALESPERSONS, ORDERS, ORDERITEMS, INVENTORY
WHERE SALESPERSONS.EmpID = ORDERS.EmpID (+)
AND ORDERS.OrderID = ORDERITEMS.OrderID (+)
AND ORDERITEMS.PartID = INVENTORY.PartID (+)
GROUP BY SALESPERSONS.EmpID
, SALESPERSONS.Ename
, SALESPERSONS.Salary
ORDER BY Profit DESC)
WHERE rownum = 1)
I've validated that that works (it's not a very big database so I was able to do this manually).
I then created an UPDATE statement to give this one employee a raise:
UPDATE SALESPERSONS
SET Salary = (
SELECT MAX(SALARY)+MAX(SALARY)*.09
FROM SALESPERSONS, (SELECT *
FROM (
SELECT SALESPERSONS.EmpID
, SALESPERSONS.Ename
, ((SUM(NVL(ORDERITEMS.Qty, 0) * NVL(INVENTORY.Price, 0)) - NVL(SALESPERSONS.Salary, 0))) AS Profit
FROM SALESPERSONS, ORDERS, ORDERITEMS, INVENTORY
WHERE SALESPERSONS.EmpID = ORDERS.EmpID (+)
AND ORDERS.OrderID = ORDERITEMS.OrderID (+)
AND ORDERITEMS.PartID = INVENTORY.PartID (+)
GROUP BY SALESPERSONS.EmpID
, SALESPERSONS.Ename
, SALESPERSONS.Salary
ORDER BY Profit DESC)
WHERE rownum = 1));
Which updates the salary for all eleven employees.
I'm trying to figure out what I'm doing wrong here. My top query only returns one result.