Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Self-Join for Hierarchies
00:00
5 left

Self-Join for Hierarchies

MediumSQL · PostgreSQL

Problem

Write a query using a self-join to find employees who earn more than their managers.

Use the employees table and compare each employee's salary with the salary of the employee referenced by manager_id. Exclude employees without a manager or without a comparable salary.

Output

  1. One row per employee earning more than their manager
  2. Columns: employee_id, employee_name, employee_salary, manager_name, manager_salary
  3. Sort by employee_id ascending

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee's full name
manager_idINTEmployee ID of the person's manager
salaryDECIMAL(12,2)Annual employee salary
Tablesemployees
Interviewer

Your question is Self-Join for Hierarchies. 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.