Docs

Topic 9 · Databases

Databases and SQL

Tables, records, fields, data types, and primary keys, and how to query a single-table database with SQL.

Lesson 1 of 1Syllabus 9.1updated October 31, 2026

Tables, records, and fields

A database is a structured way to store data so it can be searched, sorted, and updated easily. A single-table database stores everything in one table.

Each row is a record: all the data about one thing (here, one pet). Each column is a field: one piece of data stored for every record (here, the pet's name, type, and so on).

Table: Pets
PetIDNameTypeAgeOwner
P01BellaDog3Khan
P02MiloCat5Chen
P03CocoRabbit1Okafor
P04MaxDog7Silva
P05LunaCat2Patel
P06RockyDog4Chen
6 records, each with 5 fields.

Data types for fields

Each field is given a data type, which also works as validation:

Data typeHoldsExample field
Text / alphanumericLetters, digits, and symbolsName, PetID, phone number
CharacterA single characterSize: S, M, or L
BooleanTrue / false (yes / no)Vaccinated
IntegerWhole numbersAge
RealNumbers with decimalsWeight in kg
Date / timeA date or timeDateOfBirth

Primary keys

A primary key is a field that holds a unique value for every record, so each record can be identified. Names are not good keys because two pets could share a name; PetID is unique.

Key idea. No two records can have the same primary key value, and it can never be empty.

SELECT, FROM, WHERE, ORDER BY

SQL (Structured Query Language) is used to search a database. A query names the fields to show (SELECT), the table (FROM), the condition records must meet (WHERE), and how to sort the results (ORDER BY ... ASC or DESC).

In the diagram, the faded rows fail the WHERE condition and the highlighted columns are the ones selected.

Dogs, oldest first
SELECT Name, Age
FROM Pets
WHERE Type = 'Dog'
ORDER BY Age DESC;
Pets
PetIDNameTypeAgeOwner
P01BellaDog3Khan
P02MiloCat5Chen
P03CocoRabbit1Okafor
P04MaxDog7Silva
P05LunaCat2Patel
P06RockyDog4Chen
Result
NameAge
Max7
Rocky4
Bella3

Combining conditions

Use AND and OR to build more specific conditions, and comparison operators such as <, >, and <> (not equal).

Young pets that are not dogs
SELECT Name, Owner
FROM Pets
WHERE Age < 4 AND Type <> 'Dog';
Pets
PetIDNameTypeAgeOwner
P01BellaDog3Khan
P02MiloCat5Chen
P03CocoRabbit1Okafor
P04MaxDog7Silva
P05LunaCat2Patel
P06RockyDog4Chen
Result
NameOwner
CocoOkafor
LunaPatel

SUM and COUNT

COUNT returns how many records match. SUM adds up the values in a numeric field for the matching records.

SELECT COUNT(PetID)
FROM Pets
WHERE Type = 'Cat';
-- Result: 2

SELECT SUM(Age)
FROM Pets
WHERE Owner = 'Chen';
-- Result: 9 (Milo 5 + Rocky 4)

Key terms

Record
A row in a table: all the data about one item.
Field
A column in a table: one category of data.
Primary key
A field that uniquely identifies each record.
SQL
Structured Query Language, used to query databases.

Check yourself

How many records and fields does the Pets table have?Show answer

6 records and 5 fields.

Why is Owner not a suitable primary key?Show answer

It is not unique: Chen owns two pets.

Write a query to show the PetID of every cat, youngest first.Show answer

SELECT PetID FROM Pets WHERE Type = 'Cat' ORDER BY Age ASC;

What does SELECT COUNT(PetID) FROM Pets WHERE Age > 3; return?Show answer

3 (Milo 5, Max 7, Rocky 4).