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.

ColumnTypeDescription
reader_id (key)INTEGERUnique id
handleTEXTPublic user name

reader_visits — One row per visit to the site by a signed-in reader.

ColumnTypeDescription
visit_id (key)INTEGERUnique id (not chronological)
reader_idINTEGERReader who visited
visited_atTEXTVisit start, 'YYYY-MM-DD HH:MM:SS'

Example

readers

reader_idhandle
1inkwell
2morningpaper
3quietfox
4by_the_bay

reader_visits

visit_idreader_idvisited_at
1012026-02-03 08:15:00
1122026-02-03 22:40:00
1212026-02-01 23:59:00
1342026-01-28 07:00:00
1422026-02-05 09:00:00
1512026-02-02 00:01:00

Expected output

reader_idfirst_visit_date
12026-02-01
22026-02-03
3NULL
42026-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

Start from readers, not visits, so that readers with no visits still produce a row.
ISO timestamps sort correctly as text, so MIN(visited_at) is the earliest visit.
Wrap the result in date(...) to drop the time part.

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.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url