Mobile wallpaper 1Mobile wallpaper 2Mobile wallpaper 3Mobile wallpaper 4Mobile wallpaper 5Mobile wallpaper 6
641 字
3 分钟
Databases Week9 Seminar
2025-11-27

Refine and Refresh#

Exercise 1 – Considering the following tables from two different modules in the computing school, DB and FOC.

DB_module

Student_name DB_Grade
Garry C
Robin D
Hans A
Lily B
Sophie B

FOC_module

Student_name FOC_Grade
Garry A
Robin C
Hans B
Denis A
Lewis D

a. Create the tables above in the schema, taking into account the practical size of the data.

CREATE TABLE brookes.DB_module (
id INT PRIMARY KEY AUTO_INCREMENT,
Student_name TEXT NOT NULL,
DB_Grade TEXT NOT NULL
);
CREATE TABLE brookes.FOC_module (
id INT PRIMARY KEY AUTO_INCREMENT,
Student_name TEXT NOT NULL,
FOC_Grade TEXT NOT NULL
);
INSERT INTO brookes.DB_module (Student_name, DB_Grade) VALUES
('Garry', 'C'),
('Robin', 'D'),
('Hans', 'A'),
('Lily', 'B'),
('Sophie','B');
INSERT INTO brookes.FOC_module (Student_name, FOC_Grade) VALUES
('Garry', 'A'),
('Robin', 'C'),
('Hans', 'B'),
('Denis', 'A'),
('Lewis', 'D');

b.

  1. Write a basic SQL statement to retrieve the students who are intersecting between both modules.
SELECT Student_name
FROM brookes.db_module
WHERE Student_name IN (
SELECT Student_name
FROM brookes.foc_module
);
  1. The university is planning an external workshop. Only the students enrolled in both modules, DB and FOC, are allowed to attend that workshop. Write a Join statement to help the coordinator make the right decision.
select * from brookes.db_module JOIN brookes.foc_module ON
brookes.DB_module.Student_name=brookes.foc_module.Student_name;
select db_Student_name as Student_name from (
select d.id as db_id,
d.Student_name as db_Student_name,
d.DB_Grade as db_Grade,
f.id as foc_id,
f.Student_name as foc_Student_name,
f.FOC_Grade as foc_Grade
from brookes.db_module as d JOIN brookes.foc_module as f ON
d.Student_name=brookes.f.Student_name) as temp;
  1. Validate your answers by comparing the output from sections b(1) and b(2).

c. The workshop coordinator only selects students enrolled in DB and FOC modules. However, there are not enough students with the necessary conditions to fill all the available seats. So the coordinator wants a list of students who are already enrolled in one module.

SELECT d.Student_name FROM db_module AS d LEFT JOIN foc_module AS f ON d.Student_name = f.Student_name
UNION
SELECT f.student_name FROM brookes.db_module AS d RIGHT JOIN brookes.foc_module AS f ON d.Student_name = f.Student_name;
SELECT Student_name
FROM DB_module
UNION
SELECT Student_name
FROM FOC_module;
TIP

UNION 会自动去重,而 UNION ALL 不会

d. A programming contest has been planned to take place on Friday morning in the library. Students who take the DB module can join the contest. However, students enrolled in the FOC class can not join because their module has a class at the contest time. Write a join SQL statement to retrieve the prospective contest candidates.

SELECT d.Student_name
FROM brookes.db_module AS d
LEFT JOIN brookes.foc_module AS f
ON d.Student_name = f.Student_name
WHERE f.Student_name IS NULL;

e. Dr Maged is presenting a basic introduction to databases. Only students enrolled in the FOC module but not enrolled in the DB module can join the presentation. Help the teacher invite deserving students to the presentation.

SELECT f.Student_name
FROM brookes.db_module AS d
RIGHT JOIN brookes.foc_module AS f
ON d.Student_name = f.Student_name
WHERE d.Student_name IS NULL;
Databases Week9 Seminar
https://mizuki.anka2.top/posts/l5-db-week9-seminar/
作者
🐦‍🔥不死鸟Anka
发布于
2025-11-27
许可协议
MIT

部分信息可能已经过时

封面
Sample Song
Sample Artist
封面
Sample Song
Sample Artist
0:00 / 0:00