can you please help me to answer that problem?
This is a question, I have an employee salary table, How do I find the second highest from it.?

A big thanks to those who will help. I appreciate it a lot.

Recommended Answers

All 4 Replies

Member Avatar for diafol

A simple one would be

... ORDER BY salary LIMIT 2 

and pick the last one ;) If you use mysqli/PDO and get the array instead of the resource from mysql_* then you just pick $result[1]. Doubtless a more sophisticated solution is available maybe using MIN()/GROUP BY from the dataset.

//EDIT
Disregard - was having a very senior moment - check out latest post

ahm you can try this one.

SELECT * FROM EmployeeSalary a WHERE (2=(SELECT COUNT(Distinct(b.salary)) FROM EmployeeSalary b WHERE b.salary>=a.salary))
commented: For oracle its cool.Don't know why someone -1d it. +0
Member Avatar for diafol
SELECT * FROM table ORDER BY salary DESC LIMIT 1,1

DOh!

Note the LIMIT (x,y) is zero-indexed for x, that is it starts at 0 for record 1 and then 1 for record 2 etc.

commented: nods +14

Try this query to find second highest salary.

SELECT MAX(salary) From table_name WHERE salary < (SELECT Max(salary) FROM table_name);
commented: Why so late? -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.