w3resource

SQL Exercise: Most recently hired employees of department 3001

SQL subqueries on employee Database: Exercise-70 with Solution

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

70. From the following table, write a SQL query to find the recently hired employees of department 3001. Return complete information about the employees.

Sample table: employees


Sample Solution:

SELECT *
FROM employees
WHERE hire_date IN
    (SELECT max(hire_date)
     FROM employees
     WHERE dep_id = 3001) AND dep_id=3001;

Sample Output:

 emp_id | emp_name | job_name | manager_id | hire_date  | salary  | commission | dep_id
--------+----------+----------+------------+------------+---------+------------+--------
  69000 | JULIUS   | CLERK    |      66928 | 1991-12-03 | 1050.00 |            |   3001
(1 rows)

Explanation:

The said query in SQL that retrieves all the records from the 'employees' table where the hire_date is equal to the maximum hire_date for employees in the department with ID 3001.

The subquery selects the maximum hire_date for employees in the department with ID 3001. The main query then selects all employees with a hire_date equal to that maximum date, and also filters for employees who are in the department with ID 3001.

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: Total remuneration of all sales person of a department.
Next SQL Exercise: Highest paid PERTH employees before the latest grade 2.

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-70.php