CSC 480 · Database Management
Our notes
3 min readCreating tables (DDL)
CREATE TABLE department (
dept_code CHAR(3) PRIMARY KEY,
dept_name VARCHAR(80) NOT NULL UNIQUE,
faculty VARCHAR(80) NOT NULL
);
CREATE TABLE student (
mat_no VARCHAR(20) PRIMARY KEY,
surname VARCHAR(50) NOT NULL,
first_name VARCHAR(50) NOT NULL,
level SMALLINT CHECK (level IN (100, 200, 300, 400, 500)),
dept_code CHAR(3) REFERENCES department(dept_code) ON DELETE RESTRICT
);
CREATE TABLE registration (
mat_no VARCHAR(20) REFERENCES student(mat_no),
course_code VARCHAR(10) REFERENCES course(course_code),
session CHAR(9),
score SMALLINT CHECK (score BETWEEN 0 AND 100),
PRIMARY KEY (mat_no, course_code, session)
);
ALTER TABLE adds or changes columns; DROP TABLE removes a table.
Querying
SELECT first_name, surname FROM student WHERE level = 400 ORDER BY surname;
Patterns: WHERE surname LIKE 'Ok%'; WHERE score BETWEEN 50 AND 59; WHERE dept_code IN ('CSC', 'MTH'); WHERE phone IS NULL.
Joins
SELECT s.surname, c.title, r.score
FROM registration r
JOIN student s ON s.mat_no = r.mat_no
JOIN course c ON c.course_code = r.course_code
WHERE r.session = '2025/2026';
LEFT JOIN keeps students with no registrations (their course columns are NULL): useful for "students who have not registered".
Grouping
SELECT dept_code, COUNT(*) AS students, AVG(level) AS avg_level
FROM student
GROUP BY dept_code
HAVING COUNT(*) > 50;
WHERE filters rows before grouping; HAVING filters groups after. Every non-aggregated column in SELECT must be in GROUP BY.
Subqueries
Students who scored above the class average in CSC 480:
SELECT mat_no, score FROM registration
WHERE course_code = 'CSC 480'
AND score > (SELECT AVG(score) FROM registration WHERE course_code = 'CSC 480');
EXISTS / NOT EXISTS for "has / has not"; IN for membership; correlated subqueries refer to the outer row.
Set operations
UNION (removes duplicates), UNION ALL, INTERSECT, EXCEPT (MySQL supports INTERSECT and EXCEPT from version 8.0.31).
Changing data (DML)
INSERT INTO department VALUES ('CSC', 'Computer Science', 'Computing');
UPDATE student SET level = level + 100 WHERE level < 500;
DELETE FROM registration WHERE session = '2019/2020';
Always test an UPDATE or DELETE's WHERE clause with a SELECT first.
Views and permissions
CREATE VIEW hostel_list AS SELECT mat_no, surname, level FROM student;
GRANT SELECT ON hostel_list TO hostel_office;
SQL injection and prepared statements
Building SQL by joining strings with user input lets an attacker change the query (typing ' OR '1'='1 into a login form). Prepared statements send the SQL and the values separately, so input is never treated as SQL:
$stmt = $pdo->prepare('SELECT * FROM student WHERE mat_no = ?');
$stmt->execute([$matNo]);
This is the single most important habit for any web app that uses a database.
Worked query: grade point average
With a grade_point column (A = 5 ... F = 0) and units in course, a student's GPA for a session:
SELECT r.mat_no, SUM(c.units * r.grade_point) / SUM(c.units) AS gpa
FROM registration r JOIN course c ON c.course_code = r.course_code
WHERE r.session = '2025/2026'
GROUP BY r.mat_no;
Check by hand: courses of 3, 3 and 2 units with points 5, 4 and 3 give (15 + 12 + 6)/8 = 4.125.
Exams usually ask: write CREATE TABLE with keys and constraints; SQL for English queries with joins, grouping and subqueries; WHERE versus HAVING; INNER versus LEFT JOIN; explain and prevent SQL injection.
Common mistake: using WHERE with an aggregate (WHERE COUNT(*) > 50). Conditions on aggregates go in HAVING.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick PostgreSQL tutorial: The SQL language
(opens postgresql.org in a new tab)
-
PostgreSQL tutorial: Querying a table
(opens postgresql.org in a new tab)
-
CS50's Introduction to Databases with SQL (free course: notes, lectures, problem sets)
(opens cs50.harvard.edu in a new tab)
CS50, CS50's Introduction to Databases with SQL (free course: notes, lectures, problem sets), CC BY-NC-SA 4.0 · licence
Watch
-
Introduction to Structured Query Language (SQL)
9 min · ≈ 41 MB · about ₦23
(opens youtube.com in a new tab)
-
GROUP BY and HAVING Clause in SQL
8 min · ≈ 36 MB · about ₦20
(opens youtube.com in a new tab)
-
Joins in SQL
24 min · ≈ 108 MB · about ₦61
(opens youtube.com in a new tab)
-
SQL Explained in 100 Seconds
2 min · ≈ 9 MB · about ₦5
(opens youtube.com in a new tab)
-
SQL Tutorial - Full Database Course for Beginners
4 h 21 min · ≈ 1.1 GB · about ₦660
(opens youtube.com in a new tab)
-
CS50 SQL - Lecture 0 - Querying
1 h 19 min · ≈ 356 MB · about ₦200
(opens youtube.com in a new tab)
-
CS50 SQL - Lecture 1 - Relating
1 h 42 min · ≈ 459 MB · about ₦258
(opens youtube.com in a new tab)
-
CS50x - Lecture 7 - SQL
2 h 16 min · ≈ 612 MB · about ₦344
(opens youtube.com in a new tab)
-
CMU Database Systems - 02 Advanced SQL (Fall 2018)
1 h 21 min · ≈ 365 MB · about ₦205
(opens youtube.com in a new tab)
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.