SQL Exercise: Find out the least 5 earners of the company
SQL subqueries on employee Database: Exercise-38 with Solution
[An editor is available at the bottom of the page to write and execute the scripts.]
38. From the following table, write a SQL query to find five lowest paid workers. Return complete information about the employees.
Sample table: employees
Sample Solution:
SELECT *
FROM employees e
WHERE 5>
(SELECT count(*)
FROM employees
WHERE e.salary >salary);
Sample Output:
emp_id | emp_name | job_name | manager_id | hire_date | salary | commission | dep_id --------+----------+----------+------------+------------+---------+------------+-------- 63679 | SANDRINE | CLERK | 69062 | 1990-12-18 | 900.00 | | 2001 65271 | WADE | SALESMAN | 66928 | 1991-02-22 | 1350.00 | 600.00 | 3001 66564 | MADDEN | SALESMAN | 66928 | 1991-09-28 | 1350.00 | 1500.00 | 3001 68736 | ADNRES | CLERK | 67858 | 1997-05-23 | 1200.00 | | 2001 69000 | JULIUS | CLERK | 66928 | 1991-12-03 | 1050.00 | | 3001 (5 rows)
Explanation:
The said query in SQL that retrieves the five employees with the lowest salaries in the company from the 'employees' table.
The subquery in the WHERE clause that counts the number of employees who have a higher salary than the current employee being evaluated is used to limit the results to only the five employees with the lowest salaries.
The outer SELECT statement then returns the information for the employees with the five employees with the lowest salaries.
Practice Online
Structure of employee Database:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: List average salary of employees in department wise.
Next SQL Exercise: List the managers, not working under the PRESIDENT.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/sql-exercises/employee-database-exercise/sql-subqueries-exercise-employee-database-38.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics