Question
Given two relations, student(sid: integer, name: string) and friend(sid1: integer, sid2: integer) Which of the following expressions/queries will find all pairs of students who have
Given two relations, student(sid: integer, name: string) and friend(sid1: integer, sid2: integer) Which of the following expressions/queries will find all pairs of students who have at least two mutual friends? HINT: Join student or friend table with itself (self-joins) and think of next steps(more joins or filtering maybe?) SELECT ALL THAT APPLY BY ticking the text box 1) s1.sid, s2.sid ( (STUDENT AS s1) (STUDENT AS s2) ( ( sid1, sid2 (FRIEND)) AS f1 ( sid1, sid2 (FRIEND)) AS f2 |f1.sid1 = s1.sid AND f2.sid1 = s1.sid AND f1.sid2 = s2.sid AND f2.sid2 = s2.sid |f1.sid1 = s2.sid AND f2.sid1 = s2.sid AND f1.sid2 = s1.sid AND f2.sid2 = s1.sid ) |s1.sid < s2.sid ) 2) s1.sid, s2.sid ( s1.sid < s2.sid AND ( f1.sid1 = s1.sid AND f2.sid1 = s2.sid ( f1.sid2 = f2.sid2 ( FRIEND f1.sid1, f1.sid2 FRIEND f2.sid1, f2.sid2 FRIEND ) ) GROUP BY s1.sid, s2.sid HAVING COUNT(*) >= 2 ) STUDENT AS s1 STUDENT AS s2 ) 3) SELECT s1.name AS student1, s2.name AS student2, COUNT(*) AS num_mutual_friends FROM student s1 JOIN friend f1 ON s1.sid = f1.sid1 JOIN friend f2 ON s2.sid2 = f2.sid1 AND f1.sid1 <> f2.sid2 JOIN friend f3 ON f2.sid2 = f3.sid1 AND f1.sid1 <> f3.sid2 AND f2.sid1 <> f3.sid2 JOIN student s2 ON s2.sid = f3.sid2 AND s2.sid <> s1.sid GROUP BY s1.sid, s2.sid HAVING COUNT(*) >= 2; 4) SELECT s1.name AS student1, s2.name AS student2, COUNT(*) AS num_mutual_friends FROM student s1 JOIN friend f1 ON s1.sid = f1.sid1 JOIN friend f2 ON f1.sid2 = f2.sid1 AND f1.sid1 <> f2.sid2 LEFT JOIN friend f3 ON f2.sid2 = f3.sid1 AND f1.sid1 <> f3.sid2 AND f2.sid1 <> f3.sid2 JOIN student s2 ON s2.sid = f3.sid2 AND s2.sid <> s1.sid GROUP BY s1.sid, s2.sid HAVING COUNT(*) >= 2; 5) None of the above 6) SELECT s1.name AS student1, s2.name AS student2 FROM student s1 JOIN friend f ON s1.sid = f.sid1 JOIN student s2 ON s2.sid = f.sid2 AND s2.sid <> s1.sid GROUP BY s1.sid, s2.sid HAVING COUNT(*) >= 2; 7) f1.sid1, f2.sid1 ( f1.sid1 < f2.sid1 AND f1.sid2 = f2.sid2 AND ( f1.sid1, f1.sid2 (FRIEND) f2.sid1, f2.sid2 (FRIEND) ) GROUP BY f1.sid1, f2.sid1 HAVING COUNT(*) >= 2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started