Homework2 Sol
Homework2 Sol
Assignment 2
================================================================
Question A SQL. [60 points]
Given the following database about the information of a Health and Fitness Application. It structures
the management of user profiles, health records, exercise sessions, and sharing of those exercise
activities.
User (user_id: integer, name: string, email: string, age: integer, gender: string)
// Describe the basic information about users, including their ID, name, email, age, and
gender.
HealthRecord (record_id: integer, user_id: integer, date: date, weight: float, height: float,
heart_rate: integer)
// Records detailed health metrics for users, such as weight, height, and heart rate, linking
these records to the corresponding user by user_id.
(Note: You need to log into http://dboj.cs.cityu.edu.hk:8080/ and choose SQL Homework 2 under
Exercise to answer the questions for the online judge. Submissions directly made to Canvas will not
be graded.)
-- 1. Find the exercise session IDs for the user 'Bob Jones'. [6 points]
-- 2. List the names of users who have not logged any exercise sessions. [6 points]
-- 3. List the names of users who have logged more than one exercise session on any given day.
SELECT U.name
FROM User U
WHERE U.user_id IN (
SELECT ES.user_id
FROM ExerciseSession ES
GROUP BY ES.user_id, ES.date
HAVING COUNT(*) > 1
);
-- 5. List name(s) of the exercise type(s) that have never been shared.
SELECT ET.name
FROM ExerciseType ET
WHERE ET.type_id NOT IN (
SELECT DISTINCT E.type_id
FROM ExerciseSession E, ActivityShare AS ASh
WHERE E.session_id = ASh.session_id
);
-- 6. List the ID of each exercise type along with its frequency of occurrence (i.e., the total number
of times the exercise session has been done), in descending order by frequency. If a type has never
been performed in any exercise session, it will not be included in the result. [6 points]
-- 7. List the names of users who have logged at least two exercise sessions but have never shared
any of exercise sessions. [6 points]
SELECT U.name
FROM User U
WHERE U.user_id IN (
SELECT ES.user_id
FROM ExerciseSession ES
GROUP BY ES.user_id
HAVING COUNT(ES.session_id) >= 2
) AND U.user_id NOT IN (
SELECT ASh.user_id FROM ActivityShare AS ASh
);
-- 8. List the user ID(s) with the largest weight change in the HealthRecord. [6 points]
SELECT HR.user_id
FROM HealthRecord AS HR
GROUP BY HR.user_id
HAVING MAX(HR.weight) - MIN(HR.weight) = (
SELECT MAX(max_weight - min_weight) FROM (
SELECT
MAX(weight) AS max_weight,
MIN(weight) AS min_weight
FROM HealthRecord
GROUP BY user_id
) AS WeightDifference
);
-- 9. List the user name(s) with the maximum number of health records. [6 points]
SELECT name
FROM User
WHERE user_id IN (
SELECT user_id
FROM HealthRecord
GROUP BY user_id
HAVING COUNT(*) = (
SELECT MAX(record_count)
FROM (
SELECT user_id, COUNT(*) as record_count
FROM HealthRecord
GROUP BY user_id
) AS RecordCounts
)
);
-- 10. List the name(s) of exercise type(s) that result in the highest average calorie burn for female
users only. [6 points]
SELECT ET.name
FROM ExerciseType ET
WHERE ET.type_id IN (
SELECT ES.type_id
FROM ExerciseSession ES
WHERE ES.user_id IN (
SELECT user_id FROM User WHERE gender = 'F'
)
GROUP BY ES.type_id
HAVING AVG(ES.calories_burned) = (
SELECT MAX(avg_calories_burned)
FROM (
SELECT AVG(ES2.calories_burned) as avg_calories_burned
FROM ExerciseSession ES2
WHERE ES2.user_id IN (SELECT user_id FROM User WHERE gender = 'F')
GROUP BY ES2.type_id
) AS max_avg_calories_burned
)
);
1) Find the names of the artists who have album with the genre of "Pop music". [6 points]
π _ σ " " (ALBUM ∗ ARTIST)
or π _ σ " " ALBUM
2) List the name of albums released in 2023 by the Hong Kong artist. [6 points]
ARTIST_HK = σ " " (ARTIST)
ALBUM_2023 = σ (ALBUM)
π _ (ARTIST_HK ∗ ALBUM_2023)
3) List the name of female artist and the total selling amount of all its album. [6 points]
ALBUM_FEMALE = σ " " (ALBUM ∗ ARTIST)
RESULT ← _ ℑ _ (SELLING_RECORD ∗ ALBUM_FEMALE)
4) Retrieve the Album ID which contains the maximum number of songs. [6 points]
SONG_NO ← _ ℑ _ (SONG)
RESULT ← π ( _ ℑ ( _ ) (SONG_NO))
5) Find the Customer ID who buy more than three albums with the genre of "Folk music" in
last year. (Note: from "2023-01-01" " to "2023-12-31") [8 points]
LAST_YEAR_RECORDS ← σ "2023-01-01" and " - - " (CUSTOMER_RECORD)
FOLK_ALBUM ← σ " " (ALBUM)
FILTERED_RECORDS ← LAST_YEAR_RECORDS ∗ FOLK_ALBUM
𝐶𝑁𝑂 ← σ ( ) ( _ ℑ (FILTERED_RECORDS))
π _ 𝐶𝑁𝑂
6) Find the Customer ID who buy all the album of the artist “Eason Chan”. [8 points]
EASON_ALBUM ← π _ (σ _ ‘ ’ (ALBUM))
EASON_FAN ← π _ , _ (CUSTOMER_RECORD)
RESULT ← EASONFAN ÷ EASON_ALBUM