Frank against everyone
- O(2ⁿ) · Legendary
- Full plan
- SQL
- recursion
- joins
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
| name | decides |
|---|---|
| Carol | Carol |
| Grace | Carol |
| Lydia | Paige |
| Mark | Carol |
| Nicole | Carol |
| Paige | Paige |
| Rachel | Carol |
| Samuel | Paige |
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 <> 8It 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
- Not one more, not one lessSQL · window functions · operations
- The chain of commandSQL · recursion · strings
- The emptiest shelfSQL · window functions · dates · sets
- The salary in the middleSQL · window functions · grouping
- The store on a streakSQL · window functions · dates
- The Winters oneSQL · recursion · strings · window functions