How do we retrieve and change data in a database using SQL?
Use SQL to retrieve data with SELECT, FROM, WHERE and ORDER BY, and to insert, update and delete records.
A focused answer to AQA GCSE Computer Science 3.7, covering SQL to retrieve data with SELECT, FROM, WHERE and ORDER BY, and to insert, update and delete records.
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
AQA wants you to write SQL to retrieve data using SELECT, FROM, WHERE and ORDER BY, and to change data using INSERT, UPDATE and DELETE.
Retrieving data with SELECT
SELECT name, age
FROM Students
WHERE age > 15
ORDER BY name ASC
This returns the name and age of every student over 15, sorted by name. The WHERE condition can combine tests with AND and OR, for example WHERE age > 15 AND year = 11, and can use comparison operators such as , , and .
Selecting all fields
Using SELECT * returns every field in the table, rather than naming each one. This is convenient for quick checks, but naming the fields you actually need is clearer and avoids returning data you do not want.
Inserting, updating and deleting
INSERT INTO Students (name, age) VALUES ('Sam', 16)
UPDATE Students SET age = 17 WHERE name = 'Sam'
DELETE FROM Students WHERE age < 11
The single most important habit is to include WHERE in every UPDATE and DELETE: without it, the statement applies to the entire table, changing or deleting every record.
Filtering with conditions
The WHERE clause is where most of the thinking goes. A condition compares a field with a value using an operator such as , , , , or , and conditions can be combined. For example WHERE year > 2015 AND price < 10 returns only records that satisfy both tests, while WHERE author = 'Dahl' OR author = 'Blyton' returns records matching either. Text values are wrapped in quotation marks; numbers are not. Getting the operator and the logic right is the difference between returning the rows you want and returning none or all of them.
Sorting and combining clauses
ORDER BY sorts the results once they have been selected and filtered, ascending by default or descending with DESC, for example ORDER BY price DESC to list the most expensive first. The clauses must always appear in the fixed order SELECT, FROM, WHERE, ORDER BY: SELECT chooses the columns, FROM names the table, WHERE filters the rows, and ORDER BY arranges what is left. A query that retrieves the three cheapest books by one author therefore reads SELECT title, price FROM Books WHERE author = 'Dahl' ORDER BY price ASC, applying each clause in turn.
Try this
Q1. Write an SQL query to return all fields of every record in a table called Members. [2 marks]
- Cue.
SELECT * FROM Members.
Q2. Write an SQL query to return the name of every member aged over 18, sorted by name. [3 marks]
- Cue.
SELECT name FROM Members WHERE age > 18 ORDER BY name.
Exam-style practice questions
Practice questions written in the style of AQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AQA 20204 marksA table called Books has the fields title, author, year and price. Write an SQL query to return the title and author of every book published after 2015, sorted by title in ascending order.Show worked answer →
A correct query selects the two named fields, filters on the year, and sorts:
SELECT title, authorFROM BooksWHERE year > 2015ORDER BY title ASC
Markers reward the two correct fields after SELECT, the correct table after FROM, the condition year > 2015 in the WHERE clause, and ORDER BY title (ASC is the default). Listing the wrong fields or omitting the WHERE loses marks.
AQA 20224 marksUsing the Books table, write SQL to (a) increase the price of the book titled 'Matilda' to 7.99, and (b) delete every book published before 1990. Explain why the WHERE clause is essential in both statements.Show worked answer →
(a) UPDATE Books SET price = 7.99 WHERE title = 'Matilda'. (b) DELETE FROM Books WHERE year < 1990.
The WHERE clause is essential because without it the statement applies to every record in the table: the UPDATE would change the price of all books to 7.99, and the DELETE would remove every book. WHERE restricts the change to only the records that match.
Markers reward correct UPDATE and DELETE syntax with the right conditions, quotation marks around the text 'Matilda', and the explanation that omitting WHERE affects the whole table.
Related dot points
- Understand relational database concepts including tables, records, fields, primary keys and foreign keys, and why data is organised this way.
A focused answer to AQA GCSE Computer Science 3.7, covering relational database concepts including tables, records, fields, primary keys and foreign keys, and why data is organised this way.
- Use one-dimensional and two-dimensional arrays and records to store collections of data, and access elements using indexes and field names.
A focused answer to AQA GCSE Computer Science 3.2.6, covering one-dimensional and two-dimensional arrays and records, and accessing elements using indexes and field names.
- Use the three programming constructs of sequence, selection and iteration, including definite and indefinite iteration, and nest them.
A focused answer to AQA GCSE Computer Science 3.2.2, covering the three programming constructs of sequence, selection and iteration, the difference between definite and indefinite iteration, and nesting.
- Read from and write to a text file, including opening, reading line by line, writing and closing a file, so that data persists after the program ends.
A focused answer to AQA GCSE Computer Science 3.2.10, covering reading from and writing to a text file, including opening, reading line by line, writing and closing a file so data persists.
Sources & how we know this
- AQA GCSE Computer Science (8525) specification — AQA (2020)