Retrieve chat messages sent by active channel members
Company: Square
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
A team chat app keeps its data in four tables: `users`, `channels`, `memberships` and `messages`. A user can belong to many channels, and a membership can be deactivated without deleting its row.
Return all messages whose sender is an active member (`memberships.is_active = 1`) of the message's channel. Output columns `message_id`, `Username`, `Channel`, `Message`, `SentAt`, `IsEdited`, ordered by `channel_name`, then `sent_at`, then `message_id`, all ascending.
```hint Which membership row counts
Being active in some other channel does not count. Work out which two values pick out the single membership row that decides whether a given message is returned.
```
### Input Table(s)
`users` (primary key `user_id`)
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | User identifier |
| username | VARCHAR | User's display name |
`channels` (primary key `channel_id`)
| Column | Type | Meaning |
|---|---|---|
| channel_id | INT | Channel identifier |
| channel_name | VARCHAR | Channel name |
| channel_type | VARCHAR | One of 'public', 'private', 'direct' |
`memberships` (unique key `(user_id, channel_id)`)
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | Member; references `users.user_id` |
| channel_id | INT | Channel; references `channels.channel_id` |
| is_active | INT | 1 if the user is currently an active member of the channel, 0 if not |
`messages` (primary key `message_id`)
| Column | Type | Meaning |
|---|---|---|
| message_id | INT | Message identifier |
| channel_id | INT | Channel the message was posted in; references `channels.channel_id` |
| user_id | INT | Sender; references `users.user_id` |
| content | TEXT | Message text |
| sent_at | TIMESTAMP | When the message was sent |
| is_edited | INT | 1 if the message was edited after sending, 0 if not |
### Output Contract
- One row per qualifying message, with these columns in this order: `message_id`, `Username` (from `users.username`), `Channel` (from `channels.channel_name`), `Message` (from `messages.content`), `SentAt` (from `messages.sent_at`), `IsEdited` (from `messages.is_edited`).
- Column names are case-sensitive exactly as written. In PostgreSQL the mixed-case aliases need double quotes.
- Order by `channel_name` ascending, then `sent_at` ascending, then `message_id` ascending. `message_id` is unique, so the order is fully determined.
- Return values as stored, with no rounding or reformatting.
### Example
`users`
| user_id | username |
|---|---|
| 1 | ana |
| 2 | ben |
| 3 | chloe |
`channels`
| channel_id | channel_name | channel_type |
|---|---|---|
| 10 | general | public |
| 20 | design | private |
| 30 | ana-ben | direct |
`memberships`
| user_id | channel_id | is_active |
|---|---|---|
| 1 | 10 | 1 |
| 2 | 10 | 1 |
| 3 | 10 | 0 |
| 1 | 20 | 1 |
| 1 | 30 | 1 |
| 2 | 30 | 1 |
`messages`
| message_id | channel_id | user_id | content | sent_at | is_edited |
|---|---|---|---|---|---|
| 101 | 10 | 1 | Morning all | 2024-03-01 09:00:00 | 0 |
| 102 | 10 | 3 | Running late | 2024-03-01 09:02:00 | 0 |
| 103 | 10 | 2 | Standup moved to 10 | 2024-03-01 09:05:00 | 1 |
| 104 | 20 | 1 | Draft mockups attached | 2024-03-01 11:00:00 | 0 |
| 105 | 20 | 2 | Looks good | 2024-03-01 11:30:00 | 0 |
| 106 | 30 | 2 | Lunch? | 2024-03-01 12:00:00 | 0 |
| 107 | 10 | 1 | Thanks | 2024-03-01 09:05:00 | 0 |
Expected output:
| message_id | Username | Channel | Message | SentAt | IsEdited |
|---|---|---|---|---|---|
| 106 | ben | ana-ben | Lunch? | 2024-03-01 12:00:00 | 0 |
| 104 | ana | design | Draft mockups attached | 2024-03-01 11:00:00 | 0 |
| 101 | ana | general | Morning all | 2024-03-01 09:00:00 | 0 |
| 103 | ben | general | Standup moved to 10 | 2024-03-01 09:05:00 | 1 |
| 107 | ana | general | Thanks | 2024-03-01 09:05:00 | 0 |
Message 102 is dropped because chloe's membership in `general` is inactive. Message 105 is dropped because ben has no membership row for `design`, even though he is active in two other channels. Messages 103 and 107 were sent at the same time in `general`, so `message_id` decides their order.
### Constraints and Clarifications
- Row limits: `users` up to 10,000 rows, `channels` up to 1,000, `memberships` up to 100,000, `messages` up to 1,000,000.
- `channel_type` is one of `'public'`, `'private'`, `'direct'`. The membership rule applies to every channel type, direct channels included.
- `is_active` and `is_edited` are `0` or `1`.
- Each `(user_id, channel_id)` pair in `memberships` is unique, so a message matches at most one membership row.
- A sender with no membership row for the message's channel is not an active member, and the message is excluded.
- `is_active` is the member's current status. A message from a user whose membership is now inactive is excluded, whenever it was sent.
- None of the columns above contain NULLs, and every `user_id` and `channel_id` in `messages` exists in `users` and `channels`.
Overview: Write a SQL query that returns chat messages only when the sender is an active member of the channel the message was posted in. It tests joining four tables, matching a membership row on two columns at once, and ordering results with a unique tiebreaker.