-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.sql
94 lines (80 loc) · 3.36 KB
/
queries.sql
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
--Queries requested by Client.
-- Query 1: Find Family Information for a Given Student
SELECT f.*
FROM Family f
JOIN Student_Room_Contract src ON f.FamilyID = src.FamilyID
WHERE src.StudentID = <student_id>;
-- Query 2: List Available Rooms with Family Preferences
SELECT hr.*
FROM House_Has_RoomsForRent hr
JOIN Family f ON hr.FamilyID = f.FamilyID
WHERE hr.IsAvailable = 'Y' AND f.SmokeAllowed = 'N' AND f.PetAllowed = 'Y';
-- Query 3: Calculate Average Rating for a Family
SELECT f.FamilyID, AVG(sr.Rating) AS AverageRating
FROM Family f
LEFT JOIN Student_Review sr ON f.FamilyID = sr.FamilyID
GROUP BY f.FamilyID;
-- Query 4: Find Families with Common Interests
SELECT f.FamilyID, GROUP_CONCAT(fi.Interest) AS CommonInterests
FROM Family f
JOIN FamilyMember_Interest fi ON f.FamilyID = fi.FamilyID
GROUP BY f.FamilyID
HAVING COUNT(fi.Interest) >= 2;
-- Query 5: Retrieve Student's Current Room and Family Details
SELECT s.*, hrr.*, f.*
FROM Student s
JOIN Student_Room_Contract src ON s.StudentID = src.StudentID
JOIN House_Has_RoomsForRent hrr ON src.FamilyID = hrr.FamilyID AND src.UnitNumber = hrr.UnitNumber
JOIN Family f ON src.FamilyID = f.FamilyID;
-- Query 6: Find Families with Multiple Rooms for Rent
SELECT f.FamilyID, COUNT(hrr.RoomNumber) AS NumberOfRoomsForRent
FROM Family f
JOIN House_Has_RoomsForRent hrr ON f.FamilyID = hrr.FamilyID
GROUP BY f.FamilyID
HAVING COUNT(hrr.RoomNumber) > 1;
-- Query 7: Calculate Monthly Earnings for Each Family
SELECT f.FamilyID, SUM(src.Price) AS MonthlyEarnings
FROM Family f
JOIN Student_Room_Contract src ON f.FamilyID = src.FamilyID
GROUP BY f.FamilyID;
-- Query 8: List Students Interested in a Particular Hobby
SELECT s.*
FROM Student s
JOIN Student_Hobby sh ON s.StudentID = sh.StudentID
WHERE sh.Hobby = 'Travel';
-- Query 9: Count Families in Each Province
SELECT f.Province, COUNT(*) AS FamilyCount
FROM Family f
GROUP BY f.Province
ORDER BY FamilyCount DESC;
-- Query 10: Find Families with Contract End Dates in the Next Month
SELECT f.FamilyID, src.FamilyID, src.EndDate
FROM Family f
JOIN Student_Room_Contract src ON f.FamilyID = src.FamilyID
WHERE src.EndDate BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 1 MONTH);
-- Query 11: Find Families with High-Rated Rooms
SELECT f.FamilyID, AVG(sr.Rating) AS AverageRating
FROM Family f
JOIN House_Has_RoomsForRent hr ON f.FamilyID = hr.FamilyID
LEFT JOIN Student_Room_Contract src ON hr.FamilyID = src.FamilyID AND hr.UnitNumber = src.UnitNumber
LEFT JOIN Student_Review sr ON src.TrackingNumber = sr.TrackingNumber
GROUP BY f.FamilyID
HAVING AVG(sr.Rating) >= 4.5;
-- Query 12: List Students Who Haven't Reviewed Their Homestay
SELECT s.*
FROM Student s
LEFT JOIN Student_Room_Contract src ON s.StudentID = src.StudentID
LEFT JOIN Student_Review sr ON src.TrackingNumber = sr.TrackingNumber
WHERE sr.ReviewID IS NULL;
-- Query 13: Calculate Total Earnings and Average Price per Room for Each Family
SELECT f.FamilyID, SUM(src.Price) AS TotalEarnings, AVG(src.Price) AS AveragePricePerRoom
FROM Family f
JOIN Student_Room_Contract src ON f.FamilyID = src.FamilyID
GROUP BY f.FamilyID;
-- Query 14: Find Students Who Extended Contracts Multiple Times
SELECT s.StudentID, s.FirstName, s.LastName, COUNT(src.TrackingNumber) AS NumberOfExtensions
FROM Student s
JOIN Student_Room_Contract src ON s.StudentID = src.StudentID
WHERE src.EndDate > src.ExpectedEndDate
GROUP BY s.StudentID
HAVING COUNT(src.TrackingNumber) > 1;