0% found this document useful (0 votes)
17 views

Homework2 Sol

Uploaded by

xbaby y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Homework2 Sol

Uploaded by

xbaby y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Department of Computer Science, City University of Hong Kong

CS3402 Database Systems

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.

 ExerciseType (type_id: integer, name: string)


// Describes different types of exercises available in the application, each with a unique ID
and name.
 ExerciseSession (session_id: integer, user_id: integer, type_id: integer, date: date, duration:
integer, calories_burned: integer)
// Describes individual exercise sessions, including the type of exercise, duration, calories
burned, and the session date. It links to both the user and the specific type of exercise.

 ActivityShare (share_id: integer, user_id: integer, session_id: integer, share_date: date)


// Describes instances where users share their exercise sessions, including the date of
sharing. It references both the user who shared the session and the specific exercise session
shared.

(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]

SELECT session_id FROM ExerciseSession


WHERE user_id = (SELECT user_id FROM User WHERE name = 'Bob Jones');

-- 2. List the names of users who have not logged any exercise sessions. [6 points]

SELECT name FROM User


WHERE user_id NOT IN
(
SELECT DISTINCT user_id FROM ExerciseSession
);

-- 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
);

-- 4. Calculate the average calories burned of male users. [6 points]

SELECT AVG(calories_burned) AS avg_calories_burned


FROM ExerciseSession
WHERE user_id IN (
SELECT user_id FROM User WHERE gender = 'M'
);

-- 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]

SELECT type_id, COUNT(*) AS frequency


FROM ExerciseSession
GROUP BY type_id
ORDER BY frequency DESC;

-- 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
)
);

Question B Relational Algebra [40 points]


Considering the relational database schema of an album retailers shown below, write the following
queries using relational algebra expressions (Note: No score will be given for SQL).

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

You might also like