w3resource

SQL Exercise: List the result in the ascending order of experience

SQL subqueries on employee Database: Exercise-15 with Solution

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

15. From the following tables, write a SQL query to find those employees of grade 3 and 4 and work in the department of FINANCE or AUDIT and whose salary is more than the salary of ADELYN and experience is more than FRANK. Return complete information about the employees.

Sample table: employees


Sample table: department


Sample table: salary_grade


Sample Solution:

SELECT *
FROM employees e
WHERE e.dep_id IN
    (SELECT d.dep_id
     FROM department d
     WHERE d.dep_name IN ('FINANCE',
                          'AUDIT') )
  AND e.salary >
    (SELECT salary
     FROM employees
     WHERE emp_name = 'ADELYN')
  AND e.hire_date <
    (SELECT hire_date
     FROM employees
     WHERE emp_name = 'FRANK')
  AND e.emp_id IN
    (SELECT e.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) )
ORDER BY e.hire_date ASC;

Sample Output:

 emp_id | emp_name | job_name | manager_id | hire_date  | salary  | commission | dep_id
--------+----------+----------+------------+------------+---------+------------+--------
  65646 | JONAS    | MANAGER  |      68319 | 1991-04-02 | 2957.00 |            |   2001
  67832	| CLARE	   | MANAGER  |      68319 | 1991-06-09 | 2550.00 |            |   1001
(2 rows)

Explanation:

The given query in SQL that selects all employees who work in the "FINANCE" or "AUDIT" departments, have a salary greater than the employee named "ADELYN", were hired before the employee named "FRANK", and have a salary grade of 3 or 4 from the "employees" table. The results are ordered by hire date in ascending order.

The first condition in the WHERE clause filters only employees who work in departments with names of "FINANCE" or "AUDIT" obtained from a subquery. The subquery that selects the "dep_id" of departments with these names.

The second condition in the WHERE clause filters only employees with a salary greater than the salary of the employee named "ADELYN" obtained from a subquery. The subquery that selects the salary of the employee named "ADELYN".

The third condition in the WHERE clause filters only employees who were hired before the employee named "FRANK" obtained from a subquery. The subquery that selects the hire date of the employee named "FRANK".

The fourth condition in the WHERE clause filters only employees with a salary grade of 3 or 4 obtained from a subquery. The subquery that joins the 'employees' and 'salary_grade' tables and selects employees whose salary falls within the range defined by the "min_sal" and "max_sal" columns of the "salary_grade" table and whose salary grade is either 3 or 4.

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 BLAZE and work at PERTH, BRISBANE.
Next SQL Exercise: Employees with common designation as SANDRINE or ADELYN.

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://w3resource.com/sql-exercises/employee-database-exercise/sql-subqueries-exercise-employee-database-15.php