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

SQL_2025_L02_hints

The document outlines Assignment 4 for the COM2103 Database Design and Management course, due on March 19, 2024. It includes tasks such as creating a database, writing SQL queries, and performing updates on student records. Additionally, it provides specific SQL statement requirements and examples for various queries related to student data.

Uploaded by

Lee Vico
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)
7 views

SQL_2025_L02_hints

The document outlines Assignment 4 for the COM2103 Database Design and Management course, due on March 19, 2024. It includes tasks such as creating a database, writing SQL queries, and performing updates on student records. Additionally, it provides specific SQL statement requirements and examples for various queries related to student data.

Uploaded by

Lee Vico
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/ 5

COM2103 Database Design and Management

Department of Computer Science


2024/25 Semester 2
Assignment 4

Student Name:
Student ID:
Marks: _______ / 9
Due: 19 Mar 2024, 23:59pm
Chapter 5 - SQL
Question 1.
1. Create a database named HSU as described in the lecture notes Part 5A page 6.
2. Create a new user for yourself and name it your Student ID.
3. Grant your user account all access rights to the “HSU” database.
4. Create a screenshot that shows the data in the database using your account.
Below is a demo.

Your screencap:

(1 mark)

1
Question 2. Write an SQL statement for each of the following queries. Make a
screencap of your SQL statement and the result of the SQL statement.
Note:
1. Your complete SQL statement should be shown in the screencap.
2. The result of the SQL statement should be shown in the screencap.
3. The account name (your student ID) should be shown in the screencap.
Otherwise, marks will be deducted. Again, remember to display not only the results,
but also your SQL statements and your student ID in the MySQL shell.

(i) Find the ID and name of students in programmes that begin with "AH".
Use the student's ID to sort the records in descending order.

SQL statement (2 mins): 10:57-10:59am


Select StudentID, StudName from Student where Major like 'AH%'
order by StudentID desc;

Your screencap:

(1 marks)
(ii) Find the number of “B" grades each student received. Show their name
and the corresponding number. Students without a "B" grade do not
need to be shown in the results. → inner join
SQL statement:
SELECT StudName, count(grade)
FROM Student INNER JOIN Result
ON Student.StudentID = Result.StudentID
where grade='B'
group by student.studentID, student.StudName;
Your screencap:

(1 marks)

(iii) Find the number of “B" grades each student received. Show their name
and the corresponding number. Students without a "B" grade are
2
required to be shown in the results. → outer join
SQL statement:
Select StudName, count(grade)
from student left join (SELECT * from result where grade='B') as r2
on student.studentid=r2.studentid
group by student.StudentID, student.StudName;
Your screencap:

(1 mark)

(iv) Find the student name who is in the same programme as Bob. No need to
show Bob’s name.
1) Select Bobs’ program name
2) Filter out rows with Bob’s program name
3) Do not know the name ‘Bob’
SQL statement:
select StudName from student
where major=(Select major from Student where Studname='Bob')
AND StudName <> 'Bob';
Your screencap:

(1 mark)
Question 3. Write an SQL statement for each of the following updates.
Just write down the SQL statement. You don’t need to make a screencap for this
question.
(i) Delete the module with code COM2001 from the “Module” table.
DELETE FROM Module
WHERE ModuleCode=’COM2001’;
(1 mark)
(ii) Change the name of the student “Bob” to “Tom”.

3
UPDATE student
SET studName=’Tom’
WHERE studName=’Bob’;
(1 mark)
Question 4. Given a database with 2 tables below.
Table: T2(A, B, C)
A B C
1 ABC 1100
2 BCD 773
3 CDE 251
4 DEF 98

Table: T3(A, B, C)
A B C
1 1 33
2 1 23
3 1 15
4 4 50

Write down the result of the following SQL statements, including the attribute names
as column headers.
SELECT T2.B, SUM(T3.C) AS S
FROM T2 LEFT JOIN T3 ON T2.A = T3.B
GROUP BY T2.A
ORDER BY S DESC
T2.A T2.B T2.C T3.A T3.B T3.C
1 ABC 1100 1 1 33

1 ABC 1100 2 1 23

1 ABC 1100 3 1 15

2 BCD 773 Nil Nil Nil


3 CDE 251 Nil Nil Nil
4 DEF 98 4 4 50

Group by step
T2.B SUM(T3.C) → T2.B S
AS S

4
ABC 71 ABC 71
BCD Nil DEF 50

CDE Nil BCD Nil


DEF 50 CDE Nil
(2 marks)

-End-

You might also like