Quick Overview

Write a SQL query that reports each user's earliest login date and latest logout date from an activity log, keeping only users who have both. It tests aggregating one activity type at a time within a group, filtering groups with HAVING, and ordering by a key that is not in the output.

Report each user's first login date and last logout date

Company: Snapchat

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

For each user, return `username`, `first_active_date` (the earliest `activity_date` with `activity_type = 'login'`) and `last_inactive_date` (the latest `activity_date` with `activity_type = 'logout'`). Include only users with at least one login and at least one logout. Order by `user_id` ascending. ```hint Where to start Each user's group holds both login and logout rows. Think about how one aggregate can look at only the rows of one type while the other aggregate looks at the other type. ``` ### Input Table(s) `users` | Column | Type | Meaning | |---|---|---| | user_id | INT | User identifier, primary key | | username | VARCHAR(50) | The user's name, NOT NULL | `activity_log` | Column | Type | Meaning | |---|---|---| | log_id | INT | Log row identifier, primary key | | user_id | INT | The user who acted, foreign key to `users.user_id` | | activity_type | VARCHAR(10) | `'login'` or `'logout'`, NOT NULL | | activity_date | DATE | Day of the activity, NOT NULL | Keys: `users.user_id` and `activity_log.log_id` are primary keys. `activity_log.user_id` references `users.user_id`. ### Output Contract | Column | Type | Meaning | |---|---|---| | username | VARCHAR | The user's `username` | | first_active_date | DATE | Earliest `activity_date` among the user's `'login'` rows | | last_inactive_date | DATE | Latest `activity_date` among the user's `'logout'` rows | Return one row per user who has at least one login and at least one logout, ordered by `user_id` ascending. `user_id` is not an output column, but it is unique, so the order is fully determined. No rounding applies. ### Example `users` | user_id | username | |---|---| | 1 | alice | | 2 | bob | | 3 | carol | | 4 | dave | | 5 | erin | `activity_log` | log_id | user_id | activity_type | activity_date | |---|---|---|---| | 1 | 1 | login | 2024-01-05 | | 2 | 1 | logout | 2024-01-05 | | 3 | 1 | login | 2024-01-02 | | 4 | 1 | logout | 2024-01-09 | | 5 | 2 | login | 2024-01-03 | | 6 | 3 | logout | 2024-01-04 | | 7 | 3 | login | 2024-01-06 | | 8 | 4 | logout | 2024-01-06 | Expected output: | username | first_active_date | last_inactive_date | |---|---|---| | alice | 2024-01-02 | 2024-01-09 | | carol | 2024-01-06 | 2024-01-04 | Alice's earliest login is 2024-01-02 and her latest logout is 2024-01-09. Carol's only logout comes before her only login, and both dates are still reported. Bob has no logout, Dave has no login and Erin has no activity, so none of them appear. ### Constraints and Clarifications - `activity_log` has at most ${10^4}$ rows. - `activity_type` is always `'login'` or `'logout'`, and no column holds NULL. - Every `activity_log.user_id` references a valid user. - A user can log in or out many times, including several times on the same date. Only the earliest login date and the latest logout date matter. - The two dates are computed independently. Logins are not paired with logouts, so `last_inactive_date` may be earlier than `first_active_date`. - `username` is not declared unique. Two users with the same name produce two separate rows.

Overview: Write a SQL query that reports each user's earliest login date and latest logout date from an activity log, keeping only users who have both. It tests aggregating one activity type at a time within a group, filtering groups with HAVING, and ordering by a key that is not in the output.

Table `users` has columns `user_id` (INTEGER, primary key) and `username` (VARCHAR(50), NOT NULL). Table `activity_log` has columns `log_id` (INTEGER, primary key), `user_id` (INTEGER, NOT NULL, references `users.user_id`), `activity_type` (VARCHAR(10), NOT NULL, always `'login'` or `'logout'`) and `activity_date` (DATE, NOT NULL). For each user, return `username`, `first_active_date` (the earliest `activity_date` with `activity_type = 'login'`) and `last_inactive_date` (the latest `activity_date` with `activity_type = 'logout'`). Include only users with at least one login and at least one logout. Order by `user_id` ascending. Return exactly the columns `username`, `first_active_date` and `last_inactive_date`, in that order, with one row per user who has at least one login and at least one logout. `user_id` is not an output column, but it is unique, so the order is fully determined. No rounding applies. Clarifications: - `activity_log` has at most 10,000 rows. - `activity_type` is always `'login'` or `'logout'`, and no column holds NULL. - Every `activity_log.user_id` references a valid user. - A user can log in or out many times, including several times on the same date. Only the earliest login date and the latest logout date matter. - The two dates are computed independently. Logins are not paired with logouts, so `last_inactive_date` may be earlier than `first_active_date`. - `username` is not declared unique. Two users with the same name produce two separate rows.

Tables

users(user_id INTEGER, username VARCHAR(50))

activity_log(log_id INTEGER, user_id INTEGER, activity_type VARCHAR(10), activity_date DATE)

Hints

  1. Each user's group holds both login and logout rows. Think about how one aggregate can look at only the rows of one type while the other aggregate looks at the other type.
  2. Two users can share a username, so build each group from the user's id and keep the name only for display.
SnapchatData ScientisteasyData Manipulation (SQL/Python)

First Login and Last Logout per User

Table users has columns user_id (INTEGER, primary key) and username (VARCHAR(50), NOT NULL). Table activity_log has columns log_id (INTEGER, primary key), user_id (INTEGER, NOT NULL, references users.user_id), activity_type (VARCHAR(10), NOT NULL, always 'login' or 'logout') and activity_date (DATE, NOT NULL).

For each user, return username, first_active_date (the earliest activity_date with activity_type = 'login') and last_inactive_date (the latest activity_date with activity_type = 'logout'). Include only users with at least one login and at least one logout. Order by user_id ascending.

Return exactly the columns username, first_active_date and last_inactive_date, in that order, with one row per user who has at least one login and at least one logout. user_id is not an output column, but it is unique, so the order is fully determined. No rounding applies.

Clarifications:

  • activity_log has at most 10,000 rows.
  • activity_type is always 'login' or 'logout', and no column holds NULL.
  • Every activity_log.user_id references a valid user.
  • A user can log in or out many times, including several times on the same date. Only the earliest login date and the latest logout date matter.
  • The two dates are computed independently. Logins are not paired with logouts, so last_inactive_date may be earlier than first_active_date.
  • username is not declared unique. Two users with the same name produce two separate rows.

Expected Output

Tables

usersactivity_log
users
ColumnType
user_idINTEGER
usernameVARCHAR(50)
activity_log
ColumnType
log_idINTEGER
user_idINTEGER
activity_typeVARCHAR(10)
activity_dateDATE

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions