Code RoomPrevious order date
HardPrep Room Coding #5071

Previous order date

SQLAlgorithms & data structuresSenior~13 min

For every order, return `id`, `placed_on` and `prev_placed_on`: the date of that customer's previous order, or NULL for their first. Order by customer then date.

What a strong answer looks like

Say what the query is meant to return before you write it, and name the shape of the answer: how many rows, grouped by what. Then watch the NULLs and the duplicates, because that is where most of these are lost.

Run onlyA query runs as one statement, so there is nothing to step through. Run it and read the rows.

0:00 of about 13 min

Expected, in this order

idplaced_onprev_placed_on
12026-01-04NULL
22026-01-192026-01-04
72026-03-032026-01-19
32026-01-21NULL
42026-02-022026-01-21
62026-02-282026-02-02

1 more row

Your result

Run your query to compare.

1 more set of rows are waiting. Free with an account.