Who signs off on my day off?
- O(log n) · Easy
- Free
- SQL
- nulls
- sorting
Problem
At this company, a day off is signed by the direct boss of whoever asks for it. Carol runs the company and has nobody above her: she signs her own.
HR wants the table that will go on the form. Show each person's name and the employee number (the id) of whoever signs for them, in that order. No cell is left empty.
Sort the rows by the signer's number, so each boss sees their people together, and within each boss, by name.
What it has to return
These rows, in this order
| name | signed_by |
|---|---|
| Carol | 1 |
| Grace | 1 |
| Mark | 1 |
| Paige | 1 |
| Nicole | 2 |
| Rachel | 2 |
| Frank | 3 |
| Lydia | 3 |
| Samuel | 3 |
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 name, boss_id
FROM employees
ORDER BY boss_id, name;It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.