PostgreSQL Subquery: Get three minimum salaries
20. Write a query to get three minimum salaries.
Sample Solution:
Code:
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
Output:
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 --------- 2400.00 2200.00 2100.00 (3 rows)
Practice Online
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a query to get three maximum salaries.
Next: Write a query to get nth max salaries of employees.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework