The Latest Login in 2020 Problem
#1890
Description
LeetCode Problem 1890.
Table: Logins
+----------------+----------+
| Column Name | Type |
+----------------+----------+
| user_id | int |
| time_stamp | datetime |
+----------------+----------+
(user_id, time_stamp) is the primary key for this table.
Each row contains information about the login time for the user with ID user_id.
Write an SQL query to report the latest login for all users in the year 2020. Do not include the users who did not login in 2020.
Return the result table in any order.
The query result format is in the following example:
Logins table:
+---------+---------------------+
| user_id | time_stamp |
+---------+---------------------+
| 6 | 2020-06-30 15:06:07 |
| 6 | 2021-04-21 14:06:06 |
| 6 | 2019-03-07 00:18:15 |
| 8 | 2020-02-01 05:10:53 |
| 8 | 2020-12-30 00:46:50 |
| 2 | 2020-01-16 02:49:50 |
| 2 | 2019-08-25 07:59:08 |
| 14 | 2019-07-14 09:00:00 |
| 14 | 2021-01-06 11:59:59 |
+---------+---------------------+
Result table:
+---------+---------------------+
| user_id | last_stamp |
+---------+---------------------+
| 6 | 2020-06-30 15:06:07 |
| 8 | 2020-12-30 00:46:50 |
| 2 | 2020-01-16 02:49:50 |
+---------+---------------------+
User 6 logged into their account 3 times but only once in 2020, so we include this login in the result table.
User 8 logged into their account 2 times in 2020, once in February and once in December. We include only the latest one (December) in the result table.
User 2 logged into their account 2 times but only once in 2020, so we include this login in the result table.
User 14 did not login in 2020, so we do not include them in the result table.
Solution
Approach: Filter the Year, then MAX per User
- Keep only 2020 logins. Filter
time_stampto the half-open range>= '2020-01-01' AND < '2021-01-01'. It includes every moment of December 31 (a login at23:59:59counts), and unlikeYEAR(time_stamp) = 2020it does not wrap the column in a function, so an index ontime_stampcan still be used. - Latest login per user. Group the remaining rows by
user_idand takeMAX(time_stamp).
Filtering in WHERE before grouping is what makes this correct. Taking each user's overall latest login and then checking its year would lose user 6, whose latest login is in 2021 even though they also logged in during 2020. Users with no 2020 login (user 14) have no rows left after the filter, so they never appear.
The Pandas version applies the same date range as a boolean mask, then takes the maximum time_stamp per user_id.
SELECT user_id,
MAX(time_stamp) AS last_stamp
FROM Logins
WHERE time_stamp >= '2020-01-01'
AND time_stamp < '2021-01-01'
GROUP BY user_id;