Skip to content
Onsite

Mutual friends of two users

MediumP3Coding

Table friendships:

ColumnType
user_idint
friend_idint

A row means the two users are friends. Each friendship is stored once, in either direction, though the same pair can appear in both directions by accident. Friendship is mutual, so (1, 3) makes 3 a friend of 1 and 1 a friend of 3.

Return the mutual friends of users 1 and 2: users who are friends with both.

Output column: friend_id.

Schema

CREATE TABLE friendships (
  user_id int NOT NULL,
  friend_id int NOT NULL
);

Example 1

Input
friendships: (1, 3), (4, 1), (2, 3), (2, 4), (5, 2)
Output
friend_id: 3, 4

Constraints

  • PostgreSQL dialect.

Hints

PostgreSQL
Run checks the 3 visible tests, Submit all 6.