clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2335802 fiddles created (27458 in the last week).

create table friends (first_person_id int, second_person_id int); create table likes (first_person_id int, second_person_id int); create table person (id int, name varchar); create function person_friend_with_person(_first_person_id int, _second_person_id int) returns boolean as $$ select exists (select from friends where first_person_id = _first_person_id and second_person_id = _second_person_id); $$ language sql; create function person_likes_person(_first_person_id int, _second_person_id int) returns boolean as $$ select exists (select from likes where first_person_id = _first_person_id and second_person_id = _second_person_id); $$ language sql; insert into person select 1, 'A'; insert into person select 2, 'B'; insert into person select 3, 'C'; insert into friends select 1, 2; insert into friends select 2, 3; insert into likes select 1, 3;
1 rows affected
1 rows affected
1 rows affected
1 rows affected
1 rows affected
1 rows affected
 hidden batch(es)


select * from person as A, person as B, person as C where (person_friend_with_person(A.id, B.id) or person_friend_with_person(B.id, A.id)) and (person_friend_with_person(B.id, C.id) or person_friend_with_person(C.id, B.id)) and person_likes_person(A.id, C.id) and not person_friend_with_person(A.id, C.id)
id name id name id name
1 A 2 B 3 C
 hidden batch(es)