Skip to the content

Frank against everyone

Problem

Frank, employee 8, proposed changing the whole company's hours, and nobody agrees with him. At this company, an argument between two people is settled by the closest person who is over both of them: the first boss they have in common going up the org chart. Between Rachel and Nicole, Mark decides; between Rachel and Grace, Carol does. And if one of the two is the other's boss, directly or not, that one decides: that's what being the boss is for.

Show the name of each person in the company who isn't Frank and the name of whoever settles their argument with him, in that order, sorted alphabetically by the name in the first column.

The org chart changes: the query should work with however many levels there are, and also on the day Frank has people reporting to him.

What it has to return

These rows, in this order

namedecides
CarolCarol
GraceCarol
LydiaPaige
MarkCarol
NicoleCarol
PaigePaige
RachelCarol
SamuelPaige

This is what comes out with the data in the practice database this challenge uses. The column names don’t count, so name yours however you like: what counts is how many there are and the order they come in. Besides this one, the challenge has hidden tests with other data that are revealed when you submit your solution.

You start with this

SQL

SELECT e.name, b.name AS boss
FROM employees e
LEFT JOIN employees b
  ON e.boss_id = b.id
WHERE e.id <> 8
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(2ⁿ) challenges

See all challenges →