GGCSE Revision Hub

3.7.2 Structured query language (SQL)

AQA Computer Science 8525Topic 3.7 — Relational databases and structured query languageFoundation & Higher

A database is an organised collection of data, and a relational database stores it in tables linked to each other. SQL is the language you use to ask a database questions. This topic is unusually practical: the SQL you need is a small, fixed set of commands, and the marks come from writing queries that are correct rather than from understanding theory. Learn the commands by their shape, then practise joining two tables together, because that is what the exam asks for.

What you need to know

What SQL is

SQL, Structured Query Language, is the language used to retrieve and modify data in a relational database. The key thing about SQL is that you say what you want rather than how to get it: you ask for the fields you want from the records that match a condition, and the database's query planner works out how to find them. That is why the same query can be fast on one database and slow on another.

Key fact: SQL is the language used to retrieve and modify data in a relational database; you state what you want, not how to get it.

SELECT, FROM and WHERE

Be able to use SQL to retrieve data from a relational database using SELECT, FROM and WHERE. SELECT names the fields you want; FROM names the table; WHERE filters the records by a condition. The order is always SELECT … FROM … WHERE, and the WHERE clause is optional — omit it and you get every record in the table. SELECT * selects every field, which is fine for exploring but poor practice for a final query.

Key fact: SELECT the fields, FROM the table, WHERE the condition — in that order.

Ordering the results

ORDER BY … ASC | DESC sorts the result. ASC means ascending, which is the default, and DESC means descending. So ORDER BY score DESC puts the highest scores first. It goes after the WHERE clause, because it sorts the records you have already filtered. If you sort on a field that is not in the SELECT list, some databases will complain, so keep the two consistent.

Key fact: ORDER BY comes after WHERE; ASC for ascending and DESC for descending.

Joining two tables

Exam questions will require that data is extracted from no more than two tables for any one query, which is the hint about joining. When a query needs data from two tables, you name both in the FROM clause and link them with a JOIN on the key that connects them: JOIN Students ON Results.studentID = Students.studentID. Then you can select fields from both tables and refer to each with the table name when the field names would otherwise clash.

Key fact: To use two tables, JOIN them on the key that links them and use table.field when field names clash.

Inserting data

Be able to use SQL to insert data into a relational database using the commands INSERT INTO table_name (column1, column 2 …) VALUES (value1, value2 …). Note the order: the column names are listed first and then the values, in the same order. Text values are quoted, numbers are not, and the statement ends with a semicolon.

Key fact: INSERT INTO table (columns) VALUES (values), with the columns and values in the same order and text in quotes.

Updating data

To edit records you use UPDATE table_name SET column1 = value1, column2 = value2 … WHERE condition. The WHERE clause is the dangerous and important part: without it, the update applies to every record in the table. So always check the condition before running the statement, and remember the equality test in a WHERE clause is = and not ==.

Key fact: UPDATE table SET columns = values WHERE condition — and always check the WHERE clause, or every row changes.

Deleting data

To remove records you use DELETE FROM table_name WHERE condition. The same warning applies: with no WHERE clause, every record in the table is deleted. An accidental DELETE without a condition is one of the most expensive mistakes in computing, which is why you back up, and why the specification asks you to write the condition carefully.

Key fact: DELETE FROM table WHERE condition — omitting the condition deletes every record.

Writing safe queries

The theme running through all four commands is that a query with a missing or wrong WHERE clause can change far more than you intended. Read the condition back before you run it, test a SELECT with the same WHERE clause first to check that it returns the rows you expect, and keep a backup. These habits are what separate someone who can use SQL from someone who causes an incident with it.

Key fact: Always test the WHERE clause with a SELECT first, and never run an UPDATE or DELETE without checking the condition.

Flashcards

Exam questions

Written in the style of AQA Computer Science, with a mark scheme for each one. No question here is a real past paper question.

mcq1 mark

Which SQL clause restricts the records returned to those that match a condition?

  • SELECT
  • FROM
  • ORDER BY
  • WHERE

Mark scheme: 1 mark for identifying WHERE as the clause that filters records by a condition. SELECT names the fields, FROM names the table and ORDER BY sorts the results.

short3 marks

Explain in English why the WHERE clause is the most important part of an UPDATE or DELETE statement, and what could go wrong without it.

Mark scheme: 1 mark for explaining that WHERE specifies which records the statement applies to. 1 mark for explaining that without a WHERE clause the statement applies to every record in the table. 1 mark for giving the consequences, that every record would be updated or every record deleted, and that this is why you check the condition and back up first.

six6 marks

Explain in English the SQL commands you need to retrieve and to change data in a relational database, and how you would combine two tables in one query.

Mark scheme: Level 1 (1-2 marks): A short answer naming SELECT, FROM and WHERE. Level 2 (3-4 marks): A structured answer covering the four command groups. Level 3 (5-6 marks): A detailed answer explaining that SQL is declarative and that you state what you want, describing SELECT, FROM, WHERE and ORDER BY with the correct order and the use of ASC and DESC, describing INSERT INTO with the columns and values in matching order and text in quotes, describing UPDATE with SET and the critical WHERE clause, describing DELETE FROM with its critical WHERE clause, and explaining how to join two tables by naming both in FROM and linking them with a JOIN on the key, using table.field when names clash, and noting that the exam only requires two tables in any one query.

Other sections in this topic

Practise these questions

Answer them in the interactive view and they get marked against the same mark scheme, one mark at a time, with the reason for each mark.

Open the interactive topic →