[SQL][ORACLE] Greater-than operation on grouped data

Hello, I'm attempting to teach myself SQL from the book "Essence of Databases" by F.D. Rolland. I've stumbled across an exercise which has me tearing my hair out a little.

Given a table STAFF
with fields StaffID, Sname, Job, Sal, BranchID

Attempting to identify all staff whose Salary (SAL) is greater than the AVG(SAL) of their respective Branch (identified by BranchID)

So, finding the average salary for all staff at each branch is simple enough -

SELECT AVG(sal) FROM staff GROUP BY branchid
/

So I tried something along these lines

SELECT sname Surname, sal Salary FROM staff
WHERE sal > ( SELECT AVG(sal) FROM staff
GROUP BY branchid )
/

However, this is an error, because the > operator cannot be used in a situation where a nested subquery returns more than one tuple

So. knowing that there were only 3 Branches, identified by 10, 20 and 30, I attempted the following

SELECT sname Surname, sal Salary FROM staff
WHERE SAL > ( SELECT AVG(sal) FROM staff
WHERE BRANCHID = 10
GROUP BY branchid )
OR SAL > ( SELECT AVG(sal) FROM staff
WHERE branchid = 20
GROUP BY branchid )
OR SAL > ( SELECT AVG(sal) FROM staff
WHERE branchid = 30
GROUP BY branchid )
ORDER BY branchid
/

Of course, this looks up all staff whose salary is merely less than the lowest average sal.

Somehow I need to be able to use the > operator on grouped data, or I'm missing something (Such as, how to get a nested query to run multiple times with a different branchid, and a different avg salary for that particular branch).

Has anyone got any other ideas? Thanks for reading.
[1807 byte] By [Bench_] at [2007-11-20 11:34:07]
# 1 Re: [SQL][ORACLE] Greater-than operation on grouped data
The exercise in the book wants you to only find staff members that have a salary higher than the average salary of their branch. You're trying to find staff members that have a salary higher than the average salary in all branches. While it's possible to make a query that does what you want, I think you'll find that the following query gives you the expected results:
SELECT sname Surname, sal Salary
FROM staff s
WHERE sal > (SELECT AVG(sal) FROM staff WHERE branchid = s.branchid GROUP BY branchid)
andreasblixt at 2007-11-9 13:45:37 >
# 2 Re: [SQL][ORACLE] Greater-than operation on grouped data
Andreasblixt,

Thanks for your help - that's exactly what I needed! I've never seen a self join between an inner and outer query before (That is a join, right?). I guess I need to read up on these a little more.
Bench_ at 2007-11-9 13:46:38 >
# 3 Re: [SQL][ORACLE] Greater-than operation on grouped data
Actually it's just an ordinary sub-query that gets a value from the main query (s.branchid). As long as a name doesn't exist in the sub-query it will look for it in the "parent" query/queries.
andreasblixt at 2007-11-9 13:47:37 >