For each student A who likes a student B where the two are not friends, find if they have a friend C in common (who can introduce them!)
I’ve tables
Friend (ID1, ID2)
Likes(ID1, ID2)
I need to select pairs of id from Likes that don’t friends.
I’ve done it
SELECT L.id1 id1, L.id2 id2
FROM likes L
LEFT JOIN friend F ON (L.id1 = F.id1 and L.id2 = F.id2)
WHERE F.id1 IS NULL
How to find Friend IDs in common?
We need to find all tuples such that the following conditions apply:
A query that accomplishes this would be something like this:
PS I only tested in in sqlite3, so there may be a slight syntax error in another DBMS, but I don’t think so. Also, my answer returns a slightly more readable answer than just the mutual friend id.
Hope that helps,