Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL for Employee Queries

HardSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL for Employee Queries. Start with the requirements and the three tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Context

VLink's employee directory needs to identify active employees who are not currently assigned to any department. Department assignments may be expired, cancelled, inactive, or scheduled for a future date.

Task

Write a PostgreSQL query using an as-of date of 2025-02-15 to find all active VLink employees without a valid current department assignment.

Requirements

  1. Consider an assignment current only when its status is ACTIVE, its start date is on or before the as-of date, and its end date is null or on or after the as-of date.
  2. Return each qualifying employee once, even if assignment history contains multiple rows.
  3. Include the employee's most recent recorded department assignment, department hierarchy path, and assignment end date when available.
  4. Exclude employees whose employment status is not ACTIVE.
  5. Use the department hierarchy to produce a readable path from the top-level department to the assigned department.
  6. Sort the results by employee name.

Schema

employees
ColumnTypeDescription
employee_idPKINTEmployee identifier
full_nameVARCHAR(100)Employee full name
emailVARCHAR(150)Corporate email address
employment_statusVARCHAR(20)Current employment status
departments
ColumnTypeDescription
department_idPKINTDepartment identifier
department_nameVARCHAR(100)Department name
parent_department_idINTParent department identifier
employee_department_assignments
ColumnTypeDescription
assignment_idPKINTAssignment identifier
employee_idINTAssigned employee identifier
department_idINTAssigned department identifier
assignment_statusVARCHAR(20)Assignment lifecycle status
effective_start_dateDATEAssignment start date
effective_end_dateDATEAssignment end date
Tablesemployeesdepartmentsemployee_department_assignments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results