PostgreSQL Subquery: Get three minimum salaries
20. Write a query to get three minimum salaries.
SELECT DISTINCT salary FROM employees a WHERE 3 >= (SELECT COUNT(DISTINCT salary) FROM employees b WHERE a.salary >= b.salary) ORDER BY a.salary DESC;
Sample table: employees
pg_exercises=# SELECT DISTINCT salary pg_exercises-# FROM employees a pg_exercises-# WHERE 3 >= (SELECT COUNT(DISTINCT salary) pg_exercises(# FROM employees b pg_exercises(# WHERE a.salary >= b.salary) pg_exercises-# ORDER BY a.salary DESC; salary --------- 2430.00 2230.00 2130.00 (3 rows)
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?