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.
- Write a basic SQL statement to retrieve the students who are intersecting between both modules.
SELECT Student_nameFROM brookes.db_moduleWHERE Student_name IN ( SELECT Student_name FROM brookes.foc_module);- 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 ONbrookes.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_Gradefrom brookes.db_module as d JOIN brookes.foc_module as f ONd.Student_name=brookes.f.Student_name) as temp;- 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_nameUNIONSELECT 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_nameFROM DB_moduleUNIONSELECT Student_nameFROM 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_nameFROM brookes.db_module AS dLEFT JOIN brookes.foc_module AS f ON d.Student_name = f.Student_nameWHERE 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_nameFROM brookes.db_module AS dRIGHT JOIN brookes.foc_module AS f ON d.Student_name = f.Student_nameWHERE d.Student_name IS NULL;部分信息可能已经过时









