Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MAX aggregate between two tables

I have two tables, employer and position:

Employer
eID
eName

Position
eID
salary

I need to match my eID between the two tables, determine what the max salary is, and print only the eName. Any suggestions as to how I can do this? I have tried multiple ways, but nothing seems to work.

I am not sure where to put in the max(salary) function:

select eName
from employer, position
where employer.eID = position.eID
like image 697
user457666 Avatar asked May 01 '26 05:05

user457666


2 Answers

To get the name(s) of the people with the highest salary...

Using a JOIN:

SELECT e.name
  FROM EMPLOYER e
  JOIN POSITION x ON x.eid = e.eid
  JOIN (SELECT MAX(salary) AS max_salary
          FROM POSITION) y ON y.max_salary = x.salary

Using a subquery:

SELECT e.name
  FROM EMPLOYER e
  JOIN POSITION p ON p.eid = e.eid
 WHERE p.salary = (SELECT MAX(salary)
                     FROM POSITION)
like image 78
OMG Ponies Avatar answered May 03 '26 21:05

OMG Ponies


select e.ename,p.salary
from employer e ,position p
where p.salary=(select max(salary) from position)
and e.eid=p.eid
like image 29
Appuraj Avatar answered May 03 '26 21:05

Appuraj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!