-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08.AJ02-Placements.sql
More file actions
150 lines (119 loc) · 3.68 KB
/
08.AJ02-Placements.sql
File metadata and controls
150 lines (119 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
You are given three tables: Students, Friends and Packages. Students contains two columns: ID and
Name. Friends contains two columns: ID and Friend_ID (ID of the ONLY best friend). Packages contains
two columns: ID and Salary (offered salary in $ thousands per month).
Students
+--------+---------+
| Column | Type |
+--------+---------+
| ID | Integer |
| Name | String |
+--------+---------+
Friends
+-----------+---------+
| Column | Type |
+-----------+---------+
| ID | Integer |
| Friend_ID | Integer |
+-----------+---------+
Packages
+--------+---------+
| Column | Type |
+--------+---------+
| ID | Integer |
| Salary | Float |
+--------+---------+
Write a query to output the names of those students whose best friends got offered a higher salary than them.
Names must be ordered by the salary amount offered to the best friends.
It is guaranteed that no two students got same salary offer.
Sample Input
Students
+----+----------+
| ID | Name |
+----+----------+
| 1 | Ashley |
| 2 | Samantha |
| 3 | Julia |
| 4 | Scarlet |
+----+----------+
Friends
+----+-----------+
| ID | Friend_ID |
+----+-----------+
| 1 | 2 |
| 1 | 3 |
| 3 | 4 |
| 4 | 1 |
+----+-----------+
Packages
+-----+--------+
| ID | Salary |
+-----+--------+
| 1 | 15.20 |
| 2 | 10.06 |
| 3 | 11.55 |
| 4 | 12.12 |
+-----+--------+
Sample Output
Samantha
Julia
Scarlet
Explanation
See the following table:
+---------------+--------+----------+-------+---------+
| ID | 1 | 2 | 3 | 4 |
+---------------+--------+----------+-------+---------+
| Name | Ashley | Samantha | Julia | Scarlet |
| Salary | 15.2 | 10.06 | 11.55 | 12.12 |
| Friend ID | 2 | 3 | 4 | 1 |
| Friend Salary | 10.06 | 11.55 | 12.12 | 15.2 |
+---------------+--------+----------+-------+---------+
Now,
Samantha's best friend got offered a higher salary than her at 11.55
Julia's best friend got offered a higher salary than her at 12.12
Scarlet's best friend got offered a higher salary than her at 15.2
Ashley's best friend did NOT get offered a higher salary than her
The name output, when ordered by the salary offered to their friends, will be:
Samantha
Julia
Scarlet
*/
use hackerrank;
GO
raiserror('Now at the create procedure section ....',0,1)
GO
CREATE or ALTER PROCEDURE advancedjoin.proc_02placements AS
SELECT S.Name FROM advancedjoin.STUDENTS S
JOIN advancedjoin.FRIENDS F ON S.ID = F.ID
LEFT JOIN advancedjoin.PACKAGES P1 ON S.ID = P1.ID
LEFT JOIN advancedjoin.PACKAGES P2 ON F.Friend_ID = P2.ID
WHERE P2.Salary > P1.Salary
ORDER BY P2.Salary;
GO
CREATE or ALTER PROCEDURE AdvancedJoinTestClass.test_02placements
AS
BEGIN
IF OBJECT_ID('actual') IS NOT NULL DROP TABLE actual;
IF OBJECT_ID('expected') IS NOT NULL DROP TABLE expected;
CREATE TABLE actual (
NAME varchar(30) NOT NULL,
);
INSERT INTO actual (NAME) exec advancedjoin.proc_02placements
CREATE TABLE expected (
NAME varchar(30) NOT NULL,
);
INSERT INTO expected (NAME) SELECT 'Stuart';
INSERT INTO expected (NAME) SELECT 'Priyanka';
INSERT INTO expected (NAME) SELECT 'Paige';
INSERT INTO expected (NAME) SELECT 'Jane';
INSERT INTO expected (NAME) SELECT 'Julia';
INSERT INTO expected (NAME) SELECT 'Belvet';
INSERT INTO expected (NAME) SELECT 'Amina';
INSERT INTO expected (NAME) SELECT 'Kristeen';
INSERT INTO expected (NAME) SELECT 'Scarlet';
INSERT INTO expected (NAME) SELECT 'Priya';
INSERT INTO expected (NAME) SELECT 'Meera';
EXEC tSQLt.AssertEqualsTable 'expected', 'actual';
END;
GO
--exec tSQLt.Run 'AdvancedJoinTestClass.[test_02placements]';