user3691006
user3691006

Reputation: 185

SQL - Select name of the person with highest salary

I have a table called workers which includes a few persons by their names, their salary and their working station. The table looks something like the following:

|Name|Station|Salary|
|Kyle|1      |2200  |
|Lisa|2      |2250  |
|Mark|3      |1800  |
|Hans|4      |1350  |

This might sound like a very obvious beginner question but I cannot get it work. I would like to select the name of the person with the highest salary. Thank you for reading, and have a nice one.

Upvotes: 8

Views: 33804

Answers (5)

irufano
irufano

Reputation: 128

You must use subquery to get name and highest salary:

select Name, Salary from tb_name where salary=(select max(salary) from tb_name);

Upvotes: 0

flavien317
flavien317

Reputation: 48

Try to do:

SELECT TOP name 
FROM yourtable 
WHERE salary = (SELECT MAX(salary) FROM yourtable)

Upvotes: 0

Sid M
Sid M

Reputation: 4364

Try this

SELECT top 1 Name
FROM tableName
ORDER BY Salary DESC

Upvotes: 3

Lennart - Slava Ukraini
Lennart - Slava Ukraini

Reputation: 7181

You don't mention DBMS:

select name 
from table
order by salary desc
fetch first 1 rows only

If your DBMS support OLAP functions:

select name from (
    select name, row_number() over (order by salary desc) as rn 
    from table
) where rn = 1

Upvotes: 0

gh9
gh9

Reputation: 10703

Select name 
from table 
where salary = (select max(salary) from table)

I dont know if you want to include ties or not (if two people have the same salary and it is the max salary.

What this does is find the max salary and then uses that in the query to find all people with that salary. You will need to replace the word table with whatever your table name is.

Upvotes: 8

Related Questions