Count unique pairs of users who have exchanged messages

Quick Overview

Write a SQL query over a messages table that counts the distinct pairs of people who have communicated, treating a message from A to B and from B to A as the same pair and ignoring self-messages. It tests normalizing unordered pairs, counting distinct combinations and handling duplicate rows.

Count unique pairs of users who have exchanged messages

Company: Roku

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Count the unique pairs of people who have communicated in `Messages`. A pair is unordered: a message from A to B and a message from B to A belong to the same pair `(A, B)`. Messages a person sends to themselves (`sender_id = receiver_id`) do not count. Return a single column `unique_pairs`. ```hint Make direction irrelevant Before counting, think about how to write each message's two ids so that a message from A to B and one from B to A produce exactly the same value. ``` ### Input Table(s) Table `Messages`, one row per message. Primary key: `message_id`. | Column | Type | Meaning | |---|---|---| | message_id | INT | Message identifier (primary key) | | sender_id | INT | Person who sent the message (NOT NULL, positive) | | receiver_id | INT | Person who received the message (NOT NULL, positive) | | sent_at | DATETIME | When the message was sent (NOT NULL) | ### Output Contract - Exactly one row with one column, `unique_pairs`: an integer count of distinct unordered pairs of different people who exchanged at least one message. - No ordering or rounding is involved. ### Example Input `Messages`: | message_id | sender_id | receiver_id | sent_at | |---|---|---|---| | 1 | 1 | 2 | 2024-01-01 10:00:00 | | 2 | 2 | 1 | 2024-01-01 10:05:00 | | 3 | 1 | 3 | 2024-01-02 09:00:00 | | 4 | 3 | 2 | 2024-01-02 11:00:00 | | 5 | 2 | 3 | 2024-01-03 08:00:00 | Expected output: | unique_pairs | |---| | 3 | The pairs are (1, 2) from messages 1 and 2, (1, 3) from message 3, and (2, 3) from messages 4 and 5. Messages in both directions between the same two people still make one pair, so the answer is 3. ### Constraints and Clarifications - `Messages` has 1 to 100,000 rows. - `sender_id` and `receiver_id` are positive integers and never NULL. - One message in either direction is enough for a pair to count. The two people do not need to have written to each other. - Many messages between the same two people count as one pair. - Self-messages are ignored. If every row is a self-message, return one row with `unique_pairs = 0`. - `sent_at` does not affect the answer.

Overview: Write a SQL query over a messages table that counts the distinct pairs of people who have communicated, treating a message from A to B and from B to A as the same pair and ignoring self-messages. It tests normalizing unordered pairs, counting distinct combinations and handling duplicate rows.

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

Count the unique pairs of people who have communicated in Messages. A pair is unordered: a message from A to B and a message from B to A belong to the same pair (A, B). Messages a person sends to themselves (sender_id = receiver_id) do not count. Return a single column unique_pairs.

Input Table(s)

Table Messages, one row per message. Primary key: message_id.

ColumnTypeMeaning
message_idINTMessage identifier (primary key)
sender_idINTPerson who sent the message (NOT NULL, positive)
receiver_idINTPerson who received the message (NOT NULL, positive)
sent_atDATETIMEWhen the message was sent (NOT NULL)

Output Contract

  • Exactly one row with one column, unique_pairs : an integer count of distinct unordered pairs of different people who exchanged at least one message.
  • No ordering or rounding is involved.

Example

Input Messages:

message_idsender_idreceiver_idsent_at
1122024-01-01 10:00:00
2212024-01-01 10:05:00
3132024-01-02 09:00:00
4322024-01-02 11:00:00
5232024-01-03 08:00:00

Expected output:

unique_pairs
3

The pairs are (1, 2) from messages 1 and 2, (1, 3) from message 3, and (2, 3) from messages 4 and 5. Messages in both directions between the same two people still make one pair, so the answer is 3.

Constraints and Clarifications

  • Messages has 1 to 100,000 rows.
  • sender_id and receiver_id are positive integers and never NULL.
  • One message in either direction is enough for a pair to count. The two people do not need to have written to each other.
  • Many messages between the same two people count as one pair.
  • Self-messages are ignored. If every row is a self-message, return one row with unique_pairs = 0 .
  • sent_at does not affect the answer.