Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Mutual Follows Query

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Mutual Follows Query. Start with the requirements and the one table on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

I have a table with follower and followee (where the follower follows the followee). How do you return a table of mutual follows?

Return each mutual relationship once, even though the source contains two directional rows. Ignore incomplete rows where either user ID is NULL.

Output

  1. One row per mutual pair, with columns follower_id and followee_id.
  2. The lower ID must appear in follower_id, and the higher ID in followee_id.
  3. Order by follower_id, then followee_id ascending.

Schema

follow_relationships
ColumnTypeDescription
relationship_idPKINTUnique relationship record identifier
follower_idINTUser who follows another user
followee_idINTUser being followed
Tablesfollow_relationships
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results