Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
SQL for Employee Queries
00:00
5 left

SQL for Employee Queries

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.

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

Your question is SQL for Employee Queries. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.