Knowledge Guide
HomeDatabasesSQL Practice Problems

Active Users

Problem

Table: Accounts

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| name          | varchar |
+---------------+---------+
id is the primary key (column with unique values) for this table.
This table contains the account id and the user name of each account.

Table: Logins

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| login_date    | date    |
+---------------+---------+
This table may contain duplicate rows.
This table contains the account id of the user who logged in and the login date. A user may log in multiple times in the day.

Problem Definition

Active users are those who logged in to their accounts for five or more consecutive days.

Write a solution to find the id and the name of active users.

Return the result table ordered by id.

Example

Image
Image

Output

Image
Image

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To identify active users who have logged in for five or more consecutive days, the SQL solution employs a self-join and aggregation. The SELECT clause retrieves distinct account IDs (l1.id) and uses a subquery to fetch the corresponding names from the "Accounts" table. The FROM clause specifies the "Logins" table with aliases (l1 and l2) for the self-join.

The self-join is established in the JOIN clause, connecting rows where the account IDs are the same (l1.id = l2.id) and the login dates have a difference between 1 and 4 days (DATEDIFF(l2.login_date, l1.login_date) BETWEEN 1 AND 4). This ensures that the login dates are consecutive.

The GROUP BY clause is used to group the results by account ID (l1.id) and login date (l1.login_date). The HAVING clause filters the groups to include only those where the count of distinct login dates (COUNT(DISTINCT l2.login_date)) is equal to 4, indicating five or more consecutive logins.

SELECT DISTINCT l1.id, (SELECT NAME FROM accounts WHERE id = l1.id) AS NAME FROM logins l1 JOIN logins l2 ON l1.id = l2.id AND Datediff(l2.login_date, l1.login_date) BETWEEN 1 AND 4 GROUP BY l1.id, l1.login_date HAVING Count(DISTINCT l2.login_date) = 4

Let's break down the query step by step:

Step 1: Identify consecutive logins within a 4-day window

We use a self-join on the Logins table (l1 and l2) to find consecutive logins for each user where the login dates are within a 4-day window.

SELECT l1.id, (SELECT NAME FROM Accounts WHERE id = l1.id) AS NAME FROM Logins l1 JOIN Logins l2 ON l1.id = l2.id AND Datediff(l2.login_date, l1.login_date) BETWEEN 1 AND 4;

Output After Step 1:

+----+----------+ | id | name | +----+----------+ | 7 | Jonathan | | 7 | Jonathan | | 7 | Jonathan | | 7 | Jonathan | | 7 | Jonathan | | 7 | Jonathan | | 7 | Jonathan | | 7 | Jonathan | | 7 | Jonathan | | 7 | Jonathan | | 7 | Jonathan | | 7 | Jonathan | | 7 | Jonathan | | 7 | Jonathan | +----+----------+

Step 2: Group by user and login date, filter for users with consecutive logins on 4 different days

We group the results from Step 1 by user and login date and filter out users who don't have logins on 4 different days.

SELECT l1.id, (SELECT NAME FROM Accounts WHERE id = l1.id) AS NAME, l1.login_date FROM Logins l1 JOIN Logins l2 ON l1.id = l2.id AND Datediff(l2.login_date, l1.login_date) BETWEEN 1 AND 4 GROUP BY l1.id, l1.login_date HAVING Count(DISTINCT l2.login_date) = 4;

Output After Step 2:

+----+----------+ | id | name | +----+----------+ | 7 | Jonathan | +----+----------+

Step 3: Get unique active users

We use DISTINCT to get unique active users from the filtered result.

SELECT DISTINCT id, NAME FROM ( SELECT l1.id, (SELECT NAME FROM Accounts WHERE id = l1.id) AS NAME, l1.login_date FROM Logins l1 JOIN Logins l2 ON l1.id = l2.id AND Datediff(l2.login_date, l1.login_date) BETWEEN 1 AND 4 GROUP BY l1.id, l1.login_date HAVING Count(DISTINCT l2.login_date) = 4 ) active_users;

Final Output:

+----+----------+ | id | name | +----+----------+ | 7 | Jonathan | +----+----------+
🤖 Don't fully get this? Learn it with Claude

Stuck on Active Users? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Active Users** (Databases) and want to truly understand it. Explain Active Users from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Active Users** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Active Users** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Active Users** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes