Related
Input: Salary: 200, 300, 100 with respected id 1, 2, 3
Output: 200 [Here 200 is the closest value of the 300]
I have tried in this way:
select
Case
When (salary < (select max(salary) from Employee))
then salary
Else
NULL
end as SecondHighestSalary
from Employee
where salary < (select max(salary) from Employee)
order by salary desc limit 1;
After running this query it returns the output as expected for some conditions. Like if input is salary: 100 with the respected id 1 then my query returns only empty value But it should return the null value.
If you can complete the query or suggest how to complete then you are most welcome.
General solution:
SELECT salary
FROM employee
WHERE salary < ( SELECT MAX(salary)
FROM employee )
ORDER BY salary DESC LIMIT 1
Solution appicable to MySQL 8+:
WITH
cte AS (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) drnk
FROM employee
)
SELECT DISTINCT salary
FROM cte
WHERE drnk = 2
You could use this query this way.
SELECT salary FROM Employee ORDER BY salary DESC LIMIT 1,1
This query will fetch second most big salary.
This would work!
select salary from Employee order by salary desc limit 1 offset 1;
This should retrieve the second highest salary. It orders the salaries in descinding order, then neglect the first result by offset 1, then limit 1 limits the result to one record.
I'm trying to do this Leet Code Problem:
Write a SQL query to get the second highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
I'm trying to give this solution:
SELECT
CASE WHEN Salary = ''
THEN NULL
ELSE Salary END SecondHighestSalary
FROM
Employee
ORDER BY
SecondHighestSalary
LIMIT 1,1;
When there is a second salary, it works fine and returns the output. However, when there is no second salary and there's only one salary only an empty string is returned. I'm trying to return NULL, however, it doesn't return NULL like what I wrote in my query. How can I fix this?
Your CASE expression is testing the salary in each row of the table, not the one selected by the LIMIT clause. Ordering is done after generating the values in the SELECT list, since you can order by those calculated values.
Since none of the salaries are empty strings, the condition in your CASE will never be true, so it always returns the Salary value. As a result, your query is equivalent to
SELECT Salary AS SecondHighestSalary
FROM Employee
ORDER BY SecondHighestSalary
LIMIT 1, 1
Other things:
You need to use DESC to get the highest salary at the beginning. So even if your method worked, it would find the second lowest salary.
Your method doesn't handle the case where multiple employees are tied for the highest salary. LIMIT 1, 1 will return the second row, which will be one of the tied employees.
You can solve the second problem using a subquery that removes duplicates:
SELECT DISTINCT Salary
FROM Employee
So a final query could be:
SELECT IF(COUNT(*) > 0, MAX(Salary), NULL) AS SecondHighestSalary
FROM (
SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 1, 1
) AS x
You can use this solution, where if there have no second highest salary will return NULL. Otherwise will return second highest salary
SELECT
IFNULL(MIN(Salary) , 'NULL') as SecondHighestSalary
FROM
Employee
WHERE salary > (SELECT MIN(salary)
FROM Employee)
Or if you want DB default null then just remove IFNULL condition
SELECT
MIN(Salary) as SecondHighestSalary
FROM
Employee
WHERE salary > (SELECT MIN(salary)
FROM Employee)
E.g.
select max(salary) salary
from employee
where salary not in
(select max(salary) from employee)
I have an employee table in MySQL with below entries. I need to find all the employees having second highest salaries. In this case, it would be c and d.
id | name | salary
1 | a | 1000
2 | b | 1000
3 | c | 500
4 | d | 500
5 | e | 400
I tried running below query
SELECT name, MAX(salary) FROM employee WHERE salary < (SELECT MAX(salary) from employee);
But this query returns just c as a result. How to get both c and d in result?
I looked at bunch of similar questions posted but none of them mentioned how to get multiple rows for second highest salary.
You can esily get second highest salary from table
SELECT MAX(salary) FROM Employee WHERE Salary NOT IN ( SELECT Max(Salary) FROM Employee);
You can find the second highest salary with:
SELECT salary
FROM employee
GROUP BY salary
ORDER BY salary DESC
LIMIT 1, 1
Then either feed the result of that to another query in the same transaction:
SELECT *
FROM employee
WHERE salary = ?
Or do it as a subquery:
SELECT *
FROM employee
WHERE salary = (
SELECT salary
FROM employee
GROUP BY salary
ORDER BY salary DESC
LIMIT 1, 1
)
In case you want migrate to MSSQL Server :).
SELECT * FROM (
SELECT MAX(salary) T,RANK() OVER (ORDER BY SALARY DESC) AS RankBySalary FROM Employees
GROUP BY SALARY ) TB
WHERE RankBySalary = 3
Or much better:
SELECT * FROM
(
SELECT ID,NAME,SALARY,DENSE_RANK() OVER (ORDER BY SALARY DESC) AS RankBySalary FROM employee
)
TB WHERE RankBySalary = 2
SELECT *
FROM employee one1
WHERE ( N ) = (
SELECT COUNT( one2.salary )
FROM employee one2
WHERE one2.salary > one1.salary
)
Note : N means Nth highest salary
Demo
First find the second highest salary amount then select the rows having that salary.
Query
select * from Employees
where Salary = (
select min(t.salary) from (
select salary
from Employees
group by salary
order by salary desc limit 2
)t
);
SQL Fiddle demo
I suggest that you must first select the 2nd highest salary first and then use the derived table with JOIN on original table. like this:
SELECT
original_record.*
FROM
salary_record AS original_record
JOIN
(SELECT
distinct salary
FROM
salary_record
ORDER BY 1
LIMIT 1,1
) AS derived_record
ON
original_record.salary = derived_record.salary
PS: I have renamed your employee table as salary_record table
Also have a look at Varoon Sahgal's article on Nth highest salary, here: http://www.programmerinterview.com/index.php/database-sql/find-nth-highest-salary-sql/ . The comments-section of this article as well as the article itself has some optimized examples.
select name,max(salary) from employee x where (n-1)=(select count(distinct salary)from employee y where x.salary<y.salary);
Nth max salary
-- IF THIS TABLE EXISTS, DROP IT
DROP TABLE E2;
-- THE FOLLOWING CTE ARRANGES SALARIES IN DECENDING ORDER
WITH copytable(Salary) AS
(
SELECT Emp1.Salary
FROM Employees AS Emp1, Employees AS Emp2
WHERE Emp1.Salary > Emp2.salary
GROUP BY Emp1.Salary
)
-- COPY THE CTE IN A TABLE
SELECT * INTO E2 FROM copytable
-- GIVES THE RANK TO THE SALARY IN DECENDING ORDER
ALTER TABLE E2
ADD RankOfSalary INT IDENTITY(1, 1)
-- TO GET THE LOWEST RANK WHICH WILL BE THE HIGHEST SALARY
DECLARE #rankOfSalary int;
SELECT #rankOfSalary = COUNT(Salary) from E2;
-- SELECTING THE SECOND LARGEST SALARY
DECLARE #SelectSalary INT
SELECT #SelectSalary = Salary from E2 where RankOfSalary = #rankOfSalary - 1;
THIS IS HOW YOU DO IT WITHOUT USING max(), order by, top in sql server
to select second largest salary of employees without using max(), order by, top in sql server
JUST WANTED TO POST THIS :p
You can get it by this:
2nd Largest Salary:
SELECT name, salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees))
3rd Largest Salary:
SELECT name, salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees)))
Helpful links:
http://www.mysqltutorial.org/select-nth-highest-record-database-table-using-mysql.aspx
http://www.programmerinterview.com/index.php/database-sql/find-nth-highest-salary-sql/
http://www.coderanch.com/t/530503/JDBC/databases/select-Nth-highest-salary-table
try this TO GET BOTH c AND d
SELECT
name, salary
FROM
employee
WHERE salary = (
SELECT salary
FROM employee
GROUP BY salary
ORDER BY salary DESC
LIMIT 1,1
);
some one help me to find out nth highest salary from the salary table in MYSQL
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
If you want to find nth Salary from a table (here n should be any thing like 1st or 2nd or 15th highest Salaries)
This is the Query for to find nth Salary:
SELECT DISTINCT Salary FROM tblemployee ORDER BY Salary DESC LIMIT 1 OFFSET (n-1)
If you want to find 8th highest salary, query should be :
SELECT DISTINCT Salary FROM tblemployee ORDER BY Salary DESC LIMIT 1 OFFSET 7
Note: OFFSET starts from 0th position, and hence use N-1 rule here
To get nth highest salary you need to first sort data by using ORDER BY and then select the nth highest record using LIMIT with OFFSET.
SELECT DISTINCT(salary) AS salary
FROM tbl_salary
ORDER BY salary DESC
LIMIT 1 OFFSET (n - 1);
SELECT * FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
For each record processed by outer query, inner query will be executed and will return how many records has records has salary less than the current salary. If you are looking for second highest salary then your query will stop as soon as inner query will return N-1.
finding the highest salary
select MAX(Salary) from Employee;
finding the 2nd highest salary
Query-1
SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee);
Query-2
select MAX(Salary) from Employee
WHERE Salary <> (select MAX(Salary) from Employee )
finding the nth highest salary
Query-1
SELECT * /*This is the outer query part */
FROM Employee Emp1
WHERE (N-1) = ( /* Subquery starts here */
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
Query-2
SELECT *
FROM Employee Emp1
WHERE (1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
nth highest salary using the TOP keyword in SQL Server
SELECT TOP 1 Salary
FROM (
SELECT DISTINCT TOP N Salary
FROM Employee
ORDER BY Salary DESC
) AS Emp
ORDER BY Salary
Find the nth highest salary in MySQL
SELECT Salary FROM Employee
ORDER BY Salary DESC LIMIT n-1,1
Find the nth highest salary in SQL Server
SELECT Salary FROM Employee
ORDER BY Salary DESC OFFSET N-1 ROW(S)
FETCH FIRST ROW ONLY
Find the nth highest salary in Oracle using rownum
select * from (
select Emp.*,
row_number() over (order by Salary DESC) rownumb
from Employee Emp
)
where rownumb = n; /*n is nth highest salary*/
Find the nth highest salary in Oracle using RANK
select * FROM (
select EmployeeID, Salary
,rank() over (order by Salary DESC) ranking
from Employee
)
WHERE ranking = N;
Here we can create the MYSQL function for this.
nth highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE limitv INT;
SET limitv = N - 1;
RETURN (
Select IFNULL(
(select Distinct Salary from Employee order by Salary Desc limit limitv, 1),
NULL
) as getNthHighestSalary
);
END
try this:
select MIN(sal) from salary where sal in
(select sal from salary order by sal desc limit 9)
if wanna specified nth highest,could use rank method.
To get the third highest, use
SELECT * FROM
(SELECT #rank := #rank + 1 AS rank, salary
FROM tbl,(SELECT #rank := 0) r
order by salary desc ) m
WHERE rank=3
Try this one for finding 5th highest salary-
SELECT DISTINCT(column name) FROM table ORDER BY (column name) desc LIMIT 4,1
for nth salary-
SELECT DISTINCT(column name) FROM table ORDER BY (column name) desc LIMIT n-1,1
I have tried it on phpmyadmin panel..
+-------+--------+
| name | salary |
+-------+--------+
| A | 100 |
| B | 200 |
| C | 300 |
| D | 400 |
| E | 500 |
| F | 500 |
| G | 600 |
+-------+--------+
IF YOU WANT TO SELECT ONLY Nth HIGHEST SALARY THEN:
SELECT DISTINCT salary FROM emp ORDER BY salary DESC LIMIT 1 OFFSET N-1;
IF YOU WANT TO SELECT ALL EMPLOYEE WHO GETTING Nth HIGHEST SALARY THEN:
SELECT * FROM emp WHERE salary = (
SELECT DISTINCT salary FROM emp ORDER BY salary DESC LIMIT 1 OFFSET N-1
);
SELECT * FROM employe e1 WHERE n-1 = ( SELECT COUNT(DISTINCT(e2.salary)) FROM employe e2 WHERE e2.salary > e1.salary)
Where n = highest number of salary like 1,2,3
Sorting all the records first, do consume a lot of time (Imagine if the table contains millions of records).
However, the trick is to do an improved linear-search.
SELECT * FROM Employee Emp1
WHERE (N-1) = ( SELECT COUNT(*) FROM (
SELECT DISTINCT(Emp2.Salary)
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary LIMIT N))
Here, as soon as inner query finds n distinct salary values greater than outer query's salary, it returns the result to outer query.
Mysql have clearly mentioned about this optimization at http://dev.mysql.com/doc/refman/5.6/en/limit-optimization.html
The above query can also be written as,
SELECT * FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary LIMIT N)
Again, if the queries are as simple as just running on single table and needed for informational purposes only, then you could limit the outermost query to return 1 record and run a separate query by placing the nth highest salary in where clause
Thanks to Abishek Kulkarni's solution, on which this optimization is suggested.
select distinct(column_name) from table_name order by column_name desc limit (n-1),1;
MySQL query to find Nth highest salary from a salary table(100% true)
SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT N-1,1;
SELECT DISTINCT(column_name)
FROM table_name
ORDER BY column_name DESC limit N-1,1;
where N represents the nth highest salary ..
Third highest salary :
SELECT DISTINCT(column_name)
FROM table_name
ORDER BY column_name DESC limit 2,1;
The query to get the nth highest record is as follows:
SELECT
*
FROM
(SELECT
*
FROM
table_name
ORDER BY column_name ASC
LIMIT N) AS tbl
ORDER BY column_name DESC
LIMIT 1;
It's simple and easy to understand
For 4th highest salary:
select min(salary) from (select distinct salary from hibernatepractice.employee e order by salary desc limit 4) as e1;
For n th highest salary:
select min(salary) from (select distinct salary from hibernatepractice.employee e order by salary desc limit n) as e1;
This is salary table
SELECT amount FROM salary
GROUP by amount
ORDER BY amount DESC
LIMIT n-1 , 1
Or
SELECT DISTINCT amount
FROM salary
ORDER BY amount DESC
LIMIT n-1 , 1
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;
set #cnt=0;
select s.* from (SELECT (#cnt := #cnt + 1) AS rank,a.* FROM one as a order by a.salary DESC) as s WHERE s.rank='3';
=>this for 3rd highest salary.
for nth replace 3 value. for example 5th highest:
set #cnt=0;
select s.* from (SELECT (#cnt := #cnt + 1) AS rank,a.* FROM one as a order by a.salary DESC) as s WHERE s.rank='5';
SET #cnt=0;
SELECT s.*
FROM (SELECT ( #cnt := #cnt + 1 ) AS rank,
a.*
FROM one AS a
ORDER BY a.salary DESC) AS s
WHERE s.rank = '3';
To get 2nd highest salary:
SELECT salary
FROM [employees]
ORDER BY salary DESC
offset 1 rows
FETCH next 1 rows only
To get Nth highest salary:
SELECT salary
FROM [employees]
ORDER BY salary DESC
offset **n-1** rows
FETCH next 1 rows only
Try this solution.
select SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT salary ORDER BY salary DESC),',',3),',',-1) from employees
Let there be table salaries containing
+----------+--------+--------+
| emp | salary | deptno |
+----------+--------+--------+
| ep1 | 10 | dp1 |
| ep2 | 20 | dp2 |
| ep3 | 30 | dp2 |
| ep4 | 40 | dp1 |
| ep5 | 50 | dp1 |
| ep6 | 60 | dp3 |
| ep7 | 70 | dp3 |
+----------+--------+--------+
By Nested Queries: (where you can change offset 0/1/2... for first, second and third... place respectively)
select
*
from
salaries as t1
where
t1.salary = (select
salary
from
salaries
where
salaries.deptno = t1.deptno ORDER by salary desc limit 1 offset 1);
or might be by creating rank: (where you can change rank= 1/2/3... for first, second and third... place respectively)
SET #prev_value = NULL;
SET #rank_count = 0;
select * from
(SELECT
s.*,
CASE
WHEN #prev_value = deptno THEN #rank_count := #rank_count + 1
WHEN #prev_value := deptno THEN #rank_count := 1
ELSE #rank_count := 1
END as rank
FROM salaries s
ORDER BY deptno, salary desc) as t
having t.rank = 2;
I have used Procedure for this query
here getHighestSalary procedure, reports the nth highest salary from the Employee table. If there is no nth highest salary, the query should report null.
first, create table
CREATE TABLE employee (
id INT AUTO_INCREMENT,
salary INT,
PRIMARY KEY (id) );
next, create PROCEDURE
DELIMITER //
CREATE PROCEDURE getHighestSalary(emp_id int)
BEGIN
select ifnull((select salary from employee where id = emp_id order by salary desc), null) as getNthHighestSalary;
END //
DELIMITER ;
and last, call the getHighestSalary procedure with n value
call getHighestSalary(2); -- 200
If you want to get all the records of the employees who has third highest salary then you can use this sql query:
Table name: salary
select * from salary where salary =
(select distinct salary from salary order by salary desc limit 2,1)
I have a table of employees and salaries defined that way:
"name" (type: VARCHAR)
"salary" (type: INTEGER)
What query can I use to get the second highest salary in this table?
Here's one that accounts for ties.
Name Salary
Jim 6
Foo 5
Bar 5
Steve 4
SELECT name, salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees))
Result --> Bar 5, Foo 5
EDIT:
I took Manoj's second post, tweaked it, and made it a little more human readable. To me n-1 is not intuitive; however, using the value I want, 2=2nd, 3=3rd, etc. is.
/* looking for 2nd highest salary -- notice the '=2' */
SELECT name,salary FROM employees
WHERE salary = (SELECT DISTINCT(salary) FROM employees as e1
WHERE (SELECT COUNT(DISTINCT(salary))=2 FROM employees as e2
WHERE e1.salary <= e2.salary)) ORDER BY name
Result --> Bar 5, Foo 5
A straight forward answer for second highest salary
SELECT name, salary
FROM employees ORDER BY `employees`.`salary` DESC LIMIT 1 , 1
another interesting solution
SELECT salary
FROM emp
WHERE salary = (SELECT DISTINCT(salary)
FROM emp as e1
WHERE (n) = (SELECT COUNT(DISTINCT(salary))
FROM emp as e2
WHERE e1.salary <= e2.salary))
Seems I'm much late to answer this question. How about this one liner to get the same output?
SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1,1 ;
sample fiddle: https://www.db-fiddle.com/f/v4gZUMFbuYorB27AH9yBKy/0
create table svalue (
name varchar(5),
value int
) engine = myisam;
insert into svalue value ('aaa',30),('bbb',10),('ccc',30),('ddd',20);
select * from svalue where value = (
select value
from svalue
group by value
order by value desc limit 1,1)
FOR SECOND LAST:
SELECT name, salary
FROM employee
ORDER BY salary DESC
LIMIT 1 , 1
FOR THIRD LAST:
SELECT name, salary
FROM employee
ORDER BY salary DESC
LIMIT 2 , 1
You can use this below mentioned query
SELECT emp.name, emp.salary
FROM employees emp
WHERE 2 = (SELECT COUNT(DISTINCT salary)
FROM employees
WHERE emp.salary<=salary
);
You can change 2 to your desired highest record.
To display records having second largest value of mark:
SELECT username, mark
FROM tbl_one
WHERE mark = (
SELECT DISTINCT mark
FROM tbl_one
ORDER by mark desc
LIMIT 1,1
);
simple solution
SELECT * FROM TBLNAME ORDER BY COLNAME ASC LIMIT (n - x), 1
Note: n = total number of records in column
x = value 2nd, 3rd, 4th highest etc
e.g
//to find employee with 7th highest salary
n = 100
x = 7
SELECT * FROM tbl_employee ORDER BY salary ASC LIMIT 93, 1
hope this helps
Found another interesting solution
SELECT salary
FROM emp
WHERE salary = (SELECT DISTINCT(salary)
FROM emp as e1
WHERE (n) = (SELECT COUNT(DISTINCT(salary))
FROM emp as e2
WHERE e1.salary <= e2.salary))
Sorry. Forgot to write. n is the nth number of salary which you want.
The simple solution is as given below in query:
select max(salary) as salary from employees where salary<(select max(salary) from employees);
for 2nd heightest salary
select max(salary) from salary where salary not in (select top 1 salary from salary order by salary desc)
for 3rd heightest salary
select max(salary) from salary where salary not in (select top 2 salary from salary order by salary desc)
and so on......
SELECT MAX(salary) salary
FROM tbl
WHERE salary <
(SELECT MAX(salary)
FROM tbl);
To get the *N*th highest value, better to use this solution:
SELECT * FROM `employees` WHERE salary =
(SELECT DISTINCT(salary) FROM `employees`
ORDER BY salary DESC LIMIT {N-1},1);
or you can try with:
SELECT * FROM `employees` e1 WHERE
(N-1) = (SELECT COUNT(DISTINCT(salary))
FROM `employees` e2
WHERE e1.salary < e2.salary );
N=2 for second highest
N=3 for third highest and so on.
SELECT DISTINCT Salary
FROM emp
ORDER BY salary DESC
LIMIT 1 , 1
This query will give second highest salary of the duplicate records as well.
To get the second highest salary just use the below query
SELECT salary FROM employees
ORDER BY salary DESC LIMIT 1,1;
To get second highest value:
SELECT `salary` FROM `employees` ORDER BY `salary` DESC LIMIT 1, 1;
SELECT name, salary
FROM employees
where
salary = (SELECT (salary) FROM employees GROUP BY salary DESC LIMIT 1,1)
Try this one to get n th max salary
i have tried this before posting & It Works fine
eg. to find 10th max salary replace limit 9,1;
mysql> select name,salary from emp group by salary desc limit n-1,1;
SELECT MIN(id) as id FROM students where id>(SELECT MIN(id) FROM students);
SELECT name, salary
FROM EMPLOYEES
WHERE salary = (
SELECT DISTINCT salary
FROM EMPLOYEES
ORDER BY salary DESC
LIMIT 1 , 1 )
with alias as
(
select name,salary,row_number() over(order by salary desc ) as rn from employees
)
select name,salary from alias where rn=n--n being the nth highest salary
SELECT username, salary
FROM tblname
GROUP by salary
ORDER by salary desc
LIMIT 0,1 ;
SELECT name,salary FROM employee
WHERE salary = (SELECT DISTINCT(salary) FROM employee ORDER BY salary DESC LIMIT 1,1) ORDER BY name
Get second, third, fourth......Nth highest salary using following query
SELECT MIN(salary) from employees WHERE salary IN( SELECT TOP N salary FROM employees ORDER BY salary DESC)
Replace N by you number i.e. N=2 for second highest salary,
N=3 for third highest salary and so on. So for second highest salary use
SELECT MIN(salary) from employees WHERE salary IN( SELECT TOP 2 salary FROM employees ORDER BY salary DESC)
SELECT name, salary
FROM employees
order by salary desc limit 1,1
and this query should do your job.
First we are sorting the table in descending way so the person with the highest salary is at the top, and the second highest is at the second position. Now limit a,b means skip the starting a elements and then print the next b elements. So you should use limit 1,1 in this case.
Hope this helps.
Try this :
SELECT DISTINCT(`salary`)
FROM `employee`
ORDER BY `salary` DEC
LIMIT 1,1
SELECT SALARY
FROM (SELECT *
FROM EMPLOYEE
ORDER BY SALARY
DESC LIMIT ***2***) AS TOP_SALARY
ORDER BY SALARY ASC
LIMIT 1
select MIN(salary) from employee order by age desc limit 2;
It sorts the column in descending order takes the top 2 and returns the minimum of them which is the second highest.
Try this :
Proc sql;
select employee, salary
from (select * from test having salary < max(salary))
having salary = max(salary)
;
Quit;