You are working as a Data Engineer for a SaaS platform that tracks user activity. The product team wants to measure short-term engagement by identifying users who have been active recently. The platform records every user login event in a table. Your task is to filter this data to find users who have logged in within the last 7 days relative to a given reference date. Schema Summary: user_logins table: - login_id (INT) - user_id (INT) - login_time (TIMESTAMP) Requirements: Find all unique users who have logged in within the last 7 days from '2023-01-10'. Return only distinct user_id values. Sort the result by user_id in ascending order. Hints: Use a WHERE clause with a date comparison to filter recent logins. You can subtract an interval from a date using PostgreSQL syntax like '2023-01-10'::date - INTERVAL '7 days' . A common mistake is forgetting to use DISTINCT, which may result in duplicate user_ids if a user logged in multiple times.
Table Setup (SQL Schema)
CREATE TABLE user_logins (login_id INT, user_id INT, login_time TIMESTAMP);
INSERT INTO user_logins (login_id, user_id, login_time) VALUES
(1, 101, '2023-01-09 10:00:00'),
(2, 102, '2023-01-08 11:00:00'),
(3, 103, '2023-01-03 09:00:00'),
(4, 101, '2023-01-05 12:00:00'),
(5, 104, '2023-01-10 08:00:00'),
(6, 105, '2023-01-01 07:00:00');