Self Joins & Hierarchical Queries SQL Topic exerciseEasyVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Employees Earning More Than Their Manager

Find every employee whose salary is strictly greater than their direct manager's salary. Return employee_name, employee_salary, manager_name, manager_salary — ordered by employee_name, employee_id.

  • Joins
  • Filtering
  • Sorting

Exercise brief

Understand the request

Compensation governance analyst A pay review needs defensible employee-to-manager comparisons at direct-report grain.

Return

  • Return employee and manager names with both salaries.
  • Order deterministically by employee name and ID.

Constraints

  • Use the manager_id relationship.
  • Use a strict salary comparison.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

employees

  • employee_idINTEGER
  • employee_nameTEXT
  • manager_idINTEGER
  • salaryINTEGER

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

Self-join, then a WHERE that compares two columns from the SAME table (different aliases).

Hint 2

INNER JOIN here — the question is meaningless for employees with no manager.

Hint 3

Tie-breaker on employee_id keeps results stable when two employees share the same name.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT e.employee_name, e.salary AS employee_salary, m.employee_name AS manager_name, m.salary AS manager_salary FROM employees e INNER JOIN employees m ON e.manager_id = m.employee_id WHERE e.salary > m.salary ORDER BY e.employee_name, e.employee_id;

Why this works

Cross-row comparisons inside the same table are exactly what self-joins are for. Once aliased, e.salary and m.salary look like columns from two different tables to the optimizer.

Success check

Only employees strictly above their direct manager are returned.

Expected result

Use this output to verify values, aliases, ordering, and row count.

employee_nameemployee_salarymanager_namemanager_salary
Mia Overpaid Jr125000Emma Sales Mgr120000

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.