Databases and SQL
A single-table database: its records and fields, their data types and validation, a primary key, and the SQL used to ask the table questions.
WATCH IT EXPLAINED
EXPLORE IT IN 3D
The records office
A library's records: a drawer of index cards on the desk and the database on the wall. File records, give the table its key, types and rules — then ask it questions in SQL and watch the answers come out.
Tables, records and fields
A table holds data about one kind of thing: each row is a record, each column is a field.
A single-table database keeps data about one kind of thing in one table. Think of a school library's list of books.
| Record | Field | |
|---|---|---|
| In the table | one row | one column |
| Holds | all the data about one item, here one book | one piece of data, with a name and a data type, such as Pages |
The Book table below has 7 fields and 8 records. To count them, count the columns and the rows.
Watch out:The heading row only names the fields. It is not a record.
Choosing a primary key
A primary key is a field that picks out exactly one record.
A primary key is a field whose value is different for every record. It is never empty. So one key value picks out exactly one record, like your student number at school.
Remember:The key must stay unique for every record the table could ever hold, not just the records it holds today.
That is why a made-up code such as BookID is used, not a title:
- Two books can have the same title.
- A library can own two copies of one book.
Try it: test each field as the key, then add a second copy of Eagle Hunter.
Data types and validation
Give each field the data type that fits its data, and validation rules that refuse silly data.
Each field gets the data type that fits its data.
| Data type | Use it for |
|---|---|
| text/alphanumeric | names; also codes and phone numbers |
| character | one single character |
| Boolean | yes or no |
| integer | whole numbers |
| real | numbers with a decimal part |
| date/time | dates and times |
Watch out:A code or a phone number is text, not a number. It can start with 0 or contain letters, and nobody does sums with it.
Defining a table also means giving its fields validation rules (checks made when data is entered). Data that is not sensible is refused.
Field Type Example Validation
BookID Text B001 format: B then 3 digits
Title Text Code Club presence
Genre Text Novel none
Shelf Character A format: A, B or C
Pages Integer 320 more than 0
Rating Real 4.5 range: 0 to 5
OnLoan Boolean TRUE none
DateAdded Date/time 12/09/2026 noneSELECT and FROM: choosing fields
SELECT names the fields you want; FROM names the table they come from.
An SQL query (Structured Query Language: a way to ask a database for data) is written like this:
SELECT, then the fields you want, in the order you want them, separated by commas.FROM, then the name of the table.- A semicolon
;at the end of the statement.
SELECT *means every field. Use it only when every field is wanted.- Spell each field name exactly as it is in the table.
The query below outputs the title and pages of all 8 books.
SELECT Title, Pages
FROM Book;WHERE: choosing records
WHERE keeps only the records that pass a test.
A test compares a field with a value. The database checks the records one at a time. A record is output only if the test is TRUE for it.
| Operator | Means |
|---|---|
= | equal to |
< and > | less than, greater than |
<= and >= | less than or equal to, greater than or equal to |
<> | not equal to |
- Text values go in quotes:
'Novel'and"Novel"are both accepted. - Numbers,
TRUEandFALSEhave no quotes.
The query below outputs 4 of the 8 records.
SELECT Title, Pages
FROM Book
WHERE Pages > 200;AND and OR
AND needs both tests to be TRUE; OR needs at least one.
Two tests can be joined in one WHERE.
| AND | OR | |
|---|---|---|
| Keeps a record if | both tests are TRUE | at least one test is TRUE |
| Usually gives | fewer records | more records |
A range on one field needs AND: Pages >= 100 AND Pages <= 300.
Watch out:One field cannot hold two different values at once. So Genre = 'Novel' AND Genre = 'Science' outputs no records. You wanted OR.
Each row of the figure reads like one row of a truth table.
SELECT BookID, Title
FROM Book
WHERE Genre = 'Novel' AND OnLoan = FALSE;
SELECT BookID, Title
FROM Book
WHERE Genre = 'Novel' OR OnLoan = FALSE;ORDER BY: sorting the output
ORDER BY sorts the output; it is ascending unless you write DESC.
ORDER BY sorts the output by a field.
| ASC (ascending) | DESC (descending) | |
|---|---|---|
| Text | A to Z | Z to A |
| Numbers | smallest first | largest first |
- No direction written? The output is sorted in ascending order.
- ORDER BY comes last, after WHERE.
Remember:ORDER BY changes only the order the results are shown in. The records stored in the table do not move.
The query below starts with Snow Leopard, the Novel with the highest rating.
SELECT Title, Rating
FROM Book
WHERE Genre = 'Novel'
ORDER BY Rating DESC;SUM and COUNT
COUNT and SUM each give one single value, not a list of records.
COUNT(*)counts the records that pass the WHERE.SUM(Field)adds up a numeric field over those records.
Write them after SELECT, in place of the field names.
Example:There are 3 Science books. Their pages add up to 96 + 210 + 150 = 456.
Watch out:SUM only makes sense on a numeric field, such as Pages or Rating.
SELECT COUNT(*)
FROM Book
WHERE Genre = 'Science';
SELECT SUM(Pages)
FROM Book
WHERE Genre = 'Science';What will this query output?
To find a query's output, do FROM, then WHERE, then ORDER BY, then SELECT.
Work out the output by hand. Apply the clauses in this order:
FROM: find the table.WHERE: cross out every record that fails.ORDER BY: sort the records that are left.SELECT: keep only the named fields, in the order they are listed.
Write exactly those values, with no extra fields.
Example:For the query below, 4 records are left, and the output starts with Code Club.
SELECT Title, Genre
FROM Book
WHERE Shelf = 'C' OR Pages < 100
ORDER BY Pages DESC;Writing SQL in the exam
SQL is marked keyword by keyword and field by field, so small slips lose marks.
Write the clauses in this fixed order:
| Clause | What to write |
|---|---|
SELECT | only the fields the question asks for, in order |
FROM | the exact table name |
WHERE | field, operator, value; AND: both, OR: at least one |
ORDER BY | a field, then ASC or DESC |
; | at the end of the statement |
- Use the exact field and table names the question gives.
- Put text values in quotes, such as
'Novel'. Numbers, TRUE and FALSE have no quotes.
Exam:When a question asks for the output, write only the selected fields, in their order, sorted as ORDER BY says.
- ProductID
- ProductName
- Category
- Price
- Stock
- CustomerID
- FirstName
- LastName
- Town
- JoinDate
- SaleID
- CustomerID
- ProductID
- Quantity
- SaleDate
TASKList the ProductName and Price of every product in the Input category.
KEY TERMS
CHECK YOURSELF
1.What makes a good primary key?
2.Which data type should store a phone number?
3.Which SQL returns names of students with a mark above 50, highest first?
4.A table has a heading row and 8 rows of data under it. How many records does it hold?
5.Which data type suits a field that holds one shelf letter, such as A?
6.WHERE Genre = 'Novel' AND Genre = 'Science' is run on a table of books. Which records are output?
7.A query ends ORDER BY Rating, with no direction. How is the output sorted?
8.Which query outputs one number, not a list of records?