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

Assignment+2_solution_2024

Uploaded by

Krystal Yeung
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

Assignment+2_solution_2024

Uploaded by

Krystal Yeung
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/ 8

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: For Question A, 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. Submission 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. [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, 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. [6 points]
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 sessions have 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 username(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]
Solve all queries below on the COMPANY schema using relational algebra
expressions (Note: No score will be given for SQL). Write the result of each query
applied to the database state in Figure 5.6. (Note that the result can be an empty
set.)
(1) Retrieve the address of managers. [4 marks]
( ⋈ _ )
Result: 638 Voss, Houston, TX; 291 Berry, Bellaire TX; 450 Stone, Houston, TX.

(2) Retrieve the first names of all employees working in the department No.5 whose salary
are no less than 38000. [4 marks]
( !"#$$$ &' ' ( ) ( ))
Results: Franklin; Ramesh.

(3) List the first name of each department manager who has at least one son as a dependent.
[8 marks]
*+_,,-_,.- ← 01( 213 4 ( 4 ( ⋈ _ 5 )
( *+_,,-_,.- ⋈ _ )
Result: Franklin.

(4) List the birthdays of all department managers and all their dependents. [8 marks]
67+8ℎ_:*+ ← ; 0 ( ⋈ _ )
67+8ℎ_:*+_<=> ← ; 0 ( ⋈ _ 5 )
67+8ℎ_?@@ ← 67+8ℎ_:*+ ∪ 67+8ℎ_:*+_<=>
Result: 1955-12-08; 1941-06-20; 1937-11-10; 1986-04-05; 1983-10-25; 1958-05-03;
1942-02-28.

(5) Retrieve the first names and last names of employees who work on both ProductX and
ProductY. [8 marks]
B,-_ -. ← C( ,E F G )( 5 ,E ( (H IB_ ))

., ← E F G J E 4E ( FK0L 4 MN E 4E ( FK0O 4 ( P Q )R
B,-_S _ ., ← B,-_ -. ÷ .,
,U ( ∗ B,-_S _ .,)
Result: John Smith; Joyce English

(6) Find the first names of employees in department No.5 who don’t work in Houston. [8
marks]
B,-_H IB ← H IB_ ⋈E ( E F G P Q
_H IB ← ⋈ 5 B,-_H IB
_ 5_X YB ← J E (K 01( 4 Z(F 0( 4 &' ' ( ) ( _H IB )R
_ 5← J ' ( )( _H IB )R
← _ 5− _ 5_X YB
Result: John; Joyce.

You might also like