SQL and the DBMS

A-Level Computer Science · Databases and Data Security

Data Definition Language

CREATE TABLE, ALTER TABLE and constraints such as PRIMARY KEY, FOREIGN KEY and NOT NULL. DDL defines the structure.

Data Manipulation Language

SELECT … FROM … WHERE … ORDER BY for queries, plus INSERT INTO, UPDATE … SET and DELETE FROM. Aggregate functions COUNT, SUM, AVG, MAX, MIN combine with GROUP BY.

Joining tables

An INNER JOIN … ON matches a foreign key to a primary key and returns only rows that match in both tables. Writing the ON condition is the step that gets marked; forgetting it produces every combination of every row.

A worked pattern

SELECT KHACH_HANG.ten_kh, COUNT(DON_HANG.ma_don)
FROM KHACH_HANG INNER JOIN DON_HANG
ON KHACH_HANG.ma_kh = DON_HANG.ma_kh
GROUP BY KHACH_HANG.ten_kh
ORDER BY COUNT(DON_HANG.ma_don) DESC;

What the DBMS does for you

Beyond storing data: a data dictionary describing the structure; access control so different users see different data; validation of entered values; backup and recovery; and concurrency control, which uses record locking so two users cannot overwrite each other's changes.

Why the DBMS matters

It separates the data from the programs that use it, so changing one application does not require rewriting the data, and several applications can share one consistent set of data.

Worked example: writing three SQL queries

Given the tables: KHACH_HANG(ma_kh, ten_kh, thanh_pho), DON_HANG(ma_don, ngay, ma_kh, tong_tien).

Write SQL to: (a) list the names of customers in Hanoi in alphabetical order; (b) calculate the total order value for each customer; (c) find the customers whose total spending exceeds $10\,000\,000$.

Solution

(a)

SELECT ten_kh
FROM KHACH_HANG
WHERE thanh_pho = 'Hanoi'
ORDER BY ten_kh ASC;

Note: a string must be placed in quotation marks. This is the syntax error that loses the most marks.

(b)

SELECT KHACH_HANG.ten_kh, SUM(DON_HANG.tong_tien)
FROM KHACH_HANG INNER JOIN DON_HANG
ON KHACH_HANG.ma_kh = DON_HANG.ma_kh
GROUP BY KHACH_HANG.ten_kh;

Three things that must be there: the join, the ON condition matching the foreign key to the primary key, and GROUP BY on exactly the field that is not inside the aggregate function.

(c)

SELECT KHACH_HANG.ten_kh, SUM(DON_HANG.tong_tien)
FROM KHACH_HANG INNER JOIN DON_HANG
ON KHACH_HANG.ma_kh = DON_HANG.ma_kh
GROUP BY KHACH_HANG.ten_kh
HAVING SUM(DON_HANG.tong_tien) > 10000000;

The key point of part (c): you must use HAVING, not WHERE. WHERE filters individual ROWS before grouping, so it cannot see the total; HAVING filters GROUPS after the total has been computed. Writing WHERE SUM(...) > … is both a syntax error and a thinking error.

Common mistake

Writing INNER JOIN but forgetting the ON clause. Without a join condition, the database pairs EVERY row of one table with EVERY row of the other: $500$ customers and $2\,000$ orders become a million meaningless rows. This is the error that loses the most marks in an SQL question.

Key takeaway

DDL defines structure, DML manipulates data. Joining tables always needs an ON clause matching foreign key to primary key. WHERE filters rows, HAVING filters groups. And remember the DBMS also handles access rights, backup and record locking.

← Relational databases and normalisation · All A-Level Computer Science lessons · Practice A-Level Computer Science questions (free, instant scoring) · Security, privacy and the ethics of data →