Revision Notes
OxfordAQA IGCSE Computer Science (9210): Structured Query Language (SQL) – Revision Notes
Revision notes for OxfordAQA IGCSE Computer Science 9210 section 3.7.3: SELECT, FROM, WHERE, INSERT INTO, UPDATE and DELETE FROM with worked examples and a self-test.
- Subject
- Computer Science
- Level
- IGCSE
- Topic
- Topic 7: Relational databases and structured query language (SQL)
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to OxfordAQA IGCSE Computer Science (9210), For teaching from September 2017, exams from May/June 2019. Official specification .
Syllabus page (what it covers and how it is assessed): OxfordAQA IGCSE Computer Science.
Syllabus points this page covers
9210
- 3.7.3 Structured Query Language (SQL)
This page covers 1 of the 3 subtopics in topic 7, Relational databases and structured query language (SQL).
Found an error? Report a correction.
Need help with this topic? Request a free trial class for IGCSE Computer Science (9210).
Condensed notes for section 3.7.3 Structured Query Language (SQL) of OxfordAQA International GCSE Computer Science (9210). Worked queries are in the SQL study guide.
What the specification asks
- Retrieve data with SELECT, FROM, WHERE. A query uses no more than two tables.
- Insert data with INSERT INTO. Only the form that gives all of the values of a record is needed.
- Edit and delete data with UPDATE and DELETE FROM.
The commands
| Task | Form |
|---|---|
| Retrieve | SELECT fields FROM table WHERE condition; |
| Retrieve every field | SELECT * FROM table WHERE condition; |
| Add a record | INSERT INTO table VALUES (v1, v2, ...); |
| Change records | UPDATE table SET field = value WHERE condition; |
| Delete records | DELETE FROM table WHERE condition; |
Conventions
- Keywords in capitals; statement ends with
;. - Text values in quotation marks (
'Leeds'); numbers without. - Comparison operators:
=,<>,<,>,<=,>=. - Join conditions with
AND(both true) andOR(at least one true).
Example tables
Product
| ProductID | ProductName | Price | Stock | SupplierID |
|---|---|---|---|---|
| 101 | Gel Pen | 1.20 | 150 | 1 |
| 102 | Notebook | 2.50 | 80 | 2 |
| 103 | Backpack | 18.99 | 12 | 3 |
| 104 | Ruler | 0.80 | 200 | 1 |
Supplier
| SupplierID | SupplierName | Town |
|---|---|---|
| 1 | NorthPens | Leeds |
| 2 | PaperHub | Bristol |
| 3 | BagWorld | Leeds |
Worked queries
| Task | SQL | Output |
|---|---|---|
| Names and prices of all products | SELECT ProductName, Price FROM Product; |
Gel Pen 1.20, Notebook 2.50, Backpack 18.99, Ruler 0.80 |
| Products costing more than 2 | SELECT ProductName FROM Product WHERE Price > 2; |
Notebook, Backpack |
| Products with supplier 1 and stock over 100 | SELECT ProductName FROM Product WHERE SupplierID = 1 AND Stock > 100; |
Gel Pen, Ruler |
| Products with stock under 20 or price under 1 | SELECT ProductName FROM Product WHERE Stock < 20 OR Price < 1; |
Backpack, Ruler |
Two-table query
SELECT Product.ProductName, Supplier.SupplierName
FROM Product, Supplier
WHERE Product.SupplierID = Supplier.SupplierID
AND Supplier.Town = 'Leeds';
- The first condition matches the foreign key to the primary key.
- The second condition filters the records.
- Without the matching condition every product is paired with every supplier (4 × 3 = 12 pairs here).
Output (in any order): Gel Pen with NorthPens, Ruler with NorthPens and Backpack with BagWorld.
INSERT, UPDATE and DELETE
INSERT INTO Product VALUES (105, 'Folder', 1.10, 0, 2);
UPDATE Product SET Price = 1.30 WHERE ProductID = 101;
DELETE FROM Product WHERE Stock = 0;
- INSERT: values in the same order as the fields, all of them.
- UPDATE: only the records that meet the WHERE condition are changed.
- DELETE: only the records that meet the WHERE condition are removed.
- No WHERE means every record:
UPDATE Product SET Price = 1.30;changes every price, andDELETE FROM Product;empties the table.
After the three statements above (starting from the four-record table): the Folder is added, then deleted because its stock is 0, and the Gel Pen costs 1.30. The final table holds products 101, 102, 103 and 104.
Writing a query: steps
- Fields to show: after SELECT.
- Table or tables: after FROM.
- Conditions for the records wanted: after WHERE, joined by AND or OR.
- Two tables: add the matching condition.
- Check quotation marks and the semicolon.
Model answers
Write a query to show the names of all products with fewer than 100 items in stock. [3 marks]
SELECT ProductName
FROM Product
WHERE Stock < 100;
SELECT with the correct field [1]; FROM with the correct table [1]; WHERE with the correct condition [1].
Write a statement to remove product 104. [2 marks]
DELETE FROM Product
WHERE ProductID = 104;
Explain what is wrong with SELECT ProductName FROM Product, Supplier WHERE Town = 'Leeds';. [2 marks] There is no
condition that matches Product.SupplierID with Supplier.SupplierID, so every product is paired with every supplier, and the
result is not the products that come from Leeds suppliers.
Reading a query aloud
SELECT ProductName, Price FROM Product WHERE Stock = 0; reads as: “show the name and price of every product whose stock is
0”. Translating a query into a sentence is a quick way of checking that it asks the right question.
Exam traps
- No WHERE in UPDATE or DELETE FROM.
==or “equals” instead of=.- Quotation marks missing around text, or added around numbers.
- Half conditions:
Price > 2 AND < 5should bePrice > 2 AND Price < 5. Town = 'Leeds' OR 'Bristol'should repeat the field:Town = 'Leeds' OR Town = 'Bristol'.- AND instead of OR (a record cannot be in two towns at once).
- No matching condition in a two-table query.
- INSERT values in the wrong order.
- SELECT and WHERE mixed up. SELECT chooses the fields; WHERE chooses the records.
- Commands outside the specification, such as ORDER BY and JOIN.
Key terms
| Term | Meaning |
|---|---|
| Query | A request for data from a database |
| Condition | A test that a record must pass, written after WHERE |
Wildcard * |
Means every field |
| Matching condition | A WHERE condition that links a foreign key to a primary key |
Self-test
Use the Product table above.
- Write a query to show the ProductName of every product.
- Write a query to show all fields of products with a price below 1.
- State the output of
SELECT ProductName FROM Product WHERE Stock >= 150;. - Write a query to show the names of products with supplier 2 or a price over 10.
- Write a statement to add product 106, Eraser, price 0.45, stock 90, supplier 2.
- Write a statement to set the stock of product 103 to 20.
- Write a statement to delete all products with a price over 10.
- State what
UPDATE Product SET Stock = 0;does. - State what must be added to a two-table query to link the tables.
- State the symbol used for “not equal to”.
Answers:
SELECT ProductName FROM Product;SELECT * FROM Product WHERE Price < 1;- Gel Pen and Ruler.
SELECT ProductName FROM Product WHERE SupplierID = 2 OR Price > 10;INSERT INTO Product VALUES (106, 'Eraser', 0.45, 90, 2);UPDATE Product SET Stock = 20 WHERE ProductID = 103;DELETE FROM Product WHERE Price > 10;- It sets the stock of every product to 0, because there is no WHERE.
- A condition that matches the foreign key to the primary key.
<>.
More resources on this topic
Related resources
-
Practice questions
OxfordAQA IGCSE Computer Science: Relational Databases and SQL — Practice Questions (9210)
Original practice questions with worked answers on database terms, keys, data types, table design and SQL for OxfordAQA International GCSE 9210.
Computer Science · OxfordAQA · IGCSE
Try questions -
Revision notes
OxfordAQA IGCSE Computer Science: Relational Databases and SQL — Revision Notes
Recall notes on tables, records, fields, keys, data types, table design and SQL SELECT, INSERT INTO, UPDATE and DELETE FROM for OxfordAQA IGCSE 9210.
Computer Science · OxfordAQA · IGCSE
Revise topic -
Study guides
OxfordAQA IGCSE Computer Science (9210): Relational databases and SQL – Study Guide
OxfordAQA International GCSE Computer Science 9210 study guide: tables, keys, database design and the SQL SELECT, INSERT INTO, UPDATE and DELETE FROM commands.
Computer Science · OxfordAQA · IGCSE
Read guide
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.