I've one database and I try to find the solution for my one query.
I want to find the 5th person name who earn a max salary in my database. so can I do
For example this is my execute query. you can see 7 people name but i want to see only 5th person name like only 'Mahesh"
my table is like that
FirstName varchar (45),
LastName varchar (45),
Birthdate datetime,
position varchar (35),
DOJ datetime,
DeptID int,
Salary decimal (10,2)
and this is my execute query. you can see 7 people name but i want to see only 5th person name like only 'Mahesh"
Salary Name
90000.00 Amita
90000.00 Carla
89500.00 Sarah
89000.00 Gunjan
85000.00 **Mahesh**
96000.00 John
86000.00 Charles
So my question is
I want to find the only one person name and salary from my data who earn 5th max salary.
Assuming you want the user with the 5th highest salary, you could use an embedded query.
SELECT TOP 1 *
FROM (SELECT TOP 5 *
FROM YourTable
ORDER BY Salary DESC) tmp
ORDER BY tmp.Salary
Or if you want to include ties.
SELECT *
FROM YourTable
WHERE Salary = (SELECT TOP 1 Salary
FROM (SELECT TOP 5 Salary
FROM YourTable
ORDER BY Salary DESC) tmp
ORDER BY tmp.Salary)
Related
I have this employees table.
I need to write an SQL query that will bring me the max salary of an employee and the second highest salary of an employee by city id (id is linked to a table with cities. there are 5 cities.)
My query looks like this:
select MAX([dbo.Employees].Salary) as Salary from [dbo.Employees]
where [dbo.Employees].Salary not in(select MAX([dbo.Employees].Salary) from [dbo.Employees])
UNION select MAX([dbo.Employees].Salary) from [dbo.Employees] group by [dbo.Employees].Id
I try to bring the highest and exclude the highest but it suppose to bring overall 7 values but it brings only 5. (because there are 5 cities but the 5th is not in use so there are 4 cities and 2 employees in each city except 1 that has only 1 employee so the query suppose to bring me 2 pairs of employees per city = 6, and one of the cities has only 1 employee so it will bring the only possible value. overall 7. )
another problem is that I don't know how to make in bring 2 columns - one for the id of the cities and the second for the salaries themselves because it tells me that something about the group by doesn't work.
You can use ROW_NUMBER() to create a sequence for each city, with the highest salary getting value 1, second highest getting value 2, etc. Then you just need a WHERE clause.
WITH
ranked AS
(
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY Salary DESC) AS city_salary_id
FROM
dbo.Employees
)
SELECT
*
FROM
ranked
WHERE
city_salary_id IN (1, 2)
If multiple people are tied with the same salary, it will arbitrarily pick the order for you, and always return (at most) 2 employees per city.
Adding a column to the ORDER BY lets you be more specific about how to deal with ties, such as ORDER BY Salary DESC, id ASC will prioritise the highest id in the even of a tie.
Changing to RANK() will give tied Salaries the same rank, and so will return more than two employees if there are ties.
I am trying to get correct result but I can not find right way to get.
I have this table:
I would like to get result like this one
This is my code:
SELECT employee_ID, first_name, manager_id ,
(SELECT first_name from employees where MANAGER_ID= employee_id)
from employees
I got only first 3 columns and fourth one is empty because its wrong select
Do self join to the same table
select emp1.employee_ID, emp1.first_name, emp1.manager_id,emp2.first_name as manager_name from employees emp1
left join employees emp2
on emp1.manager_id=emp2.employee_id;
Sorry my bad explanation, I have 5 columns, first one in not in the picture because its only ID of row, my first row is Employee id, second one is first_name, third one is manager id and fourth one should be first name of manager. Manager name is that when manager id is equal to employee id, so e.g. employee id = 206, first name is William, manager id is 205. and name should be Shelley because Shelley's employee ID is 205
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.
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);
I have a table named employee_salary, with three columns: empsal_id, empsal_name and empsal_sal. The data is as follows:
empsal_id empsal_name empsal_sal
1 dilip 14000
2 santosh 20000
3 amit 32000
4 dilip 22000
5 amit 38000
6 santosh 25000
7 dilip 30000
The empsal_id is an Identity column with a Seed and an Increment of 1, and is the Primary Key. I want to return the name and current salary of each employee. Salary can decrease as well as increase, so current does not necessarily mean highest.
So I need the following output:
empname emp_sal
dilip 30000
amit 38000
santosh 25000
I am using Microsoft SQL Server, and I have to do this in a single query.
This query will return each employee, along with their highest salary:
SELECT
empsal_name, MAX(empsal_sal)
FROM
employee
GROUP BY
empsal_name
This query will return each employee, along with their current salary (i.e. the salary with the highest empsal_id:
SELECT
empsal_name, empsal_sal
FROM
employee e1
WHERE
empsal_id =
(SELECT MAX(empsal_id)
FROM employee e2
WHERE e1.empsal_name=e2.empsal_name)
Personally, I think you would be better off using an effective date column (e.g. empsal_effectivedate) to determine which record is the most current, so this query will return each employee, along with their current salary (i.e. the salary with the most recent empsal_effectivedate), assuming there is an empsal_effectivedate field:
SELECT
empsal_name, empsal_sal
FROM
employee e1
WHERE
empsal_effectivedate =
(SELECT MAX(empsal_effectivedate)
FROM employee e2
WHERE e1.empsal_name=e2.empsal_name)
Assuming there is also an ID in the table and also assuming that the larger this ID is the most recent the salary is for that employee you can do
SELECT DISTINCT
e.empname,
(SELECT TOP 1
emp_sal
FROM
employee s
WHERE
s.empname = e.empname
ORDER BY
recid DESC) AS emp_sal
FROM
employee e
(also assuming that empname is unique for an employee)
because of the many assumptions though : you should probably post all the columns of the table and what they mean ..
SELECT empname, MAX(emp_sal)
FROM employee
GROUP BY empname
UPDATED:
SELECT DISTINCT EmpA.empname,
EmpA.emp_sal
FROM Employee AS EmpA
INNER JOIN ( SELECT EmpName, MAX(recID) AS recid
FROM Employee
GROUP BY EmpName
) AS EmpB ON EmpA.recid = EmpB.recid;
You need a date field to determine the latest inserted row . In case if the table is linked to some other table which has date column in it .Then its pretty easy to fetch the current data .
For Example
Employee Table
{
EmpName varchar(30) PK,
EmpAddress varchar(255) ,
Company varchar(30),
CurrentTimeStamp Datetime
}
Salary Table
{
EmpName varchar(30) FK,
EmpSalary int
}
To get the Latest record use the CTE function
With LatestSal(EmpName ,EmpSalary)
AS
(
Select row_number() over (PARTITION BY b.[EmpName], order by CurrentTimestamp DESC) as seq
b.EmpName,b.EmpSalary
From Employee as a,
Salary as b
on a.[EmpName]=b.[EmpName]
)
Select EmpName,EmpSalary
from LatestSal
where seq=1