postgresql - What is a simple SQL query to select the ID of a person with the SECOND highest salary -
if had relation: employee(id, salary). simplest sql query select id of person second highest salary.
i know how select highest salary using:
select max(salary) employee salary < (select max(salary) employee);
but how select id of person, , display id of person second highest salary (without using limit).
i use row_number()
or rank()
, depending on mean second highest salary:
select e.* (select e.*, rank() on (order salary desc) seqnum employee e ) e seqnum = 2;
if want use query -- , seems way more complicated -- can use subqueries:
select e.* employee e e.salary = (select max(e2.salary) employee e2 e2.salary < (select max(e3.salary) employee e3 ) );
it nice see sql has made progress in last 30 years.
Comments
Post a Comment