Retrieve chat messages sent by active channel members

Quick 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.

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.

Square logo
Square
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

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.

Input Table(s)

users (primary key user_id)

ColumnTypeMeaning
user_idINTUser identifier
usernameVARCHARUser's display name

channels (primary key channel_id)

ColumnTypeMeaning
channel_idINTChannel identifier
channel_nameVARCHARChannel name
channel_typeVARCHAROne of 'public', 'private', 'direct'

memberships (unique key (user_id, channel_id))

ColumnTypeMeaning
user_idINTMember; references users.user_id
channel_idINTChannel; references channels.channel_id
is_activeINT1 if the user is currently an active member of the channel, 0 if not

messages (primary key message_id)

ColumnTypeMeaning
message_idINTMessage identifier
channel_idINTChannel the message was posted in; references channels.channel_id
user_idINTSender; references users.user_id
contentTEXTMessage text
sent_atTIMESTAMPWhen the message was sent
is_editedINT1 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_idusername
1ana
2ben
3chloe

channels

channel_idchannel_namechannel_type
10generalpublic
20designprivate
30ana-bendirect

memberships

user_idchannel_idis_active
1101
2101
3100
1201
1301
2301

messages

message_idchannel_iduser_idcontentsent_atis_edited
101101Morning all2024-03-01 09:00:000
102103Running late2024-03-01 09:02:000
103102Standup moved to 102024-03-01 09:05:001
104201Draft mockups attached2024-03-01 11:00:000
105202Looks good2024-03-01 11:30:000
106302Lunch?2024-03-01 12:00:000
107101Thanks2024-03-01 09:05:000

Expected output:

message_idUsernameChannelMessageSentAtIsEdited
106benana-benLunch?2024-03-01 12:00:000
104anadesignDraft mockups attached2024-03-01 11:00:000
101anageneralMorning all2024-03-01 09:00:000
103bengeneralStandup moved to 102024-03-01 09:05:001
107anageneralThanks2024-03-01 09:05:000

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 .