w3resource

SQL Exercise: Grade, same as TUCKER or more experience than SANDRINE

SQL subqueries on employee Database: Exercise-67 with Solution

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

67. From the following table, write a SQL query to find those employees whose grade same as the grade of TUCKER or experience is more than SANDRINE and who are belonging to SYDNEY or PERTH. Return complete information about the employees.

Sample table: employees


Sample table: department


Sample table: salary_grade


Sample Solution:

SELECT *
FROM employees e,
     department d,
     salary_grade s
WHERE e.dep_id= d.dep_id
  AND d.dep_location IN ('SYDNEY',
                         'PERTH')
  AND e.salary BETWEEN s.min_sal AND s.max_sal
  AND (s.grade IN
         (SELECT s.grade
          FROM employees e,
               salary_grade s
          WHERE e.salary BETWEEN s.min_sal AND s.max_sal
            AND e.emp_name = 'TUCKER')
       OR age (CURRENT_DATE,hire_date) >
         (SELECT age(CURRENT_DATE,hire_date)
          FROM employees
          WHERE emp_name = 'SANDRINE')) ;

Sample Output:

 emp_id | emp_name | job_name | manager_id | hire_date  | salary  | commission | dep_id | dep_id | dep_name  | dep_location | grade | min_sal | max_sal
--------+----------+----------+------------+------------+---------+------------+--------+--------+-----------+--------------+-------+---------+---------
  64989 | ADELYN   | SALESMAN |      66928 | 1991-02-20 | 1700.00 |     400.00 |   3001 |   3001 | MARKETING | PERTH        |     3 |    1501 |    2100
  68454 | TUCKER   | SALESMAN |      66928 | 1991-09-08 | 1600.00 |       0.00 |   3001 |   3001 | MARKETING | PERTH        |     3 |    1501 |    2100
(2 rows)

Explanation:

The said query in SQL that retrieves all columns of employees from the 'employees' and 'department' and 'salary_grade' table where the employee's department location is either 'SYDNEY' or 'PERTH', and the employee's salary falls within the salary range for a given grade in the 'salary_grade' table and includes those rows that satisfy the conditions are as follows :

If the employee's grade is the same as or higher than the grade for an employee named 'TUCKER', obtained from a subquery,

or

If the employee's hire date is more recent than that of an employee named 'SANDRINE', also obtained from a subquery.

The WHERE clause filters the results to include only those rows where the employee's department location is either 'SYDNEY' or 'PERTH' and their salary falls within the salary range for a grade in the 'salary_grade' table obtained from a subquery.

The subquery retrieves the grade for an employee named 'TUCKER' from the 'employees' and 'salary_grade' tables.

The AND operator in the subquery selects only the employee named 'TUCKER'.

The OR operator checks if the employee's age in years, as calculated by the "age" function, is greater than the age of an employee named 'SANDRINE', also obtained from a subquery.

The subquery retrieves the age of an employee named 'SANDRINE' from the 'employees' table.

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 with salary grades higher than MARKER.
Next SQL Exercise: Employees whose salary is same as any one employee.

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.