Watch 2 video solutions for Second Day Verification, a easy level problem involving Database. This walkthrough by Everyday Data Science has 842 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: emails
+-------------+----------+ | Column Name | Type | +-------------+----------+ | email_id | int | | user_id | int | | signup_date | datetime | +-------------+----------+ (email_id, user_id) is the primary key (combination of columns with unique values) for this table. Each row of this table contains the email ID, user ID, and signup date.
Table: texts
+---------------+----------+
| Column Name | Type |
+---------------+----------+
| text_id | int |
| email_id | int |
| signup_action | enum |
| action_date | datetime |
+---------------+----------+
(text_id, email_id) is the primary key (combination of columns with unique values) for this table.
signup_action is an enum type of ('Verified', 'Not Verified').
Each row of this table contains the text ID, email ID, signup action, and action date.
Write a Solution to find the user IDs of those who verified their sign-up on the second day.
Return the result table ordered by user_id in ascending order.
The result format is in the following example.
Example:
Input:
emails table:
+----------+---------+---------------------+ | email_id | user_id | signup_date | +----------+---------+---------------------+ | 125 | 7771 | 2022-06-14 09:30:00| | 433 | 1052 | 2022-07-09 08:15:00| | 234 | 7005 | 2022-08-20 10:00:00| +----------+---------+---------------------+
texts table:
+---------+----------+--------------+---------------------+
| text_id | email_id | signup_action| action_date |
+---------+----------+--------------+---------------------+
| 1 | 125 | Verified | 2022-06-15 08:30:00|
| 2 | 433 | Not Verified | 2022-07-10 10:45:00|
| 4 | 234 | Verified | 2022-08-21 09:30:00|
+---------+----------+--------------+---------------------+
Output:
+---------+ | user_id | +---------+ | 7005 | | 7771 | +---------+
Explanation:
Problem Overview: The task is to identify users who completed their verification exactly on the second day after their initial signup. You are given two tables representing different events (such as registration and verification). The goal is to connect those records and filter the cases where the verification date occurs one day after the signup date.
Approach 1: Correlated Subquery (O(n log n) time, O(1) extra space)
A straightforward way is to query the signup table and check for a matching verification event using a correlated subquery. For each user record, the query searches the verification table to see if a verification exists exactly one day after the signup date. This works by applying a DATE_ADD or equivalent date comparison inside the subquery. While simple to write, the database may execute the subquery repeatedly for each row unless indexes are present, which increases runtime on large datasets. This approach is useful when datasets are small or when writing quick exploratory queries.
Approach 2: Joining Two Tables (O(n log n) time with indexes, O(1) extra space)
The more efficient and cleaner approach uses an explicit JOIN between the signup table and the verification table. Join the tables on the user identifier (for example user_id) and then filter rows where the verification date equals the signup date plus one day. The join allows the database optimizer to use indexes and execute the lookup in a single relational operation rather than repeated subqueries. The key step is the date comparison condition such as verification_date = DATE_ADD(signup_date, INTERVAL 1 DAY). This pattern is common in database interview questions that test your ability to combine event tables and reason about time differences.
Because the problem only requires matching records across two datasets and filtering by a date offset, the solution relies on fundamental SQL JOIN operations. The database engine handles scanning and matching rows internally, so the logical complexity remains simple while still performing well with proper indexing.
Recommended for interviews: The JOIN-based approach is the one interviewers expect. It shows that you understand how relational tables interact and how to express event relationships with SQL conditions. Mentioning the subquery alternative demonstrates awareness of multiple query styles, but the JOIN solution is cleaner, easier for query planners to optimize, and aligns with common production SQL patterns.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Correlated Subquery | O(n log n) with indexes | O(1) | Small datasets or quick exploratory SQL queries |
| JOIN Between Tables | O(n log n) with indexed joins | O(1) | Preferred approach in interviews and production SQL queries |