w3resource

SQL Exercise: Employees who earn less than employee 182

SQL JOINS on HR Database: Exercise-7 with Solution

7. From the following table, write a SQL query to find the employees who earn less than the employee of ID 182. Return first name, last name and salary.

Sample table: employees


Sample Solution:

-- Selecting specific columns (E.first_name, E.last_name, E.salary) from the 'employees' table, aliased as 'E'
SELECT E.first_name, E.last_name, E.salary 

-- Performing an INNER JOIN between the 'employees' table (aliased as 'E') and the 'employees' table (aliased as 'S') based on the condition that 'E.salary' is less than 'S.salary' and 'S.employee_id' is 182
FROM employees E 

JOIN employees S
  ON E.salary < S.salary 
   AND S.employee_id = 182;

Sample Output:

first_name	last_name	salary
James		Landry		2400.00
Steven		Markle		2200.00
TJ		Olson		2100.00
Ki		Gee		2400.00
Hazel		Philtanker	2200.00

Code Explanation:

The said query in SQL that selects the first name, last name, and salary of all employees who earn less than the employee with ID 182. The query retrieves data from the employees table.
The JOIN clause joins the employees table with itself using the salary column, which is used to compare salaries between employees. This type of join is known as a self-join.
The ON clause specifies the join condition, which is that the salary of the employee from the left table (E) must be less than the salary of the employee with ID 182 from the right table (S).

Relational Algebra Expression:

Relational Algebra Expression: Display the first and last name and salary for those employees who earn less than the employee earn whose number is 182.

Relational Algebra Tree:

Relational Algebra Tree: Display the first and last name and salary for those employees who earn less than the employee earn whose number is 182.

Visual Presentation:

SQL Exercises: Display the first and last name and salary for those employees who earn less than the employee earn whose number is 182

Alternative Solutions:

Using a Subquery with Comparison Operator:


SELECT E.first_name, E.last_name, E.salary
FROM employees E
WHERE E.salary < (SELECT salary FROM employees WHERE employee_id = 182);

Using a Correlated Subquery:


SELECT E.first_name, E.last_name, E.salary
FROM employees E
WHERE E.salary < (
    SELECT S.salary
    FROM employees S
    WHERE S.employee_id = 182
);

Practice Online


HR database model

Query Visualization:

Duration:

Query visualization of Display the first and last name and salary for those employees who earn less than the employee earn whose number is 182 - Duration

Rows:

Query visualization of Display the first and last name and salary for those employees who earn less than the employee earn whose number is 182 - Rows

Cost:

Query visualization of Display the first and last name and salary for those employees who earn less than the employee earn whose number is 182 - Cost

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous SQL Exercise: Departments which does not have any employee.
Next SQL Exercise: Employees including the first name of their manager.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://www.w3resource.com/sql-exercises/joins-hr/sql-joins-hr-exercise-7.php