How to Find nth MIN and nth Max Salary

SQL to Find the 3rd MAX salary

[code language=”sql”]

SELECT DISTINCT sal
FROM emp e1
WHERE 3 =
(SELECT count(DISTINCT sal)
FROM emp e2
WHERE e1.sal <= e2.sal);

[/code]

SQL to Find the 3rd MIN salary

[code language=”sql”]

SELECT DISTINCT sal
FROM emp e1
WHERE 3 =
(SELECT count(DISTINCT sal)
FROM emp e2
WHERE e1.sal >= e2.sal);

[/code]

Click here for more Complex SQL Queries.

Related

Discover more from Srinimf

Subscribe now to keep reading and get access to the full archive.

Continue reading