w3resource

SQL Exercise: Employees within grade 3 to 5 and belongs to SYDNEY

SQL subqueries on employee Database: Exercise-22 with Solution

[An editor is available at the bottom of the page to write and execute the scripts.]

22. From the following tables, write a SQL query to find those employees of grade 3 to 5 and location at SYDNEY. The employees are not in PRESIDENT designated and salary is more than the highest paid employee of PERTH where no MANAGER and SALESMAN are working under KAYLING. Return complete information about the employees.

Sample table: employees


Sample table: department


Sample table: salary_grade


Sample Solution:

SELECT *
FROM employees
WHERE dep_id IN
    (SELECT dep_id
     FROM department
     WHERE department.dep_location ='SYDNEY')
  AND emp_id IN
    (SELECT emp_id
     FROM employees e,
          salary_grade s
     WHERE e.salary BETWEEN s.min_sal AND s.max_sal
       AND s.grade IN (3,
                       4,
                       5) )
  AND job_name != 'PRESIDENT'
  AND salary >
    (SELECT max(salary)
     FROM employees
     WHERE dep_id IN
         (SELECT dep_id
          FROM department
          WHERE department.dep_location = 'PERTH')
       AND job_name IN ('MANAGER',
                        'SALESMAN')
       AND manager_id NOT IN
         (SELECT emp_id
          FROM employees
          WHERE emp_name = 'KAYLING'));

Sample Output:

 emp_id | emp_name | job_name | manager_id | hire_date  | salary  | commission | dep_id
--------+----------+----------+------------+------------+---------+------------+--------
  67832 | CLARE    | MANAGER  |      68319 | 1991-06-09 | 2550.00 |            |   1001
(1 row)

Explanation:

The said query in SQL that selects all employees from the employees table who meet the following criteria:

They work in a department located in Sydney.

Their salary falls within a certain range based on their salary grade (grade 3, 4, or 5).

They are not a president.

Their salary is greater than the highest salary of any employee who meets the following criteria:

a. They work in a department located in Perth.

b. Their job title is either 'MANAGER' or 'SALESMAN'.

c. Their manager's name is not 'KAYLING'.

The first WHERE clause filters the results to only include employees whose dep_id matches a dep_id of a department located in Sydney.

The second WHERE clause filters the results further to only include employees whose salary falls within the salary range specified by their salary grade (which is determined by joining the employees table with the salary_grade table) and by the WHERE clause excludes any employees whose job title is 'PRESIDENT'.

The another WHERE clause filters the results even further to only include employees whose salary is greater than the maximum salary of any employee who meets some criteria which is determined by a subquery -

Finds the maximum salary among all employees who work in a department located in Perth.

Employees , whose job title is either 'MANAGER' or 'SALESMAN'.

And the employees whose manager's name is not 'KAYLING'.

Practice Online


Structure of employee Database:

employee database structure

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

Previous SQL Exercise: Employees senior to most recently hired under KAYLING.
Next SQL Exercise: List the details of senior employees as on year 1991.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.