views:

28

answers:

2

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
A: 

Join the tables, sort, and get the first one:

select top 1 e.eName, p.salary
from Employer e
inner join Position p on p.eID = e.eID
order by p.salary desc

(This returns the salary also, but you can of course remove it if you really don't want it.)

Guffa
+2  A: 

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)
OMG Ponies
Thank you, this helped so much!! I am very new to SQL (just started learning it this week) and I forgot about renaming tables to create new ones.