Skip to main content
EnglandComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.88 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 dot point is asking
  2. Retrieving data with SELECT
  3. Selecting all fields
  4. Inserting, updating and deleting
  5. Filtering with conditions
  6. Sorting and combining clauses
  7. Try this

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 \ne.

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 ==, \ne, <<, >>, \le or \ge, 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, author
FROM Books
WHERE year > 2015
ORDER 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

Sources & how we know this