SQL - Select name of the person with highest salary - sql

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.

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.

Try this
SELECT top 1 Name
FROM tableName
ORDER BY Salary DESC

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

Try to do:
SELECT TOP name
FROM yourtable
WHERE salary = (SELECT MAX(salary) FROM yourtable)

You must use subquery to get name and highest salary:
select Name, Salary from tb_name where salary=(select max(salary) from tb_name);

Related

Retrieve attribute with max function

How retrieve other attribute with max function like max salary with name.
to retrieve max salary along with their name please help anyone.
If my understanding of the question is correct, you can use something like this:
select name, salary from table where salary = (select max(salary) from table)
With SalaryOrder AS (
Select name, salary, Row_Number() Over(Order By salary Desc) RN
From table
)
Select /*the top clause is needed as described below*/ Top (1) *
From SalaryOrder Where RN = 1
this query may return more than a record when existing people with the same salary.
To solve this you can add more orders in CTE or use Top one to pick a random record.
RANK() should be an efficient way to get name where salary is MAX. Using sub-query will also get us the same result but it will take more time than RANK() in some cases.
Query:
SELECT name, salary
FROM
(
SELECT name, salary, RANK() over(ORDER BY salary DESC) AS rnk
FROM your_table
) AS a
WHERE rnk=1
Look at the db<>fiddle with time consumption. (The time may vary for different runs)

Query to find No. of employees earning maximum salary

I am trying to find the number of employees in a table that earn exactly the maximum salary of all the employees in the table called tblPerson.
Select Max(x.[No of Employees]) as Number, x.Salary as Salary
from
(
Select Count(Id) as [No of Employees], Salary
from tblPerson
Group by Salary
Having Salary = MAX(Salary)
)x
where x.[No of Employees]=3
Now I know this is a kind of long and complex way of doing it, but I was trying to do it using a derived table. But I am getting the error:
"Column 'x.Salary' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause"
My question is, why am I getting this particular error since the main query is a simple Select statement with a where clause. Isn't it??
Mainly, aggregate functions work only with other aggregate functions or grouped by columns.
Why? Because an aggregate function needs to know the set of values to do calculation with.
In you case, the max() will want to use all the data available for the calculation and display a single result (single row) and the other column will want to be displayed row by row. So there's a conflict.
Thank you all. Every answer helped me. However, I think I found a pretty simple way to do it:
Select top 1 Count(Id) as [No of Employees], salary
from tblPerson
Group by Salary
Order by [No of Employees] DESC
select count(*) from tblPerson where salary=(select max(salary) from tblPerson)
You get the error because 'max' is an aggregation, while you have nothing to aggregate the number by.
Select Max(x.[No of Employees]) as Number, x.Salary as Salary
from
(
Select Count(Id) as [No of Employees], Salary
from tblPerson
Group by Salary
Having Salary = MAX(Salary)
)x
---------
Group by Salary -- all other items in your select statement
---------
where x.[No of Employees]=3
however, you can also use a temporary table or variable to find the persons.
To solve this via a variable, you could do the following
declare #maxSalary Decimal
set #maxSalary = (Select max(salary) from tblperson) --insert the max value into a variable
Then either aggregate the persons (or do some other logic):
Select ID from tblperson where salary = #maxSalary
The reason for not using a group by is that using a variable is more efficient, as you search the table instead of aggregating over it.
Create a CTE (RESULT), and using DENSE_RANK function, get the highest salary, together with the EmployeeID's.
The first row of the RESULT table will give the highest salary.
Using the aggregate function COUNT, get the number of Employees with the highest Salary.
with RESULT (EmployeeID, Salary, DenseRank) as
(select EmployeeID, Salary,
DENSE_RANK() over (ORDER BY SALARY DESC) AS DenseRank
from Employee)
select TOP 1 Salary,
(select COUNT(EmployeeID)
from Employee
where Salary = (select TOP 1 Salary)
from RESULT
where DenseRank = 1)
)
from RESULT
where DenseRank = 1;

Select records with maximum value

I have a table that is called: customers.
I'm trying to get the name and the salary of the people who have the maximum salary.
So I have tried this:
SELECT name, salary AS MaxSalary
FROM CUSTOMERS
GROUP BY salary
HAVING salary = max(salary)
Unfortunately, I got this error:
Column 'CUSTOMERS.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I know I should add the name column to the group by clause, but I get all the records of the table.
I know that I can do it by:
SELECT name, salary
FROM CUSTOMERS
WHERE salary = (SELECT MAX(salary) FROM CUSTOMERS)
But I want to achieve it by group by and having clauses.
This requirement isn't really suited for a group by and having solution. The easiest way to do so, assuming you're using a modern-insh version of MS SQL Server, is to use the rank window function:
SELECT name, salary
FROM (SELECT name, salary, RANK() OVER (ORDER BY salary DESC) rk
FROM customers) c
WHERE rk = 1
Mureinik's answer is good with rank, but if you didn't want a windowed function for whatever reason, you can just use a CTE or a subquery.
with mxs as (
select
max(salary) max_salary
from
customers
)
select
name
,salary
from
customers cst
join mxs on mxs.max_salary = cst.salary
There was no need to use group by and having clause there, you know. But if you want to use them then query should be
SELECT name, salary
FROM CUSTOMERS
GROUP BY salary
having salary = (select max(salary) from CUSTOMERS)

Retrieve 3rd MAX salary in Hive

I'm a novice. I have the following Employee table.
ID Name Country Salary ManagerID
I retrieved the 3rd max salary using the following.
select name , salary From (
select name, salary from
employee sort by salary desc limit 3)
result sort by salary limit 1;
How to do the same to display 3rd max salary for each country? can we use OVER (PARTITION BY country)? I tried looking in the languageManual Windowing and Analytics but I'm finding it difficult to understand. Please help!
You're definitely on the right track with windowing functions. row_number() is a good function to use here.
select name, salary
from (
select name
, salary
, row_number() over (partition by country order by salary desc) idx
from employee ) x
where idx = 3
when you order by salary, make sure that it is a numerical type, or it will not be sorted correctly.

SQL MAX() funct

Let's say I have a list of workers in a table Workers(ID, Name, Salary).
If I want to see the name of a guy with a highest salary, I would do something like:
SELECT Name
FROM
(
SELECT Name, MAX(Salary)
FROM Workers
) as T
I was just wondering if I could do it using only one SELECT Query? I'm sorry if it's a dumb question, but I'm completely new to SQL.
more than 1 name with salary = max(salary)
SELECT top (1) with ties Name, Salary
FROM Workers
ORDER BY Salary DESC
Order by with a limit should work
SELECT Name, Salary
FROM Workers
ORDER BY Salary DESC
LIMIT 1
If there are multiple people tied for the highest salary, and you want them all:
SELECT Name, Salary
FROM Workers
WHERE Salary = (select max(Salary) from Workers)
This will select all names with the max salary even if there are ties
select name from
workers w1
left join workers w2 on w2.salary > w1.salary
group by name
having count(w2.salary) = 0