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.
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.
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))
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.
Try this query to find second highest salary.
SELECT MAX(salary) From table_name WHERE salary < (SELECT Max(salary) FROM table_name);
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.