I would like to figure out how to select a table where the value is the ID of the person with the highest pay.
So if I had
Table=theJobs
JobID Pay
----------
12345 10
12346 12
12347 13
table=thePerson
Person JobID
--------------
Person1 1
Person2 2
Person3 3
table=hire(FKs)
JobID PersonID
----------------
12345 2
12347 1
12346 3
I’d like it to show the max payed person so it should show
Person1
I tried to use where for the a Max function but it seems to fail. I’m pretty sucky at these group functions. I guess I’m more asking how to use a group function as a constraint than anything. Since I had a similar issue a bit ago.
This solution will work in pretty much any database system:
If you are using a DBMS that supports ranking functions and common-table expressions such as SQL Server 2005 and later, then the problem is easier. This solution will show only one name and ignore ties:
This solution will show any that match the top pay and include ties: