How do you create, query and update a relational database in SQL?
Implement and query a relational database in SQL, using data definition (CREATE, ALTER) and data manipulation (SELECT, INSERT, UPDATE, DELETE) with joins, aggregate functions, GROUP BY and HAVING, subqueries, wildcards and computed columns.
A focused answer to the SQA Advanced Higher Computing Science content on SQL, covering data definition and data manipulation, multi-table joins, aggregate functions with GROUP BY and HAVING, subqueries, wildcards and computed columns.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
The SQA wants you to read and write SQL: create tables, change them, insert, update and delete data, and above all query data with multi-table joins, aggregate functions, grouping, subqueries, wildcards and computed columns. SQL is examined in the question paper and is the implementation language for a database project.
Data definition: building the structure
Data definition language (DDL) creates and changes the database structure. CREATE TABLE defines a table with its columns, data types and constraints, including the primary key and any foreign keys; ALTER TABLE changes an existing table, for example adding a column or a constraint.
CREATE TABLE Customer (
customerID INTEGER PRIMARY KEY,
name VARCHAR(40),
town VARCHAR(30)
);
Data manipulation: changing data
Data manipulation language (DML) works with the rows. INSERT adds a row, UPDATE changes existing rows matched by a WHERE clause, and DELETE removes matched rows. Always include a WHERE clause on UPDATE and DELETE unless you really mean to change every row.
INSERT INTO Customer VALUES (1, 'Ali', 'Perth');
UPDATE Customer SET town = 'Dundee' WHERE customerID = 1;
DELETE FROM Customer WHERE customerID = 1;
Querying with SELECT and joins
A SELECT lists the columns to return, the tables in FROM, a WHERE condition, and an optional ORDER BY. To combine related tables you join them on the foreign-key match, so a sale is paired with the customer it belongs to.
Aggregates, grouping and HAVING
Aggregate functions summarise many rows into one value: SUM, COUNT, AVG, MIN and MAX. GROUP BY splits rows into groups so the aggregate is computed per group, and HAVING filters those groups (unlike WHERE, which filters individual rows before grouping).
Subqueries, wildcards and computed columns
A subquery is a SELECT nested inside another query, often in the WHERE clause, to compare against a value the outer query cannot name directly (for example sales above the average). Wildcards with LIKE match text patterns, where % stands for any sequence of characters. A computed column calculates a value in the SELECT list, such as price * quantity AS lineTotal.
SELECT name FROM Customer WHERE town LIKE 'P%';
SELECT name FROM Sale WHERE amount > (SELECT AVG(amount) FROM Sale);
Try this
Q1. Name the SQL clause used to filter groups created by GROUP BY. [1 mark]
- Cue. HAVING.
Q2. State which aggregate function counts the number of rows in a group. [1 mark]
- Cue. COUNT.
Exam-style practice questions
Practice questions written in the style of SQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AH style: join + aggregate5 marksTables Customer(customerID, name) and Sale(saleID, customerID, amount) are linked by customerID. Write an SQL query to list each customer's name and their total sales, for customers whose total exceeds 1000, ordered by total descending.Show worked answer →
SELECT name, SUM(amount) AS total (1 mark for the aggregate with an alias) FROM Customer, Sale (or with an explicit JOIN) WHERE Customer.customerID = Sale.customerID (1 mark for the join condition) GROUP BY name (1 mark, grouping by the non-aggregated column) HAVING SUM(amount) > 1000 (1 mark, filtering the grouped totals with HAVING not WHERE) ORDER BY total DESC (1 mark). Markers reward the join, the SUM, the GROUP BY, the HAVING for the aggregated condition, and the correct ORDER BY.
AH style: WHERE vs HAVING2 marksExplain the difference between WHERE and HAVING in an SQL query that uses GROUP BY.Show worked answer →
WHERE filters individual rows before they are grouped, so it cannot refer to an aggregate (1 mark). HAVING filters the groups after aggregation, so it is the clause used to test an aggregate such as SUM or COUNT against a condition (1 mark). For example, WHERE amount > 0 removes rows first; HAVING SUM(amount) > 1000 removes whole groups whose total is too small.
Related dot points
- Normalise data from unnormalised form to first, second and third normal form, using functional dependency and primary, foreign and composite keys to remove redundancy and anomalies.
A focused answer to the SQA Advanced Higher Computing Science content on normalisation, covering unnormalised form, first, second and third normal form, functional dependency, and how normalisation removes redundancy and update anomalies.
- Analyse data requirements and design a relational database using entities, attributes and relationships, entity-relationship diagrams, a data dictionary, and primary, foreign and composite keys.
A focused answer to the SQA Advanced Higher Computing Science content on database analysis and design, covering entities, attributes and relationships, entity-relationship diagrams, the data dictionary, and primary, foreign and composite keys.
- Test SQL queries against expected results, maintain referential integrity, and evaluate a database solution for fitness for purpose and data integrity.
A focused answer to the SQA Advanced Higher Computing Science content on database testing and evaluation, covering testing SQL queries against expected results, referential integrity, entity integrity and evaluating a database for fitness for purpose.
- Describe and implement object-oriented programming using classes, objects, instantiation, attributes and methods, encapsulation, inheritance, and polymorphism through method overriding.
A focused answer to the SQA Advanced Higher Computing Science content on object-oriented programming, covering classes and objects, instantiation, encapsulation, inheritance and polymorphism through method overriding, with UML class diagrams.