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.
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 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
- Database analysis and design: identifying end-user and functional requirements, designing tables with fields and appropriate field types, and choosing primary and foreign keys and validation.
An SQA National 5 Computing Science answer on database analysis and design, covering how to identify end-user and functional requirements, design tables with fields and suitable field types, choose a primary key and a foreign key to link tables, and add validation such as presence, restricted choice, field length and range checks.
- Data types and structures: variables holding character, string, numeric (integer and real) and Boolean values, and the 1-D array as a structure for holding many values of the same type under one name.
An SQA National 5 Computing Science answer on data types and structures, covering the variable types of character, string, integer, real and Boolean, when each is chosen, and how a one-dimensional array stores many values of the same type under a single name accessed by an index.
- Testing with normal, extreme and exceptional test data; the three kinds of error (syntax, execution and logic); and evaluating software for fitness for purpose, efficiency, robustness and readability.
An SQA National 5 Computing Science answer on testing and evaluation, covering the three categories of test data (normal, extreme and exceptional), the three kinds of error (syntax, execution and logic), and the four criteria for evaluating software: fitness for purpose, efficiency, robustness and readability.
- Computational constructs: assignment, arithmetic, comparison and logical operators, concatenation, predefined functions, and the control structures of selection and iteration (fixed and conditional).
An SQA National 5 Computing Science answer on computational constructs, covering assignment, arithmetic, comparison and logical operators, string concatenation, predefined functions, and the control structures of selection (IF) and iteration (fixed and conditional loops) used to build programs in a high-level language.
- The iterative software development process: analysis, design, implementation, testing, documentation and evaluation, and why the process is iterative rather than strictly linear.
An SQA National 5 Computing Science answer on the software development process, covering the six stages of analysis, design, implementation, testing, documentation and evaluation, what is produced at each stage, and why the process is iterative so that developers loop back to earlier stages when problems are found.