File tree 4 files changed +143
-16
lines changed 4 files changed +143
-16
lines changed Original file line number Diff line number Diff line change
1
+ # Count Mutual Friends
2
+ ![ Star Badge] ( https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99 )
3
+ [ ![ View Main Folder] ( https://img.shields.io/badge/View-Main_Folder-971901? )] ( https://github.com/thecoddiwompler/SQL-Practice-Questions/tree/main )
4
+ [ ![ View Repositories] ( https://img.shields.io/badge/View-My_Repositories-blue?logo=GitHub )] ( https://github.com/thecoddiwompler?tab=repositories )
5
+ [ ![ View My Profile] ( https://img.shields.io/badge/View-My_Profile-green?logo=GitHub )] ( https://github.com/thecoddiwompler )
6
+
7
+ ---
8
+
9
+ ## 🛠️ Problem Statement
10
+
11
+ <b >Table Name : Friends</b >
12
+
13
+ | Column Name | Type |
14
+ | ------------- | ------------- |
15
+ | Friend1 | varchar(10) |
16
+ | Friend2 | varchar(10) |
17
+
18
+
19
+ </br >
20
+
21
+ In this table, all pair of friends are given. For the given friends, Write a SQL Query to find the no of mutual friends.
22
+ Return the result table in any order.
23
+
24
+ The query result format is in the following example:
25
+
26
+ <details >
27
+ <summary >
28
+ Input
29
+ </summary >
30
+ <br >
31
+ <b > Table Name: Friends </b >
32
+
33
+ <br >
34
+
35
+ | Friend1 | Friend2 |
36
+ | ---------| ---------|
37
+ | Jason | Mary |
38
+ | Mike | Mary |
39
+ | Mike | Jason |
40
+ | Susan | Jason |
41
+ | John | Mary |
42
+ | Susan | Mary |
43
+
44
+
45
+ </details >
46
+
47
+ <details >
48
+ <summary >
49
+ Output
50
+ </summary >
51
+ <br >
52
+
53
+ | Friend1 | Friend2 | no_of_mutual_friends |
54
+ | ---------| ---------| ----------------------|
55
+ | Jason | Mary | 2|
56
+ | Mike | Mary | 0|
57
+ | Mike | Jason | 1|
58
+ | Susan | Jason | 1|
59
+ | John | Mary | 1|
60
+ | Susan | Mary | 1|
61
+
62
+ </details >
63
+
64
+ ---
Original file line number Diff line number Diff line change
1
+ CREATE TABLE Friends (Friend1 VARCHAR (10 ), Friend2 VARCHAR (10 ));
2
+
3
+ INSERT INTO
4
+ Friends
5
+ VALUES
6
+ (' Jason' , ' Mary' ),
7
+ (' Mike' , ' Mary' ),
8
+ (' Mike' , ' Jason' ),
9
+ (' Susan' , ' Jason' ),
10
+ (' John' , ' Mary' ),
11
+ (' Susan' , ' Mary' );
12
+
13
+ COMMIT ;
Original file line number Diff line number Diff line change
1
+ WITH cte AS (
2
+ SELECT
3
+ friend1,
4
+ friend2
5
+ FROM
6
+ friends
7
+ UNION
8
+ SELECT
9
+ friend2,
10
+ friend1
11
+ FROM
12
+ friends
13
+ ),
14
+ friend1_friend AS (
15
+ SELECT
16
+ a .friend1 ,
17
+ a .friend2 ,
18
+ b .friend2 friend1_friend
19
+ FROM
20
+ friends a
21
+ LEFT OUTER JOIN cte b ON a .friend1 = b .friend1
22
+ AND a .friend2 <> b .friend2
23
+ ),
24
+ friend2_friend AS (
25
+ SELECT
26
+ a .friend1 ,
27
+ a .friend2 ,
28
+ b .friend2 friend2_friend
29
+ FROM
30
+ friends a
31
+ INNER JOIN cte b ON a .friend2 = b .friend1
32
+ WHERE
33
+ a .friend1 <> b .friend2
34
+ )
35
+ SELECT
36
+ a .friend1 ,
37
+ a .friend2 ,
38
+ count (* ) filter(
39
+ WHERE
40
+ a .friend1_friend IS NOT NULL
41
+ ) no_of_mutual_friends
42
+ FROM
43
+ friend1_friend a
44
+ LEFT OUTER JOIN friend2_friend b ON a .friend1 = b .friend1
45
+ AND a .friend2 = b .friend2
46
+ AND a .friend1_friend = b .friend2_friend
47
+ GROUP BY
48
+ a .friend1 ,
49
+ a .friend2 ;
Original file line number Diff line number Diff line change 3
3
3 . Child - Parent - Grandparent Hierarchy
4
4
4 . Clocked Hours
5
5
5 . Company Structure [ Extra Difficult]
6
- 6 . Department Top 3 Salary
7
- 7 . Employees Check-in Details
8
- 8 . Employees Hiring [ Difficult]
9
- 9 . Employees Log [ Extra Difficult]
10
- 10 . Final Destination
11
- 11 . Find Cumulative Salary of an Employee
12
- 12 . Highest-Grossing Items
13
- 13 . Increasing Sales Revenue
14
- 14 . Last Person to Fit in the Bus
15
- 15 . Manager with at least 5 direct reportees
16
- 16 . Mismatched IDs
17
- 17 . Missing Invoices
18
- 18 . Odd and Even Measurements
19
- 19 . Onboarded Cities
20
- 20 . Qualifying Criteria
21
- 21 . Travel Analytics
6
+ 6 . Count Mutual Friends [ Difficult]
7
+ 7 . Department Top 3 Salary
8
+ 8 . Employees Check-in Details
9
+ 9 . Employees Hiring [ Difficult]
10
+ 10 . Employees Log [ Extra Difficult]
11
+ 11 . Final Destination
12
+ 12 . Find Cumulative Salary of an Employee
13
+ 13 . Highest-Grossing Items
14
+ 14 . Increasing Sales Revenue
15
+ 15 . Last Person to Fit in the Bus
16
+ 16 . Manager with at least 5 direct reportees
17
+ 17 . Mismatched IDs
18
+ 18 . Missing Invoices
19
+ 19 . Odd and Even Measurements
20
+ 20 . Onboarded Cities
21
+ 21 . Qualifying Criteria
22
+ 22 . Travel Analytics
You can’t perform that action at this time.
0 commit comments