AnalystPath

Unfold Shift Readings

SQLEasyJunior level~15 min

Problem

A factory logs each machine's output reading for three daily shifts in a wide table.

```
ShiftLog
+------------+------+
| Column | Type |
+------------+------+
| machine_id | int |
| morning | int |
| afternoon | int |
| night | int |
+------------+------+
machine_id is the primary key.
Each column holds the reading for that shift, or NULL if the machine was idle that shift.
```

Reshape the table into one row per `(machine_id, shift, reading)`. Do **not** emit a row when the reading for a shift is NULL.

Return the result in any order.

Tables

Example rows — the live problem includes the full dataset.

ShiftLog
machine_idmorningafternoonnight
095100105
17080

Expected output

Your answer should return 5 rows with the columns machine_id, shift, reading.

Starter code (SQL)

SELECT *
FROM ShiftLog;

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