Paige is leaving
- O(n²) · Very hard
- Full plan
- SQL
- changing data
- subqueries
Problem
Paige, employee 3, is leaving the company. Before her row gets deleted, her people need somewhere to go: whoever reports to her today will report to whoever Paige reports to. Nobody is left without a boss, and nobody is left pointing to someone who's gone.
Her people are the ones who have her as their boss, whatever department they're in. And don't type in Paige's boss by hand: the change has to work just as well on the day she reports to someone else.
Write the handoff and the delete. It's graded by looking at who the boss is for each person still at the company.
What it has to return
After your code, this query runs
SELECT id, name, boss_id FROM employees;These rows, in any order
| id | name | boss_id |
|---|---|---|
| 1 | Carol | NULL |
| 2 | Mark | 1 |
| 4 | Rachel | 2 |
| 5 | Nicole | 2 |
| 6 | Samuel | 1 |
| 7 | Lydia | 1 |
| 8 | Frank | 1 |
| 9 | Grace | 1 |
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
DELETE FROM employees
WHERE id = 3;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(n²) challenges
- Past dueSQL · dates · nulls · joins
- Starting at the bottomSQL · changing data · subqueries
- The library forgetsSQL · changing data · subqueries
- The next rung upSQL · window functions · subqueries
- The register, day by daySQL · window functions · grouping · conditionals
- The third stampSQL · window functions · joins · dates