Ask Claude about this

Most Recent Employee Hire Analysis

Business Context

At Microsoft, tracking our talent acquisition across departments is crucial for workforce planning, onboarding coordination, and understanding departmental growth patterns. This analysis identifies the most recently hired employee in each department, providing HR and department managers with insights into their newest team members and helping them ensure proper onboarding and integration procedures are followed.

Technical Requirements

The primary objective is to develop a PostgreSQL query for the following:

Task: Find the most recently hired employee in each department.

Definition:

  • "Most recently hired" refers to the employee with the latest hire_date within each department.
  • If multiple employees share the same latest hire_date in a department, include all of them. (Note: The provided sample data does not have such ties for simplicity in the expected output, but the solution should handle it.)
  • The output should list the department name, employee name, and their hire date.

Database Schema

The database consists of two tables: Employees and Departments.

-- Table: Departments
CREATE TABLE Departments (
    department_id INT PRIMARY KEY,
    department_name VARCHAR(255) NOT NULL
);

-- Table: Employees
CREATE TABLE Employees (
    employee_id INT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    department_id INT,
    hire_date DATE NOT NULL,
    salary INT,
    FOREIGN KEY (department_id) REFERENCES Departments(department_id)
);

Sample Data

Assume the following sample data for the Employees and Departments tables:

Table: Employees

employee_id name department_id hire_date salary
101 John Smith 1 2022-01-10 60000
102 Jane Doe 1 2022-02-15 65000
103 Bob Johnson 2 2022-01-05 55000
104 Alice Brown 2 2022-03-20 62000
105 Mike Wilson 3 2022-02-10 70000
106 Sarah Garcia 3 2022-01-20 65000

Table: Departments

department_id department_name
1 Engineering
2 Marketing
3 Finance

Expected Output (based on sample data):

department_name employee_name hire_date
Engineering Jane Doe 2022-02-15
Marketing Alice Brown 2022-03-20
Finance Mike Wilson 2022-02-10

Most Recent Hire per Department

MODERATE

Write a PostgreSQL query to identify the most recently hired employee(s) in each department. Display the department name, the employee's name, and their hire date.

Your Turn! What are your thoughts on this problem or alternative ways to solve it? Share your own SQL query attempts or insights in the comments below!

Nerchuko Academy · Free DS Interview Prep