You are given two tables: Accounts and Logins.
The Accounts table contains account information:
id(int): The primary key representing the account IDname(varchar): The user name of each account
The Logins table contains login records:
id(int): The account ID of the user who logged inlogin_date(date): The date when the login occurred
Note: The Logins table may contain duplicates as a user can log in multiple times in the same day.
Write an SQL query to find the id and name of active users.
Active users are defined as users who logged in to their accounts for 5 or more consecutive days.
Return the result table ordered by the id.
Table Schema
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Primary key, account identifier |
name
|
varchar | User name for the account |
| Column Name | Type | Description |
|---|---|---|
id
|
int | Account ID (foreign key to Accounts) |
login_date
|
date | Date when the user logged in |
Input & Output
| id | name |
|---|---|
| 1 | Winston |
| 7 | Jonathan |
| id | login_date |
|---|---|
| 7 | 2020-05-30 |
| 1 | 2020-05-30 |
| 7 | 2020-05-31 |
| 7 | 2020-06-01 |
| 7 | 2020-06-02 |
| 7 | 2020-06-02 |
| 7 | 2020-06-03 |
| 1 | 2020-06-07 |
| 7 | 2020-06-10 |
| id | name |
|---|---|
| 7 | Jonathan |
Jonathan (id=7) logged in on 5 consecutive days: 2020-05-30, 2020-05-31, 2020-06-01, 2020-06-02, and 2020-06-03. Note that duplicate logins on the same day (2020-06-02) are counted as one day. Winston (id=1) only logged in on 2 non-consecutive days, so he doesn't qualify as an active user.
| id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| id | login_date |
|---|---|
| 1 | 2020-01-01 |
| 1 | 2020-01-02 |
| 1 | 2020-01-04 |
| 2 | 2020-01-01 |
| 2 | 2020-01-03 |
| id | name |
|---|
Neither Alice nor Bob has 5 consecutive login days. Alice logged in on 2020-01-01, 2020-01-02, and 2020-01-04 (gap on 2020-01-03). Bob only logged in on 2 non-consecutive days. Therefore, no users qualify as active users.
Constraints
-
1 ≤ Accounts.id ≤ 100 -
1 ≤ Logins.id ≤ 100 -
1 ≤ Accounts.name.length ≤ 10 -
login_dateis a valid date between2020-01-01and2020-12-31