Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 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
Build my plan
Complex SQL and Recursion
00:00
5 left

Complex SQL and Recursion

HardSQL · PostgreSQL

Problem

Explain how you would handle complex SQL queries, including cases that involve recursion.

Write a PostgreSQL query that expands the reachable reporting hierarchy from employees whose manager is NULL. Exclude employees whose manager cannot be reached from a root, and protect the query from revisiting an employee in the same path.

Output

  1. One row per reachable employee with employee_id, employee_name, root_employee_id, depth, and hierarchy_path
  2. depth is 0 for each root
  3. Order by hierarchy traversal from each root, with roots ordered by root_employee_id

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee display name
manager_idINTEmployee ID of the direct manager
Tablesemployees
Interviewer

Your question is Complex SQL and Recursion. Start with the requirements and the one table 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.