Temporary email TTL status and message counts at a reference time

Quick Overview

Write a SQL query that reports each temporary email address's TTL status at a fixed reference time, along with how many messages it received while it was valid and when the last one arrived. It tests where filters go in an outer join, aggregation with COALESCE, and careful timestamp boundaries.

Temporary email TTL status and message counts at a reference time

Company: Confluent

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A temporary email service gives every address a fixed lifetime: each address expires exactly 5 hours after it is created. The service also logs every message delivered to an address. For each temporary email, report its status as of the reference time `2024-01-15 12:00:00` and summarize the messages it received while it was still valid: how many there were and when the last one arrived. Every email must appear in the output, including emails that never received a qualifying message. ```hint Keep every email Emails with no qualifying messages still need a row. Think about what a condition on the messages table does to an outer join when it sits in `WHERE` instead of `ON`. ``` ### Input Table(s) `temp_emails` | Column | Type | Meaning | |---|---|---| | email_id | INT | Primary key | | email_address | VARCHAR | The temporary address (unique) | | created_at | TIMESTAMP | When the address was created | | expires_at | TIMESTAMP | When the address stops being valid, exactly 5 hours after `created_at` | Primary key: `email_id`. `email_address` is unique. `messages` | Column | Type | Meaning | |---|---|---| | message_id | INT | Primary key | | email_id | INT | The address that received the message, foreign key to `temp_emails.email_id` | | received_at | TIMESTAMP | When the message arrived | Primary key: `message_id`. Every `email_id` references an existing row in `temp_emails`. ### Output Contract One row per row of `temp_emails`, with exactly these columns in this order. The mixed-case names are part of the contract, so quote them in PostgreSQL. | Column | Value | |---|---| | email_id | `temp_emails.email_id` | | Email | `temp_emails.email_address` | | CreatedAt | `temp_emails.created_at` | | ExpiresAt | `temp_emails.expires_at` | | Status | `'Active'` when `expires_at` is later than the reference time, otherwise `'Expired'` | | MessageCount | Number of qualifying messages, 0 when there are none | | LastMessageAt | Latest `received_at` among qualifying messages, NULL when there are none | A message qualifies when it arrived while its email was valid and no later than the reference time, that is `created_at <= received_at < expires_at` and `received_at <= '2024-01-15 12:00:00'`. Order the rows by `expires_at` descending, then `email_id` ascending. No rounding is involved. ### Example `temp_emails` | email_id | email_address | created_at | expires_at | |---|---|---|---| | 1 | a1@tmp.io | 2024-01-15 05:00:00 | 2024-01-15 10:00:00 | | 2 | b2@tmp.io | 2024-01-15 09:30:00 | 2024-01-15 14:30:00 | | 3 | c3@tmp.io | 2024-01-15 07:00:00 | 2024-01-15 12:00:00 | | 4 | d4@tmp.io | 2024-01-15 11:00:00 | 2024-01-15 16:00:00 | `messages` | message_id | email_id | received_at | |---|---|---| | 101 | 1 | 2024-01-15 06:15:00 | | 102 | 1 | 2024-01-15 09:59:00 | | 103 | 1 | 2024-01-15 10:30:00 | | 104 | 2 | 2024-01-15 10:00:00 | | 105 | 2 | 2024-01-15 12:45:00 | | 106 | 3 | 2024-01-15 11:59:59 | | 107 | 3 | 2024-01-15 12:00:00 | Expected result: | email_id | Email | CreatedAt | ExpiresAt | Status | MessageCount | LastMessageAt | |---|---|---|---|---|---|---| | 4 | d4@tmp.io | 2024-01-15 11:00:00 | 2024-01-15 16:00:00 | Active | 0 | NULL | | 2 | b2@tmp.io | 2024-01-15 09:30:00 | 2024-01-15 14:30:00 | Active | 1 | 2024-01-15 10:00:00 | | 3 | c3@tmp.io | 2024-01-15 07:00:00 | 2024-01-15 12:00:00 | Expired | 1 | 2024-01-15 11:59:59 | | 1 | a1@tmp.io | 2024-01-15 05:00:00 | 2024-01-15 10:00:00 | Expired | 2 | 2024-01-15 09:59:00 | Email 1 expired at 10:00. Messages 101 and 102 arrived inside its window, while 103 arrived at 10:30, after expiry, so it is ignored even though it is earlier than the reference time. Email 2 is still active; message 104 counts and message 105 arrived after the reference time. Email 3 expires exactly at the reference time, so it is `Expired`, and message 107 arrived at the moment of expiry, so only 106 counts. Email 4 has no messages at all. ### Constraints and Clarifications - `temp_emails` has at most 10,000 rows and `messages` has at most 100,000 rows. - `expires_at` is always exactly 5 hours after `created_at`. - No column in either table is NULL. - An email whose `expires_at` equals the reference time is `Expired`. A message received exactly at `expires_at` does not qualify. - Messages received after the reference time are ignored, even for an email that is still active. - Messages received after an email expired, or timestamped before it was created, are ignored. - Several messages can share the same `received_at`; each one counts. - Use the fixed reference time `2024-01-15 12:00:00`, never the current clock.

