UNIHIVE

Study Hub UNIPORT CSC 480

CSC 480 · Database Management

SQL: defining, querying and changing data

Data definition (CREATE TABLE with constraints), SELECT with WHERE, joins, GROUP BY and HAVING, subqueries, set operations, INSERT, UPDATE, DELETE, views, and prepared statements against SQL injection.

Our notes

3 min read

Creating 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

Watch

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.

All CSC 480 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.