How can I find the difference of highest paid salary in each department.
DeptID EmpName Salary
Engg Sam 10000
Engg Smith 15000
HR Denis 20000
HR Archie 15000
HR Danny 30000
IT David 25000
IT Chrish 40000
IT John 35000
Result should be
DeptID Salary
Engg 5000
HR 10000
IT 5000
You could use a sub query:
SELECT
DeptID,
(MAX(Salary) - (SELECT
MAX(Salary)
FROM your_table
WHERE DeptID = yt.DeptID
AND Salary <
MAX(yt.Salary))
) sal_diff
FROM your_table yt
GROUP BY DeptID
ORDER BY DeptID
Find the 2nd max salary per deptId from the below query
-- Assuming a temp table named as #salary
select deptid, max(salary) as MaxSal_2nd from #salary
where salary not in (select max(salary) from #salary group by deptid)
group by deptid
Use that query with inner join to the main table and derive the difference with max sal per deptId as below. So, final query:
select s.deptid,max(salary)-Max(dt.MaxSal_2nd) from #salary s
inner join
(select deptid, max(salary) as MaxSal_2nd from #salary
where salary not in (select max(salary) from #salary group by deptid)
group by deptid)dt --Second Max Salary per deptId
on s.deptid=dt.deptid
group by s.deptid
Use the window functoins to get the Salary rank within department, then pivot and look for top 2 salaries and find the difference.
select
baseq.DeptID,
max(case when baseq.SalaryRank=1 then baseq.Salary else 0 end) - max
(case when baseq.SalaryRank=2 then baseq.Salary else 0 end)
from
(
select DeptID, EmpName, Salary, Rank() over (parition by DeptID order by
Salary Desc) as SalaryRank
from TestTable
) baseq
group by baseq.DeptID
I am a beginner at SQL. I am trying to write a query which "provides the Total salary drawn by all people for each department if the total salary is greater than 300,000".
I have written some of it, but can't figure it out completely.
USE EMP_DB_01;
SELECT DEPTNAME, SUM(SALARY) AS 'Total Salary'
FROM DEPT, EMP
WHERE (SALARY > 300000) AND (DEPT.DEPTNO = EMP.DEPTNO)
GROUP BY DEPTNAME
Table is here
enter image description here
You should use a join between the DEPT and EMP could be based on DEPT.DEPTNO = EMP.DEPTNO ..and for total salary is greater than 300,000 you should use having and not where
Having filter the result of aggregated result .. where filter the rows values
this return the dept and the the related Total Salary when the sum is > 300.00
USE EMP_DB_01;
SELECT DEPTNAME, SUM(SALARY) AS 'Total Salary'
FROM DEPT
INNER JOIN EMP ON DEPT.DEPTNO = EMP.DEPTNO
HAVING SUM(SALARY) > 300000
GROUP BY DEPTNAME
How to get the sum of distinct IDs and their respective salaries?
ID Salary
1 1000
2 2000
1 1000
2 2000
In above I want to get the out like this
Total salary
3000
I tried and made the output like this
ID Salary
1 1000
2 2000
select distinct(id), sum (salary) from employee group by id
Main issue is that I am not able to total after applying distinct to the ID.
Want to remove the duplicate entries of id and sum of there distinct ID?
Check this Query is,
Query
SELECT SUM (
DISTINCT salary
) FROM employee
Result
SUM(DISTINCT SALARY)
--------------------
3000
Check Demo SQLFiddle
You can do also by this way,
SELECT SUM(salary)
FROM (SELECT DISTINCT id, salary
FROM employee
GROUP BY id,salary
) AS emp
Check this Demo SQLfiddle
Try this 100% work
SELECT SUM(salary) as "Total salary"
FROM(SELECT DISTINCT id, salary FROM
employee GROUP BY id,salary ) as e
Try Like This
select sum(salary) from( select distinct id, salary from
employee) as t
Simplest way would be, remove duplicate records and write simple query. Second way is this query, in which I have taken MAX(Highest) salary for each employee in sub query, and then applied SUM() from calculating total salary.
SELECT SUM (sal) from
(
SELECT MAX (salary) as sal
FROM employee GROUP BY id
) as tbl
Try this:
It will work definitely
It will sum of salary with same id only.
Select SUM(salary) as total from employee group by id;
Suppose that you are given the following simple database table called Employee that has 2 columns named Employee ID and Salary:
Employee
Employee ID Salary
3 200
4 800
7 450
I wish to write a query select max(salary) as max_salary, 2nd_max_salary from employee
then it should return
max_salary 2nd_max_salary
800 450
i know how to find 2nd highest salary
SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee )
or to find the nth
SELECT FROM Employee Emp1 WHERE (N-1) = ( SELECT COUNT(DISTINCT(Emp2.Salary)) FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
but i am unable to figureout how to join these 2 results for the desired result
You can just run 2 queries as inner queries to return 2 columns:
select
(SELECT MAX(Salary) FROM Employee) maxsalary,
(SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee )) as [2nd_max_salary]
SQL Fiddle Demo
Try like this
SELECT (select max(Salary) from Employee) as MAXinmum),(max(salary) FROM Employee WHERE salary NOT IN (SELECT max(salary)) FROM Employee);
(Or)
Try this, n would be the nth item you would want to return
SELECT DISTINCT(Salary) FROM table ORDER BY Salary DESC LIMIT n,1
In your case
SELECT DISTINCT(column_name) FROM table_name ORDER BY column_name DESC limit 2,1;
Simplest way to fetch second max salary & nth salary
select
DISTINCT(salary)
from employee
order by salary desc
limit 1,1
Note:
limit 0,1 - Top max salary
limit 1,1 - Second max salary
limit 2,1 - Third max salary
limit 3,1 - Fourth max salary
The Best & Easiest solution:-
SELECT
max(salary)
FROM
salary
WHERE
salary < (
SELECT
max(salary)
FROM
salary
);
You can write 2 subqueries like this example
SELECT (select max(Salary) from Employee) as max_id,
(select Salary from Employee order by Salary desc limit 1,1) as max_2nd
Select Distinct sal From emp Order By sal Desc Limit 1,1;
It will take all Distinct sal. And Limit 1,1 means: leaves top one record and print 1 record.
$q="select * from info order by salary desc limit 1,0"; // display highest 2 salary
or
$q="select * from info order by salary desc limit 1,0"; // display 2nd highest salary
I think, It is the simplest way to find MAX and second MAX Salary.You may try this way.
SELECT MAX(Salary) FROM Employee; -- For Maximum Salary.
SELECT MAX(Salary) FROM Employee WHERE Salary < (SELECT MAX(Salary) FROM Employee); -- For Second Maximum Salary
i think that the simple way in oracle is this:
SELECT Salary FROM
(SELECT DISTINCT Salary FROM Employee ORDER BY Salary desc)
WHERE ROWNUM <= 2;
`select max(salary) as first, (select salary from employee order by salary desc limit 1, 1) as second from employee limit 1`
For max salary simply we can use max function, but second max salary we should use sub query. in sub query we can use where condition to check second max salary or simply we can use limit.
You can write SQL query in any of your favorite database e.g. MySQL, Microsoft SQL Server or Oracle. You can also use database specific feature e.g. TOP, LIMIT or ROW_NUMBER to write SQL query, but you must also provide a generic solution which should work on all database. In fact, there are several ways to find second highest salary and you must know a couple of them e.g. in MySQL without using the LIMIT keyword, in SQL Server without using TOP and in Oracle without using RANK and ROWNUM.
Generic SQL query:
SELECT
MAX(salary)
FROM
Employee
WHERE
Salary NOT IN (
SELECT
Max(Salary)
FROM
Employee
);
Another solution which uses sub query instead of NOT IN clause. It uses < operator.
SELECT
MAX(Salary)
FROM
Employee
WHERE
Salary < (
SELECT
Max(Salary)
FROM
Employee
);
This solution will give all employee name and salary who have second highest salary
SELECT name, salary
FROM employee
WHERE salary = (
SELECT
salary
FROM employee AS emp
ORDER BY salary DESC
LIMIT 1,1
);
Find Max salary of an employee
SELECT MAX(Salary) FROM Employee
Find Second Highest Salary
SELECT MAX(Salary) FROM Employee
Where Salary Not In (Select MAX(Salary) FROM Employee)
OR
SELECT MAX(Salary) FROM Employee
WHERE Salary <> (SELECT MAX(Salary) FROM Employee )
This will be the simplest code format :
select max(salary) as 'max_salary',
(select salary from employee order by salary desc limit 1,1) as
'2nd_max_salary'
from employee;
For finding the nth highest salary, syntax is :
select max(salary) as 'max_salary',
(select salary from employee order by salary desc limit n-1,1) as
'nth_max_salary'
from employee;
Not really a nice query but :
SELECT * from (
SELECT max(Salary) from Employee
) as a
LEFT OUTER JOIN
(SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee )) as b
ON 1=1
For unique salaries (i.e. first can't be same as second):
SELECT
MAX( s.salary ) AS max_salary,
(SELECT
MAX( salary )
FROM salaries
WHERE salary <> MAX( s.salary )
ORDER BY salary DESC
LIMIT 1
) AS 2nd_max_salary
FROM salaries s
And also because it's such an unnecessary way to go about solving this problem (Can anyone say 2 rows instead of 2 columns, LOL?)
Try
SELECT
SUBSTRING( GROUP_CONCAT( Salary ), 1 , LOCATE(",", GROUP_CONCAT( Salary ) ) -1 ) AS max_salary,
SUBSTRING( GROUP_CONCAT( Salary ), LOCATE(",", GROUP_CONCAT( Salary ) ) +1 ) AS second_max_salary
FROM
(
SELECT Salary FROM `Employee` ORDER BY Salary DESC LIMIT 0,2
) a
Demo here
For second highest salary, This one work for me:
SELECT salary
FROM employee
WHERE salary
NOT IN (
SELECT MAX( salary )
FROM employee
ORDER BY salary DESC
)
LIMIT 1
This is awesome Query to find the nth Maximum:
For example: -
You want to find salary 8th row Salary, Just Changed the indexed value to 8.
Suppose you have 100 rows with Salary. Now you want to find Max salary for 90th row. Just changed the Indexed Value to 90.
set #n:=0;
select * from (select *, (#n:=#n+1) as indexed from employee order by Salary desc)t where t.indexed = 1;
with Common table expression
With cte as (
SELECT
ROW_NUMBER() Over (Order By Salary Desc) RowNumber,
Max(Salary) Salary
FROM
Employee
Group By Salary
)
Select * from cte where RowNumber = 2
without nested query
select max(e.salary) as max_salary, max(e1.salary) as 2nd_max_salary
from employee as e
left join employee as e1 on e.salary != e1.salary
group by e.salary desc limit 1;
Here change n value according your requirement:
SELECT top 1 amount
FROM (
SELECT DISTINCT top n amount
FROM payment
ORDER BY amount DESC ) AS temp
ORDER BY amount
This should work same :
SELECT MAX(salary) max_salary,
(SELECT MAX(salary)
FROM Employee
WHERE salary NOT IN
(SELECT MAX(salary)
FROM Employee)) 2nd_max_salary
FROM Employee
If we want to find Employee that gets 3nd highest salary then execute this query
SELECT a.employeeid,
a.salary
FROM (SELECT employeeid,
salary,
Dense_rank()
OVER(
ORDER BY salary) AS n
FROM employee) AS a
WHERE n = 3
What do you want
This will work To find the nth maximum number
SELECT
TOP 1 * from (SELECT TOP nth_largest_no * FROM Products Order by price desc) ORDER BY price asc;
For Fifth Largest number
SELECT
TOP 1 * from (SELECT TOP 5 * FROM Products Order by price desc) ORDER BY price asc;
Here is another solution which uses sub query but instead of IN clause it uses < operator
SELECT MAX(Salary) From Employees WHERE Salary < ( SELECT Max(Salary) FROM Employees);
select * from emp where sal =(select max(sal) from emp where eno in(select eno from emp where sal <(select max(sal)from emp )));
try the above code ....
if you want the third max record then add another nested query "select max(sal)from emp" inside the bracket of the last query and give less than operator in front of it.
select * from
Employees where Sal >=
(SELECT
max(Sal)
FROM
Employees
WHERE
Sal < (
SELECT
max(Sal)
FROM
Employees;
));
Max Salary:
select max(salary) from tbl_employee <br><br>
Second Max Salary:
select max(salary) from tbl_employee where salary < ( select max(salary) from tbl_employee);
Try below Query, was working for me to find Nth highest number salary.
Just replace your number with nth_No
Select DISTINCT TOP 1 salary
from
(Select DISTINCT TOP *nth_No* salary
from Employee
ORDER BY Salary DESC)
Result
ORDER BY Salary