Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Self-Join for Hierarchies

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

Your question is Self-Join for Hierarchies. Start with the requirements and the one table 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

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results