Reputation: 3
here is the situation. I have two tables, one is table Tbl_employ
, second is tbl_details
.
Tbl_employ
----------
id | name
1 | Ravi
2 | ram
3 | sham
4 | john
Tbl_details
-----------
id | salary | emp_id
1 | 500 | 1
2 | 200 | 2
3 | 400 | 3
4 | 501 | 4
I want to return the name of the employee
who has top salary in tbl_detail
.
What will be the join query for this condition?
Please suggest. Thanks in advance.
Upvotes: 0
Views: 166
Reputation: 2389
I appreciate the answer of @Max Vernon. You can also do it by another way. Please try this
select t.name from (
select Distinct top 1 salary ,name
from Tbl_employ as E
left outer join Tbl_details as D on D.empid=E.id
order by salary desc
) as t
you can check it here SQL Fiddle
Upvotes: 0
Reputation: 3472
Perhaps:
SELECT TOP(1) name
FROM Tbl_employ e INNER JOIN Tbl_details d ON e.id = d.emp_id
ORDER BY d.salary DESC;
Essentially, this joins the two tables on the key fields (id
and emp_id
), returning only a single result (TOP(1)
) that is the maximum salary
row (ORDER BY d.salary DESC
).
Upvotes: 2