SQL Exercise: Employees whose salary is same as any one employee
SQL employee Database: Exercise-68 with Solution
[An editor is available at the bottom of the page to write and execute the scripts.]
68. From the following tables, write a SQL query to find those employees whose salary is same as any one of the employee. Return complete information about the employees.
Sample table: employees
Sample Solution:
SELECT *
FROM employees
WHERE salary IN
(SELECT salary
FROM employees e
WHERE employees.emp_id <> e.emp_id);
Sample Output:
emp_id | emp_name | job_name | manager_id | hire_date | salary | commission | dep_id --------+----------+----------+------------+------------+---------+------------+-------- 67858 | SCARLET | ANALYST | 65646 | 1997-04-19 | 3100.00 | | 2001 69062 | FRANK | ANALYST | 65646 | 1991-12-03 | 3100.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 (4 rows)
Explanation:
The said query in SQL that selects all the records from the 'employees' table where the salary is present in the subquery that selects the salary of all employees who have a different emp_id than the current record.
Practice Online
Sample Database: employee
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: Grade, same as TUCKER or more experience than SANDRINE.
Next SQL Exercise: Total remuneration of all sales person of a department.
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://www.w3resource.com/sql-exercises/employee-database-exercise/sql-subqueries-exercise-employee-database-68.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics