Hasn't taken the course
Find students who haven't taken course IS210.
Objective: Consolidate everything covered in Weeks 1-5, read and analyze a real-world management problem, identify Entity/Attribute/Relationship, convert from ER/EER to a relational model, and combine multiple SQL techniques to solve a complete problem.
01 · OBJECTIVES & BIG PICTURE
By the end of Week 6, students should be able to:
SELECT, WHERE, ORDER BY, GROUP BY, HAVING, JOIN, Subquery, aggregate functions, INSERT, UPDATE, DELETEStudents need to look back over the whole process of building a database:
02 · REVIEWING THE DATABASE MODEL
An Entity is an object that needs to be managed within the system. Example - a student management system:
Example - the SINHVIEN Entity:
| Attribute | Meaning |
|---|---|
| MaSV | Student identifier |
| HoTen | Student name |
| NgaySinh | Date of birth |
| GioiTinh | Gender |
| MaLop | Student's class |
03 · KEYS IN A DATABASE
A Primary Key uniquely identifies a record:
CREATE TABLE SinhVien (
MaSV VARCHAR(10) PRIMARY KEY,
HoTen NVARCHAR(100),
NgaySinh DATE
);MaSV cannot be duplicated and cannot be NULL.
A Foreign Key creates a relationship between tables:
CREATE TABLE SinhVien (
MaSV VARCHAR(10) PRIMARY KEY,
HoTen NVARCHAR(100),
MaLop VARCHAR(10),
FOREIGN KEY (MaLop)
REFERENCES Lop(MaLop)
);Relationship - one class has many students:
04 · REVIEWING DATABASE CREATION & INSERT
Example: building a student and academic results management database with 4 tables, LOP, SINHVIEN, MONHOC, KETQUA, related as follows:
CREATE TABLE Lop (
MaLop VARCHAR(10) PRIMARY KEY,
TenLop NVARCHAR(100)
);
CREATE TABLE MonHoc (
MaMH VARCHAR(10) PRIMARY KEY,
TenMH NVARCHAR(100),
SoTinChi INT
);
CREATE TABLE SinhVien (
MaSV VARCHAR(10) PRIMARY KEY,
HoTen NVARCHAR(100),
MaLop VARCHAR(10),
FOREIGN KEY (MaLop)
REFERENCES Lop(MaLop)
);INSERT INTO Lop
VALUES
('HTTT01', N'Hệ thống thông tin 01'),
('HTTT02', N'Hệ thống thông tin 02');
INSERT INTO SinhVien
VALUES
('SV001', N'Nguyễn Văn An', 'HTTT01'),
('SV002', N'Trần Văn Bình', 'HTTT01'),
('SV003', N'Lê Văn Cường', 'HTTT02');SinhVien table has a Foreign Key referencing Lop, then HTTT01 and HTTT02 must already exist in the Lop table before students can be added.05 · REVIEWING SELECT, WHERE & LIKE
Retrieve all data:
SELECT *
FROM SinhVien;Select specific columns and give them an alias:
SELECT
MaSV AS [Mã sinh viên],
HoTen AS [Họ tên]
FROM SinhVien;Find students in class HTTT01:
SELECT *
FROM SinhVien
WHERE MaLop = 'HTTT01';Multiple conditions:
SELECT *
FROM SinhVien
WHERE MaLop = 'HTTT01'
AND HoTen LIKE N'Nguyễn%';% and _See the full details on both wildcard characters in Week 2 - Special Operators. Quick review:
| Wildcard | Meaning |
|---|---|
% | Represents any string of characters, including an empty string (0, 1, or more characters) |
_ | Represents exactly one character |
-- Starts with "Nguyễn"
WHERE HoTen LIKE N'Nguyễn%'
-- Ends with "An"
WHERE HoTen LIKE N'%An'
-- Contains "Văn"
WHERE HoTen LIKE N'%Văn%'
-- Exactly 4 characters, starting "PB0" + 1 any character
WHERE MaPB LIKE 'PB0_'06 · ORDER BY & AGGREGATE FUNCTIONS
Sort students by name:
SELECT *
FROM SinhVien
ORDER BY HoTen ASC; -- Change to DESC to sort descendingFunctions to remember: COUNT(), SUM(), AVG(), MIN(), MAX(). Example - how many students are there:
SELECT COUNT(*) AS SoLuongSinhVien
FROM SinhVien;07 · GROUP BY & HAVING
Count the number of students in each class:
SELECT
MaLop,
COUNT(*) AS SoLuong
FROM SinhVien
GROUP BY MaLop;| MaLop | SoLuong |
|---|---|
| HTTT01 | 2 |
| HTTT02 | 1 |
Only keep classes with 2 or more students:
SELECT
MaLop,
COUNT(*) AS SoLuong
FROM SinhVien
GROUP BY MaLop
HAVING COUNT(*) >= 2;08 · REVIEWING JOIN
This is the core topic of the review week. To display Student ID, Full Name, and Class Name:
SELECT
SV.MaSV,
SV.HoTen,
L.TenLop
FROM SinhVien SV
JOIN Lop L
ON SV.MaLop = L.MaLop;Only returns records that have a match in both tables - if a class has no students yet, that class won't appear in the result.
Shows every class, including ones with no students yet - this is a question type students commonly run into during practice:
SELECT
L.MaLop,
L.TenLop,
SV.MaSV,
SV.HoTen
FROM Lop L
LEFT JOIN SinhVien SV
ON L.MaLop = SV.MaLop;Example: given LOP ➜ SINHVIEN ➜ KETQUA ➜ MONHOC. Query which student took which course and what score they got:
SELECT
SV.MaSV,
SV.HoTen,
MH.TenMH,
KQ.Diem
FROM SinhVien SV
JOIN KetQua KQ
ON SV.MaSV = KQ.MaSV
JOIN MonHoc MH
ON KQ.MaMH = MH.MaMH;This is the kind of comprehensive query students need to master before finishing the practical portion of the course.
09 · REVIEWING SUBQUERY
Find students whose grade is higher than the average grade:
SELECT *
FROM KetQua
WHERE Diem > (
SELECT AVG(Diem)
FROM KetQua
);The thinking:
10 · REVIEWING UPDATE & DELETE
Fix a student's name:
UPDATE SinhVien
SET HoTen = N'Nguyễn Văn Nam'
WHERE MaSV = 'SV001';UPDATE SinhVien SET HoTen = N'Nguyễn Văn Nam'; without a WHERE, since that statement would update every student.DELETE FROM SinhVien
WHERE MaSV = 'SV001';Same idea - always double-check the WHERE clause before running DELETE.
11 · PRACTICE REQUIREMENTS
This week's formal exercise uses the classic "Suppliers - Parts - Shipments" schema (Goods Management) - the schema, sample data, and all 36 questions are on the exercise page below.
See the full assignment and detailed answers (access code required) for this comprehensive review exercise.
12 · CHALLENGE PROBLEMS
Extra practice with the Training Management schema (LOP, SINHVIEN, MONHOC, KETQUA) used throughout the theory sections above - no longer the formal exercise, but still useful for building reflexes. For stronger students to attempt in addition - each has a hidden hint, click to reveal.
Find students who haven't taken course IS210.
Find students whose grade is 5 or higher in every course.
Find the student with the highest average grade.
Rank students by average grade.
Find the course with the most students scoring below 5.
13 · COMMON MISTAKES
-- Wrong
JOIN Lop L ON SV.MaSV = L.MaLop
-- Correct
JOIN Lop L ON SV.MaLop = L.MaLop-- Wrong logic
WHERE COUNT(*) > 2
-- Correct
GROUP BY MaLop
HAVING COUNT(*) > 2-- Wrong
SELECT MaLop, COUNT(*)
FROM SinhVien;
-- Must include GROUP BY
SELECT MaLop, COUNT(*)
FROM SinhVien
GROUP BY MaLop;Always check with SELECT before running UPDATE or DELETE:
SELECT *
FROM SinhVien
WHERE MaSV = 'SV001';14 · REVIEW CHECKLIST
Students should self-assess before the practical exam: