Inilah salah satu yang menjelaskan ikatan.
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: Saya mengambil posting kedua Manoj, mengubahnya, dan membuatnya sedikit lebih mudah dibaca manusia. Bagi saya n-1 tidak intuitif; namun, menggunakan nilai yang saya inginkan, 2=2, 3=3, dll. adalah.
/* 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