AnalystPath

Interleave Volunteers by Shift Type

SQLMediumMid level~15 min

Problem

A community kitchen tracks its volunteers and which shift type each one prefers. Table `roster` has columns `(volunteer_id, shift)` where `shift` is one of `'morning'`, `'evening'`, or `'overnight'`.

Reorder the rows so the shifts cycle in the fixed order `'morning'`, then `'evening'`, then `'overnight'`, repeating. Inside each shift, list volunteers by `volunteer_id` ascending. So the first three rows are the lowest-id morning, evening, and overnight volunteers; the next three are the second-lowest of each shift; and so on. If a shift runs out of volunteers, simply skip it for the remaining cycles.

Return the columns `volunteer_id` and `shift` in this interleaved order.

Tables

Example rows — the live problem includes the full dataset.

roster
volunteer_idshift
4overnight
7morning
2evening

Expected output

Your answer should return 9 rows with the columns volunteer_id, shift.

Starter code (SQL)

SELECT *
FROM roster;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions