Skip to main content
ScotlandComputer ScienceSyllabus dot point

How do we use SQL to search, sort, add, change and delete data in a database?

SQL implementation: searching and sorting with SELECT, FROM, WHERE and ORDER BY, and changing data with INSERT, UPDATE and DELETE, then testing and evaluating the database.

An SQA National 5 Computing Science answer on implementing a database in SQL, covering how to search and sort records using SELECT, FROM, WHERE and ORDER BY, how to add, change and remove data with INSERT, UPDATE and DELETE, and how to test and evaluate that the database meets its requirements.

Generated by Claude Opus 4.811 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this key area is asking
  2. Searching and sorting with SELECT
  3. Changing data: INSERT, UPDATE and DELETE
  4. Testing and evaluating the database
  5. Why SQL matters
  6. How this key area is examined
  7. For the official course specification

What this key area is asking

The SQA wants you to read and write SQL to search and sort records (SELECT, FROM, WHERE, ORDER BY) and to add, change and remove data (INSERT, UPDATE, DELETE), then to test and evaluate that the database meets its requirements.

Searching and sorting with SELECT

The order of the clauses matters: SELECT, then FROM, then WHERE, then ORDER BY. The WHERE condition uses comparison operators (=, <, >, <=, >=, <>) and can combine conditions with AND and OR, just like a condition in a program.

Changing data: INSERT, UPDATE and DELETE

These three statements modify the database rather than just reading it, so they must be used carefully. An INSERT adds one row of data. An UPDATE without a WHERE clause changes every record; a DELETE without a WHERE clause removes every record. The WHERE clause is therefore essential whenever you mean to affect only some records.

Testing and evaluating the database

After implementing the SQL, the database is tested to check it meets the functional requirements: do the queries return the right records, in the right order, and do the INSERT, UPDATE and DELETE statements change exactly the intended data? It is then evaluated for fitness for purpose - whether it does everything the requirements asked for - just as a program is evaluated. Testing with data that should and should not match a query confirms the WHERE conditions are correct.

Why SQL matters

SQL is how a relational database is actually used: every search, report, addition and change is an SQL statement. Getting the clauses right - selecting the correct fields, filtering with WHERE, sorting with ORDER BY, and always limiting UPDATE and DELETE with WHERE - is the practical core of this area and is examined by writing and reading statements.

How this key area is examined

Questions ask you to write an SQL statement to meet a description, or read a statement and say what it returns or changes. Expect SELECT queries with WHERE and ORDER BY, and INSERT, UPDATE and DELETE statements. The reliable marks come from listing the correct fields, writing a correct WHERE condition, choosing ascending or descending correctly, and never omitting WHERE on UPDATE or DELETE.

For the official course specification

The SQA publishes the full National 5 Computing Science course specification, specimen question papers and coursework tasks at sqa.org.uk. Always revise from the current specification and SQA past papers, because question style and terminology are board-specific.

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.

SQA N5 style4 marksA table Pupil has fields name, year and house. Write an SQL statement to display the name and house of every pupil in year 4, sorted by name in ascending order.
Show worked answer →

Four marks for the right fields, table, condition and sort.

SQL:

SELECT name, house
FROM Pupil
WHERE year = 4
ORDER BY name ASC;

Markers reward SELECT with the two correct fields, FROM the correct table, a WHERE condition that matches year 4, and ORDER BY name in ascending order. Selecting all fields with * when only two were asked for, or omitting the ORDER BY, would lose marks.

SQA N5 style3 marksWrite SQL to change the house of the pupil whose name is 'Ben Ross' to 'North', and explain why a WHERE clause is essential here.
Show worked answer →

Two marks for the correct UPDATE and one for the explanation.

SQL:

UPDATE Pupil
SET house = 'North'
WHERE name = 'Ben Ross';

Explanation: the WHERE clause limits the change to the one matching record. Without WHERE, the UPDATE would change the house of every pupil in the table, not just Ben Ross.

Markers reward UPDATE ... SET ... with the correct field and value, a WHERE that selects the right record, and the point that omitting WHERE updates every row.

Related dot points

Sources & how we know this