First Visit Date per Reader
A news site is studying how long registered readers take to come back after their first visit. As a first step it needs each reader's first visit date.
Return one row for every registered reader with reader_id and first_visit_date: the calendar date (text 'YYYY-MM-DD', no time part) of their earliest visit. Readers who have never visited get NULL. Note that visit_id values are not in time order, because old visits were imported later.
Order by reader_id. No registered readers means no rows.
Tables
readers — One row per registered reader account.
| Column | Type | Description |
|---|---|---|
reader_id (key) | INTEGER | Unique id |
handle | TEXT | Public user name |
reader_visits — One row per visit to the site by a signed-in reader.
| Column | Type | Description |
|---|---|---|
visit_id (key) | INTEGER | Unique id (not chronological) |
reader_id | INTEGER | Reader who visited |
visited_at | TEXT | Visit start, 'YYYY-MM-DD HH:MM:SS' |
Example
readers
| reader_id | handle |
|---|---|
| 1 | inkwell |
| 2 | morningpaper |
| 3 | quietfox |
| 4 | by_the_bay |
reader_visits
| visit_id | reader_id | visited_at |
|---|---|---|
| 10 | 1 | 2026-02-03 08:15:00 |
| 11 | 2 | 2026-02-03 22:40:00 |
| 12 | 1 | 2026-02-01 23:59:00 |
| 13 | 4 | 2026-01-28 07:00:00 |
| 14 | 2 | 2026-02-05 09:00:00 |
| 15 | 1 | 2026-02-02 00:01:00 |
Expected output
| reader_id | first_visit_date |
|---|---|
| 1 | 2026-02-01 |
| 2 | 2026-02-03 |
| 3 | NULL |
| 4 | 2026-01-28 |
Return the rows in exactly the order described. Your query also runs against hidden datasets with tricky edge cases.
SQL Path · Dates & Time · problem 45 of 70 · SQLite
The idea
Because ISO-8601 timestamps such as '2026-02-01 23:59:00' sort as text in chronological order, the earliest visit is simply MIN(visited_at). Wrapping it in date(...) keeps only the calendar date the statement asks for.
The output needs every reader, so the query starts from readers and LEFT JOINs the visits. A reader with no visits contributes one row whose visit columns are all NULL, and MIN of nothing but NULL is NULL, which is exactly the required output.
Do not rely on visit_id to tell you which visit came first. Ids reflect insertion order, and imports or backfills routinely break the link between id order and time order.
The alternative uses a correlated subquery per reader that sorts that reader's visits by time and takes the first one. A scalar subquery with no row also returns NULL.
Solution
SELECT r.reader_id,
date(MIN(v.visited_at)) AS first_visit_date
FROM readers AS r
LEFT JOIN reader_visits AS v ON v.reader_id = r.reader_id
GROUP BY r.reader_id
ORDER BY r.reader_id;
Another way
SELECT r.reader_id,
(SELECT date(v.visited_at)
FROM reader_visits AS v
WHERE v.reader_id = r.reader_id
ORDER BY v.visited_at
LIMIT 1) AS first_visit_date
FROM readers AS r
ORDER BY r.reader_id;
Why common answers fail
Returns the full timestamp of the first visit instead of just its calendar date.
SELECT r.reader_id, MIN(v.visited_at) AS first_visit_date
FROM readers AS r
LEFT JOIN reader_visits AS v ON v.reader_id = r.reader_id
GROUP BY r.reader_id
ORDER BY r.reader_id;
Reads only the visits table, so readers who never visited are missing instead of shown with NULL.
SELECT v.reader_id, date(MIN(v.visited_at)) AS first_visit_date
FROM reader_visits AS v
GROUP BY v.reader_id
ORDER BY v.reader_id;
Treats the smallest visit_id as the earliest visit, but ids are not in time order.
SELECT r.reader_id,
(SELECT date(v.visited_at) FROM reader_visits AS v
WHERE v.reader_id = r.reader_id
ORDER BY v.visit_id LIMIT 1) AS first_visit_date
FROM readers AS r
ORDER BY r.reader_id;
What the interviewer is checking
Checks that you pick the right driving table for "every user" questions and that you order by time rather than by surrogate ids.
Mistakes to avoid
- Grouping only the visits table and losing readers with no visits.
- Using the smallest id as a proxy for the earliest time.
- Returning the full timestamp when a date was asked for.
Follow-ups to expect
- Count how many readers had their first visit on each date (group the result by first_visit_date).
- Return the full first visit row, not just its date (ROW_NUMBER() OVER (PARTITION BY reader_id ORDER BY visited_at) = 1).
- Days between first and second visit (LEAD over each reader's visits, then julianday difference).
In real work
First-touch dates drive acquisition cohorts, onboarding funnels and "new versus returning" splits in nearly every product analytics stack.