Overview: Write a SQL query that reports each temporary email address's TTL status at a fixed reference time, along with how many messages it received while it was valid and when the last one arrived. It tests where filters go in an outer join, aggregation with COALESCE, and careful timestamp boundaries.

Confluent logo
Confluent
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

A temporary email service gives every address a fixed lifetime: each address expires exactly 5 hours after it is created. The service also logs every message delivered to an address.

For each temporary email, report its status as of the reference time 2024-01-15 12:00:00 and summarize the messages it received while it was still valid: how many there were and when the last one arrived. Every email must appear in the output, including emails that never received a qualifying message.

Input Table(s)

temp_emails

ColumnTypeMeaning
email_idINTPrimary key
email_addressVARCHARThe temporary address (unique)
created_atTIMESTAMPWhen the address was created
expires_atTIMESTAMPWhen the address stops being valid, exactly 5 hours after created_at

Primary key: email_id. email_address is unique.

messages

ColumnTypeMeaning
message_idINTPrimary key
email_idINTThe address that received the message, foreign key to temp_emails.email_id
received_atTIMESTAMPWhen the message arrived

Primary key: message_id. Every email_id references an existing row in temp_emails.

Output Contract

One row per row of temp_emails, with exactly these columns in this order. The mixed-case names are part of the contract, so quote them in PostgreSQL.

ColumnValue
email_idtemp_emails.email_id
Emailtemp_emails.email_address
CreatedAttemp_emails.created_at
ExpiresAttemp_emails.expires_at
Status'Active' when expires_at is later than the reference time, otherwise 'Expired'
MessageCountNumber of qualifying messages, 0 when there are none
LastMessageAtLatest received_at among qualifying messages, NULL when there are none

A message qualifies when it arrived while its email was valid and no later than the reference time, that is created_at <= received_at < expires_at and received_at <= '2024-01-15 12:00:00'.

Order the rows by expires_at descending, then email_id ascending. No rounding is involved.

Example

temp_emails

email_idemail_addresscreated_atexpires_at
1a1@tmp.io2024-01-15 05:00:002024-01-15 10:00:00
2b2@tmp.io2024-01-15 09:30:002024-01-15 14:30:00
3c3@tmp.io2024-01-15 07:00:002024-01-15 12:00:00
4d4@tmp.io2024-01-15 11:00:002024-01-15 16:00:00

messages

message_idemail_idreceived_at
10112024-01-15 06:15:00
10212024-01-15 09:59:00
10312024-01-15 10:30:00
10422024-01-15 10:00:00
10522024-01-15 12:45:00
10632024-01-15 11:59:59
10732024-01-15 12:00:00

Expected result:

email_idEmailCreatedAtExpiresAtStatusMessageCountLastMessageAt
4d4@tmp.io2024-01-15 11:00:002024-01-15 16:00:00Active0NULL
2b2@tmp.io2024-01-15 09:30:002024-01-15 14:30:00Active12024-01-15 10:00:00
3c3@tmp.io2024-01-15 07:00:002024-01-15 12:00:00Expired12024-01-15 11:59:59
1a1@tmp.io2024-01-15 05:00:002024-01-15 10:00:00Expired22024-01-15 09:59:00

Email 1 expired at 10:00. Messages 101 and 102 arrived inside its window, while 103 arrived at 10:30, after expiry, so it is ignored even though it is earlier than the reference time. Email 2 is still active; message 104 counts and message 105 arrived after the reference time. Email 3 expires exactly at the reference time, so it is Expired, and message 107 arrived at the moment of expiry, so only 106 counts. Email 4 has no messages at all.

Constraints and Clarifications

  • temp_emails has at most 10,000 rows and messages has at most 100,000 rows.
  • expires_at is always exactly 5 hours after created_at .
  • No column in either table is NULL.
  • An email whose expires_at equals the reference time is Expired . A message received exactly at expires_at does not qualify.
  • Messages received after the reference time are ignored, even for an email that is still active.
  • Messages received after an email expired, or timestamped before it was created, are ignored.
  • Several messages can share the same received_at ; each one counts.
  • Use the fixed reference time 2024-01-15 12:00:00 , never the current clock.