Problem · Database
Most Recent Active User Per Department
Problem statement
The table activity_logs stores user-activity records in input order. For each department, return the user from the row with the largest last_seen timestamp.
- If several rows in one department share its largest timestamp, keep the row with the smallest
log_position. - Order departments by the smallest
log_positionat which each department appears.
Return department and user_id. The reported exercise formats these ordered pairs as a semicolon-delimited string; the tabular workspace represents the same pairs as ordered result rows.
Input table
| Column | Meaning |
|---|---|
log_position | One-based position of the record in the input |
department | Department name |
user_id | User identifier |
last_seen | Activity timestamp |
Table schema
MySQLPostgreSQLPandas
Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.
activity_logs
One row per activity record, with explicit input order.
| Column | Type | Nullable | Description |
|---|---|---|---|
| log_positionPK | Integer | No | — |
| department | Text | No | — |
| user_id | Text | No | — |
| last_seen | Integer | No | — |
Expected result
Your query or function must return these columns.
| Column | Type | Nullable | Description |
|---|---|---|---|
| department | Text | No | — |
| user_id | Text | No | — |
Row order: must match exactly. Numeric tolerance: 0.
Constraints
- The
activity_logstable has between1and10^5rows. log_positionis unique, non-null, and records the input order.departmentanduser_idare non-null strings of length from1through50.0 <= last_seen <= 10^9.