Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Find Active Employees Without Current Departments
00:00
5 left

Find Active Employees Without Current Departments

MediumSQL · PostgreSQL

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.

Output details

  • Return columns in this order: employee_id, full_name, email, last_department_name, last_department_path, last_assignment_end_date.
  • Sort by full_name in ascending order.
  • When selecting the most recent assignment, break ties by descending effective_start_date, then descending assignment_id.
  • Build department paths with > between names, beginning at a top-level department.
  • Treat assignments as current only when their departments are reachable from a top-level department in the hierarchy.
  • Return NULL for unavailable department names, paths, and assignment end dates.

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
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.