AnalystPath

Recipe Suggestions From Connections

SQLHardSenior level~15 min

Problem

A cooking community lets members connect and save recipes.

Table: connections

| Column Name | Type |
|-------------|------|
| member_a | int |
| member_b | int |

(member_a, member_b) is the primary key for this table. Each row means member_a and member_b are connected. Connections are mutual but each pair is stored only once (in one direction).

Table: saves

| Column Name | Type |
|-------------|------|
| member_id | int |
| recipe_id | int |

(member_id, recipe_id) is the primary key for this table. Each row means a member saved a recipe.

The app suggests a recipe to a member when at least one of that member's connections has saved it and the member has not already saved it themselves. For every member, report how many of their connections saved each suggested recipe.

Return member_id, recipe_id, and the count of connections who saved it (call it connection_saves), in any order.

Tables

Example rows — the live problem includes the full dataset.

connections
member_amember_b
12
13
14
saves
member_idrecipe_id
188
223
324

Expected output

Your answer should return 16 rows with the columns member_id, recipe_id, connection_saves.

Starter code (SQL)

SELECT *
FROM connections;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions