How to get any highest salary from SQL Server table - sql-server

How to get 7th highest salary from table in SQL Server?
I tried this:
SELECT max(salary)
FROM emptable
WHERE salary < (SELECT max(salary)
FROM emptable);

SELECT salary
FROM (
SELECT salary
, DENSE_RANK()OVER(ORDER BY salary) DR
FROM emptable
) T
WHERE T.DR = 7

From SQL Server 2012 and onwards:
SELECT DISTINCT salary
FROM emptable
ORDER BY salary
OFFSET 6 ROWS
FETCH NEXT 1 ROWS ONLY;

You can as the below:
SELECT MAX(salary)
FROM (SELECT DISTINCT TOP 7 salary FROM emptable ORDER BY salary) A

Related

SQL Query change row values

Consider i have 2 employers(Google,Yahoo) and google is having 3 employee's and yahoo is 2 employee
My query should add
1000 to employee1 for google company
2000 to employee2 for google company
3000 to employee3 for google company
1000 to employee1 for Yahoo company
2000 to employee2 for Yahoo company
For each set of employees it should adding salary starting from 1000
You can use ROW_NUMBER() to calculate the new Salary:
SELECT Company,
Emp,
ROW_NUMBER() OVER(PARTITION BY Company ORDER BY Emp) * 1000 + Salary As NewSalary
FROM YourTable
If you want to actually update the salary, one way is to use that query as a CTE:
;WITH CTE AS (
SELECT Company,
Emp,
ROW_NUMBER() OVER(PARTITION BY Company ORDER BY Emp) * 1000 + Salary As NewSalary
FROM YourTable
)
UPDATE T
SET Salary = NewSalary
FROM YourTable T
INNER JOIN CTE C ON(T.Company = C.Company AND T.Emp = C.Emp)

Third highest salary department wise without using rank or other functions

I want to find third highest salary department wise without using any functions such as rank or row_number or dense_rank in sql
My logic
select max(salary),deptno from emp
where salary not in
(select max(salary) from emp where salary not in
(select max(salary) from emp group by deptno)group by deptno)
group by deptno
This is my query.syntax is correct but it is not giving wright answer/result
This should do the job
select salary, deptno from emp e where 2 =
(select count(distinct salary) from emp where
salary > e.salary and deptno = e.depto)
Using MSSQL,
SELECT MAX(salary) as '3rdHighest', deptno
FROM EMP n WHERE 2 =
(SELECT COUNT(DISTINCT salary) FROM NewTbl
WHERE salary > n. salary and deptno = n.deptno)
GROUP BY deptno

T-Sql query for the situation - Need a departmentID with max number of employees?

let's say employee table has employee details and deptId of the employee.
to get the number of employees in each deptid,
select deptId, COUNT(*) from employee group by deptId;
question is: to get the deptId having max number of employees of the above result set,
select Top 1 deptId, COUNT(*) from employee group by deptId order by 2 desc
(2-ref to second column in the query list) - will do.. but
Is there anyway to avoid ordering this set? or better way of writing this sql,
thanks
If you just want the MAX number of employees within a department, you can do this:
SELECT TOP 1 DepartmentID,
COUNT(EmployeeID)
FROM EmployeeTable
GROUP BY DepartmentID
ORDER BY COUNT(EmployeeID) DESC
Without any ordering, it is hard, but try
Select deptId, cnt
From (Select deptId, count(*) cnt
from employee
Group By deptId) Z
Where cnt = (Select Max(cnt)
From (Select deptId, count(*) cnt
From employee
Group By deptId) ZZ)

How to retrieve name of employee having salary greater than the average salary of their respective department

I have a table named "tblEmployee". Columns in this table are "EmpId","Department","Salary".
Rows in this table are as follows(e1,sales,10000),(e2,hr,20000),(e3,sales,30000),(e4,production,40000),(e5,hr,50000).
I want to retrieve name of the employee whose salary is greater than the average salary of their respective department.I want solution for sql server.
Thanks in advance.
select emp.EmpId, emp.Department
from tblEmployee emp
where emp.Salary > ( select
avg(emp2.Salary)
from tblEmployee emp2
where emp2.Department = emp.Department
)
Avoiding an aggregate by using an the OVER clause to generate in-line AVG
;WITH cte as
(
select
EmpId, Department, Salary,
AVG(Salary) OVER (PARTITION BY Department) AS DeptAvgSal
FROM
tblEmployee
)
SELECT
EmpId, Department, Salary
FROM
cte
WHERE
Salary > DeptAvgSal
This works for me:
select e.eid,e.did,e.ename,e.sal,t.avg_sal dpt_avg_sal from emp e inner join
(select emp.did, AVG(sal) avg_sal from emp inner join dpt on emp.did = dpt.did
group by emp.did) t
on e.did = t.did where e.sal > avg_sal
Where #department is the passed-in parameter denoting which department...
select * from tblEmployee where Salary >
(select AVG(Salary) from tblEmployee where Department = #department)
and Department = #department
order by salary desc

How to get the highest paid employee row

A table consists of employee name, address, phone, department, salary:
How to get the highest paid employee row from each department?
I tried with
select dept, max(salary) from employee group by dept
but it gives only two columns. But I want to select an entire row. How to do it?
Alternatively how to add more columns to the result?
(I am using SQL Server 2008)
You simply need to join the query you currently have back to the employee table to get the full employee information.
select e.*
from employee e
inner join (select dept, max(salary) ms from employee group by dept) m
on e.dept = m.dept and e.salary = m.ms
SELECT name,
address,
phone,
department,
salary,
dept
FROM (SELECT name,
address,
phone,
department,
salary,
dept,
row_number() OVER(PARTITION BY dept ORDER BY salary DESC) AS rn
FROM employee) AS e
WHERE e.rn = 1
Using row_number() will give you one row if there is a tie for the highest salary. If you want all the highest salaries for each department you should use rank() instead.
SELECT name,
address,
phone,
department,
salary,
dept
FROM (SELECT name,
address,
phone,
department,
salary,
dept,
rank() OVER(PARTITION BY dept ORDER BY salary DESC) AS rn
FROM employee) AS e
WHERE e.rn = 1
Something like this?
select * from employee where salary = (select max(salary) from employee)
with cte as (
select *, rank() over (partition by dept order by salary desc) as [r]
from employees
)
select * from cte where [r] = 1;
select * from employee
where salary in
(select max(salary) from employee group by dept);
If situation like you have both employee details and department in same table and you have to find the highest paid employee from each department like given below
EmployeeId EmployeeName Department Salary
1 Neeraj Dot Net 45000
2 Ankit Java 5000
3 Akshay Java 6000
4 Ramesh Dot Net 7600
5 Vikas Java 4000
7 Neha Php 8500
8 Shivika Php 4500
9 Tarun Dot Net 9500
Then you can solve it by using below solutions
Solution - 1
SELECT t.EmployeeName,t.Department,t.Salary
FROM(SELECT MAX(Salary) AS TotalSalary,Department FROM Employee GROUP BY Department)
AS TempNew Inner Join Employee t ON TempNew.Department=t.Department
and TempNew.TotalSalary=t.Salary
ORDER BY t.Department ASC
Solution -2
;WITH EmployeeDetails AS (
SELECT EmployeeName, Department, DENSE_RANK() OVER(PARTITION BY Department
ORDER BY Salary DESC) AS SalaryRank, Salary FROM Employee )
SELECT EmployeeName, Department, Salary FROM EmployeeDetails WHERE SalaryRank=1
OUTPUT
EmployeeName Department Salary
Neeraj Dot Net 45000
Akshay Java 6000
Neha Php 8500

Resources