Database tables, fields and records
A shop has to know what it sells, what each item costs and how many are left. It keeps this in a database: an organised collection of data. One of its tables is Stock; grey shows each field’s data type.
| Code | ItemName | Category | Price | Quantity | Available |
|---|---|---|---|---|---|
| text | text | text | real | integer | Boolean |
| P01 | Pen | Stationery | 1.50 | 12 | TRUE |
| P02 | Notebook | Stationery | 3.00 | 4 | TRUE |
| P03 | Cable | Electronics | 6.50 | 0 | FALSE |
| P04 | Mouse | Electronics | 12.00 | 5 | TRUE |
| P05 | Folder | Stationery | 2.00 | 8 | TRUE |
Stock: the same five records continue across both panels. Code identifies the matching row.
| Code | ItemName | Category |
|---|---|---|
| text | text | text |
| P01 | Pen | Stationery |
| P02 | Notebook | Stationery |
| P03 | Cable | Electronics |
| P04 | Mouse | Electronics |
| P05 | Folder | Stationery |
Same Code, continued fields:
| Code | Price | Quantity | Available |
|---|---|---|---|
| text | real | integer | Boolean |
| P01 | 1.50 | 12 | TRUE |
| P02 | 3.00 | 4 | TRUE |
| P03 | 6.50 | 0 | FALSE |
| P04 | 12.00 | 5 | TRUE |
| P05 | 2.00 | 8 | TRUE |
Records, fields and the primary key
To use a table you need names for its parts, and one field that never repeats, so each record can be found again.
Record = a row
All the data about one item. This table has 5 records. The heading row is not a record.
Field = a column
One item of data stored for every record, e.g. Price. This table has 6 fields.
Primary key
A field whose value is unique for every record, so it identifies each record. Here: Code.
Choose the most appropriate data type
The type decides what a field can hold and what can be done with it: Quantity can be added up, a phone number cannot. Choose each type from the values and how they are used.
| Data type | Use it for | Stock field |
|---|---|---|
| Text / alphanumeric | letters, codes, or numbers with a leading zero or spaces (e.g. phone numbers) | Code, ItemName, Category |
| Character | a single letter or symbol, e.g. a size code 'S' | – |
| Boolean | only two values: TRUE / FALSE (yes / no) | Available |
| Integer | whole numbers that may be calculated with | Quantity |
| Real | numbers with a fractional part, e.g. money | Price |
| Date / time | a date or a time | – |
Why Stock uses Code
Why not ItemName as the key?
Two items could one day have the same name, so ItemName may not stay unique. Each new item gets an unused Code, such as P01, so Code is always unique.
Why is Code text, not integer?
It contains a letter and is used as an identifier. A phone number is text too: it can start with 0.
Bikes (a hire shop’s table):
BikeID | Type | Gears | DayRate | InService |
|---|---|---|---|---|
| B01 | City | 3 | 8.00 | TRUE |
| B02 | Mountain | 21 | 15.00 | TRUE |
| B03 | City | 7 | 10.00 | FALSE |
| B04 | Road | 18 | 12.50 | TRUE |
1 How many records and how many fields does Bikes have?
2 Which field is the primary key? Why?
3 Give the data type of DayRate and of InService.
- 14 records (rows), 5 fields (columns)
- 2BikeID: its value is unique for every bike (Type repeats: City)
- 3DayRate is real (money has a fractional part); InService is Boolean (only TRUE or FALSE)
Querying with SELECT, FROM and WHERE
You can check five items by eye, but not five thousand. A query asks the table a question for you. Its answer is a smaller table: the fields you name, from the records that match.
Every query reads this one table, Stock (5 records):
| Code | ItemName | Category | Price | Quantity | Available |
|---|---|---|---|---|---|
| P01 | Pen | Stationery | 1.50 | 12 | TRUE |
| P02 | Notebook | Stationery | 3.00 | 4 | TRUE |
| P03 | Cable | Electronics | 6.50 | 0 | FALSE |
| P04 | Mouse | Electronics | 12.00 | 5 | TRUE |
| P05 | Folder | Stationery | 2.00 | 8 | TRUE |
Stock: the same five records continue across both panels. Code identifies the matching row.
| Code | ItemName | Category |
|---|---|---|
| P01 | Pen | Stationery |
| P02 | Notebook | Stationery |
| P03 | Cable | Electronics |
| P04 | Mouse | Electronics |
| P05 | Folder | Stationery |
Same Code, continued fields:
| Code | Price | Quantity | Available |
|---|---|---|---|
| P01 | 1.50 | 12 | TRUE |
| P02 | 3.00 | 4 | TRUE |
| P03 | 6.50 | 0 | FALSE |
| P04 | 12.00 | 5 | TRUE |
| P05 | 2.00 | 8 | TRUE |
Items with no stock
SELECT ItemName FROM Stock WHERE Quantity = 0;
Result
ItemName |
|---|
| Cable |
SELECT ItemName FROM Stock WHERE Quantity = 0;
| Code | ItemNameSELECT | QuantityWHERE | In result? |
|---|---|---|---|
| P01 | Pen | 12 | no |
| P02 | Notebook | 4 | no |
| P03 | Cable | 0 | yes |
| P04 | Mouse | 5 | no |
| P05 | Folder | 8 | no |
ItemName |
|---|
| Cable |
Code and price of the pen: columns follow SELECT order; text values go in quotes
SELECT Code, Price FROM Stock WHERE ItemName = "Pen";
Result
| Code | Price |
|---|---|
| P01 | 1.50 |
SELECT Code, Price FROM Stock WHERE ItemName = "Pen";
| CodeSELECT | ItemNameWHERE | PriceSELECT | In result? |
|---|---|---|---|
| P01 | Pen | 1.50 | yes |
| P02 | Notebook | 3.00 | no |
| P03 | Cable | 6.50 | no |
| P04 | Mouse | 12.00 | no |
| P05 | Folder | 2.00 | no |
| Code | Price |
|---|---|
| P01 | 1.50 |
Using a comparison operator
SELECT ItemName, Quantity FROM Stock WHERE Price >= 3.00;
Greater or equal is one of six comparison operators, and any WHERE condition can use them. Not equal is written <>.
| equal | not equal | less | less or equal | greater | greater or equal | |
|---|---|---|---|---|---|---|
| SQL | = | <> | < | <= | > | >= |
Result
ItemName | Quantity |
|---|---|
| Notebook | 4 |
| Cable | 0 |
| Mouse | 5 |
Without ORDER BY, the rows may come out in any order.
SELECT ItemName, Quantity FROM Stock WHERE Price >= 3.00;
| Code | ItemNameSELECT | PriceWHERE | QuantitySELECT | In result? |
|---|---|---|---|---|
| P01 | Pen | 1.50 | 12 | no |
| P02 | Notebook | 3.00 | 4 | yes |
| P03 | Cable | 6.50 | 0 | yes |
| P04 | Mouse | 12.00 | 5 | yes |
| P05 | Folder | 2.00 | 8 | no |
ItemName | Quantity |
|---|---|
| Notebook | 4 |
| Cable | 0 |
| Mouse | 5 |
Without ORDER BY, the rows may come out in any order.
Greater or equal is one of six comparison operators, and any WHERE condition can use them. Not equal is written <>.
| equal | not equal | less | less or equal | greater | greater or equal | |
|---|---|---|---|---|---|---|
| SQL | = | <> | < | <= | > | >= |
A Boolean field: TRUE and FALSE (and numbers) are not in quotes
SELECT ItemName FROM Stock WHERE Available = FALSE;
Result
ItemName |
|---|
| Cable |
SELECT ItemName FROM Stock WHERE Available = FALSE;
| Code | ItemNameSELECT | AvailableWHERE | In result? |
|---|---|---|---|
| P01 | Pen | TRUE | no |
| P02 | Notebook | TRUE | no |
| P03 | Cable | FALSE | yes |
| P04 | Mouse | TRUE | no |
| P05 | Folder | TRUE | no |
ItemName |
|---|
| Cable |
WHERE Quantity = 0 and WHERE Available = FALSE find the same record.Bikes (a hire shop’s table):
BikeID | Type | Gears | DayRate | InService |
|---|---|---|---|---|
| B01 | City | 3 | 8.00 | TRUE |
| B02 | Mountain | 21 | 15.00 | TRUE |
| B03 | City | 7 | 10.00 | FALSE |
| B04 | Road | 18 | 12.50 | TRUE |
1 Write the result of SELECT BikeID FROM Bikes WHERE Gears > 10;
2 Write a query to show the Type and DayRate of bike B03.
3 Write a query to show the BikeID of every bike that is not in service.
- 1B02, B04
- 2
SELECT Type, DayRate FROM Bikes WHERE BikeID = "B03";gives City 10.00 - 3
SELECT BikeID FROM Bikes WHERE InService = FALSE;gives B03
Filtering with AND and OR
A real question often has two parts: stationery that is running low, or anything electronic or cheap. AND keeps a record only when both conditions are true. OR keeps it when at least one is true, including when both are. Test the whole condition on each record in turn.
Every query reads this one table, Stock (5 records):
| Code | ItemName | Category | Price | Quantity | Available |
|---|---|---|---|---|---|
| P01 | Pen | Stationery | 1.50 | 12 | TRUE |
| P02 | Notebook | Stationery | 3.00 | 4 | TRUE |
| P03 | Cable | Electronics | 6.50 | 0 | FALSE |
| P04 | Mouse | Electronics | 12.00 | 5 | TRUE |
| P05 | Folder | Stationery | 2.00 | 8 | TRUE |
Stock: the same five records continue across both panels. Code identifies the matching row.
| Code | ItemName | Category |
|---|---|---|
| P01 | Pen | Stationery |
| P02 | Notebook | Stationery |
| P03 | Cable | Electronics |
| P04 | Mouse | Electronics |
| P05 | Folder | Stationery |
Same Code, continued fields:
| Code | Price | Quantity | Available |
|---|---|---|---|
| P01 | 1.50 | 12 | TRUE |
| P02 | 3.00 | 4 | TRUE |
| P03 | 6.50 | 0 | FALSE |
| P04 | 12.00 | 5 | TRUE |
| P05 | 2.00 | 8 | TRUE |
AND: a record must pass both tests
SELECT ItemName FROM Stock WHERE Category = "Stationery" AND Quantity <= 5;
| Code | ItemNameSELECT | CategoryWHERE | QuantityWHERE | In result? |
|---|---|---|---|---|
| P01 | Pen | Stationery | 12 | no |
| P02 | Notebook | Stationery | 4 | yes |
| P03 | Cable | Electronics | 0 | no |
| P04 | Mouse | Electronics | 5 | no |
| P05 | Folder | Stationery | 8 | no |
Result
ItemName |
|---|
| Notebook |
SELECT ItemName FROM Stock WHERE Category = "Stationery" AND Quantity <= 5;
| Code | ItemNameSELECT | CategoryWHERE | QuantityWHERE | In result? |
|---|---|---|---|---|
| P01 | Pen | Stationery | 12 | no |
| P02 | Notebook | Stationery | 4 | yes |
| P03 | Cable | Electronics | 0 | no |
| P04 | Mouse | Electronics | 5 | no |
| P05 | Folder | Stationery | 8 | no |
ItemName |
|---|
| Notebook |
Notebook is the only record that passes both tests. Pen and Folder fail the quantity test; Cable and Mouse fail the category test. Passing one test is not enough for AND.
- Notebook is the only record that passes both tests.
- Pen and Folder fail the quantity test; Cable and Mouse fail the category test.
- Passing one test is not enough for AND.
OR: a record must pass at least one test
SELECT ItemName FROM Stock WHERE Category = "Electronics" OR Price < 2.00;
Result
ItemName |
|---|
| Pen |
| Cable |
| Mouse |
Pen passes the price test; Cable and Mouse pass the category test.
SELECT ItemName FROM Stock WHERE Category = "Electronics" OR Price < 2.00;
| Code | ItemNameSELECT | CategoryWHERE | PriceWHERE | In result? |
|---|---|---|---|---|
| P01 | Pen | Stationery | 1.50 | yes |
| P02 | Notebook | Stationery | 3.00 | no |
| P03 | Cable | Electronics | 6.50 | yes |
| P04 | Mouse | Electronics | 12.00 | yes |
| P05 | Folder | Stationery | 2.00 | no |
ItemName |
|---|
| Pen |
| Cable |
| Mouse |
Pen passes the price test; Cable and Mouse pass the category test.
Each result row comes from one record: never put one item's name with another item's value. A record that passes both OR tests still appears only once.
- Each result row comes from one record: never put one item's name with another item's value.
- A record that passes both OR tests still appears only once.
Bikes (a hire shop’s table):
BikeID | Type | Gears | DayRate | InService |
|---|---|---|---|---|
| B01 | City | 3 | 8.00 | TRUE |
| B02 | Mountain | 21 | 15.00 | TRUE |
| B03 | City | 7 | 10.00 | FALSE |
| B04 | Road | 18 | 12.50 | TRUE |
1 Write the result of SELECT BikeID FROM Bikes WHERE Type = "City" AND InService = TRUE;
2 Write the result of SELECT BikeID FROM Bikes WHERE Type = "City" OR Gears > 20;
3 Write a query to show the BikeID of bikes with more than 5 gears that cost less than 13.00 a day.
- 1B01 (B03 is City but not in service)
- 2B01, B02, B03 (B01 and B03 are City; B02 has 21 gears)
- 3
SELECT BikeID FROM Bikes WHERE Gears > 5 AND DayRate < 13.00;gives B03, B04
Without ORDER BY, SQL does not promise any order for the rows. The order shown in a result table here is only one possible order.
Sorting query results with ORDER BY
A price list is easier to read with the dearest item first, and a list of names in A to Z order. WHERE picks the records, SELECT picks the fields and ORDER BY sets the order of the rows. A sorted row moves as a whole: its values stay together.
Every query reads this one table, Stock (5 records):
| Code | ItemName | Category | Price | Quantity | Available |
|---|---|---|---|---|---|
| P01 | Pen | Stationery | 1.50 | 12 | TRUE |
| P02 | Notebook | Stationery | 3.00 | 4 | TRUE |
| P03 | Cable | Electronics | 6.50 | 0 | FALSE |
| P04 | Mouse | Electronics | 12.00 | 5 | TRUE |
| P05 | Folder | Stationery | 2.00 | 8 | TRUE |
Stock: the same five records continue across both panels. Code identifies the matching row.
| Code | ItemName | Category |
|---|---|---|
| P01 | Pen | Stationery |
| P02 | Notebook | Stationery |
| P03 | Cable | Electronics |
| P04 | Mouse | Electronics |
| P05 | Folder | Stationery |
Same Code, continued fields:
| Code | Price | Quantity | Available |
|---|---|---|---|
| P01 | 1.50 | 12 | TRUE |
| P02 | 3.00 | 4 | TRUE |
| P03 | 6.50 | 0 | FALSE |
| P04 | 12.00 | 5 | TRUE |
| P05 | 2.00 | 8 | TRUE |
Filter first, then sort by descending price
SELECT ItemName, Price FROM Stock WHERE Category = "Stationery" AND Price <= 3.00 ORDER BY Price DESC;
Result
ItemName | Price |
|---|---|
| Notebook | 3.00 |
| Folder | 2.00 |
| Pen | 1.50 |
SELECT ItemName, Price FROM Stock WHERE Category = "Stationery" AND Price <= 3.00 ORDER BY Price DESC;
| Code | ItemNameSELECT | CategoryWHERE | PriceSELECT · WHERE · ORDER BY | In result? |
|---|---|---|---|---|
| P01 | Pen | Stationery | 1.50 | 3rd |
| P02 | Notebook | Stationery | 3.00 | 1st |
| P03 | Cable | Electronics | 6.50 | no |
| P04 | Mouse | Electronics | 12.00 | no |
| P05 | Folder | Stationery | 2.00 | 2nd |
ItemName | Price |
|---|---|
| Notebook | 3.00 |
| Folder | 2.00 |
| Pen | 1.50 |
- 1WHERE keeps P01, P02 and P05, with prices 1.50, 3.00 and 2.00.
- 2DESC puts the highest price first: Notebook, Folder, Pen.
- 3The columns stay in SELECT order: ItemName, then Price.
Sort by a field that is not displayed
SELECT Code FROM Stock WHERE Category = "Stationery" ORDER BY Quantity DESC;
Result
| Code |
|---|
| P01 |
| P05 |
| P02 |
Sorted by Quantity (12, 8, 4), even though Quantity is not shown.
SELECT Code FROM Stock WHERE Category = "Stationery" ORDER BY Quantity DESC;
| CodeSELECT | CategoryWHERE | QuantityORDER BY | In result? |
|---|---|---|---|
| P01 | Stationery | 12 | 1st |
| P02 | Stationery | 4 | 3rd |
| P03 | Electronics | 0 | no |
| P04 | Electronics | 5 | no |
| P05 | Stationery | 8 | 2nd |
| Code |
|---|
| P01 |
| P05 |
| P02 |
Sorted by Quantity (12, 8, 4), even though Quantity is not shown.
ORDER BY Field ASC sorts smallest to largest (A → Z for text); DESC sorts largest to smallest (Z → A). ASC is the default if neither is written. “Alphabetical order” means ASC.Bikes (a hire shop’s table):
BikeID | Type | Gears | DayRate | InService |
|---|---|---|---|---|
| B01 | City | 3 | 8.00 | TRUE |
| B02 | Mountain | 21 | 15.00 | TRUE |
| B03 | City | 7 | 10.00 | FALSE |
| B04 | Road | 18 | 12.50 | TRUE |
1 Write the result of SELECT BikeID, DayRate FROM Bikes ORDER BY DayRate DESC;
2 Write a query to show the Type of every bike in service, fewest gears first.
- 1B02 15.00; B04 12.50; B03 10.00; B01 8.00 (highest rate first)
- 2
SELECT Type FROM Bikes WHERE InService = TRUE ORDER BY Gears ASC;gives City, Road, Mountain (3, 18, 21 gears)
COUNT, SUM and defining a table
Sometimes the answer you want is one number, not a list: how many stationery items there are, or how many are in stock altogether. COUNT gives how many records match. SUM adds up one number field across the records that match.
Stock has five records. Its Stationery records are P01 (Quantity 12), P02 (4) and P05 (8); the other records are P03 (0) and P04 (5).
COUNT
Counts matching records. COUNT(*) counts every matching row; COUNT(FieldName) gives the same count here.
SUM
Finds the total of the values in one numeric field for the records that match.
The same condition: COUNT gives how many records
SELECT COUNT(Code) FROM Stock WHERE Category = "Stationery";
Result
COUNT(Code) |
|---|
| 3 |
3 records match: P01, P02, P05.
SELECT COUNT(Code) FROM Stock WHERE Category = "Stationery";
| CodeCOUNT | CategoryWHERE | Counted? |
|---|---|---|
| P01 | Stationery | yes |
| P02 | Stationery | yes |
| P03 | Electronics | no |
| P04 | Electronics | no |
| P05 | Stationery | yes |
COUNT(Code) |
|---|
| 3 |
3 records match: P01, P02, P05.
…and SUM adds up their quantities
SELECT SUM(Quantity) FROM Stock WHERE Category = "Stationery";
Result
SUM(Quantity) |
|---|
| 24 |
12 + 4 + 8 = 24 items.
SELECT SUM(Quantity) FROM Stock WHERE Category = "Stationery";
| Code | CategoryWHERE | QuantitySUM | Added? |
|---|---|---|---|
| P01 | Stationery | 12 | yes |
| P02 | Stationery | 4 | yes |
| P03 | Electronics | 0 | no |
| P04 | Electronics | 5 | no |
| P05 | Stationery | 8 | yes |
SUM(Quantity) |
|---|
| 24 |
12 + 4 + 8 = 24 items.
No WHERE: COUNT includes every record
SELECT COUNT(*) FROM Stock;
Result
COUNT(*) |
|---|
| 5 |
Every record, including Cable with Quantity 0.
- WHERE runs first. COUNT or SUM then gives one result from the records kept: 3 records for COUNT, or 24 items for SUM(Quantity).
- Output that one value, not the three rows.
Defining a table: fields, types, key and validation
Choose meaningful field names → a suitable data type for each → a unique primary key → validation rules for the data entered. In Stock, Code identifies an item, Quantity stores a whole-item count and Price allows a fractional amount.
Every query depends on choices made when the table was designed:
- 1Choose meaningful field names.
- 2Give each a suitable data type.
- 3Choose a unique primary key.
- 4Set validation rules for the data entered.
In Stock, Code identifies an item, Quantity stores a whole-item count and Price allows a fractional amount. Each row below is one check made as data is entered; a field can need more than one.
| Field | Validation check | Rule |
|---|---|---|
| Code | presence check | must not be blank: it is the primary key |
| Code | length and format check | exactly 3 characters: “P” followed by two digits |
| Quantity | type check · range check | an integer · 0 or more |
| Price | range check | greater than 0 |
WHERE runs first. COUNT or SUM then gives one result from the records kept: 3 records for COUNT, or 24 items for SUM(Quantity). Output that one value, not the three rows.
Bikes (a hire shop’s table):
BikeID | Type | Gears | DayRate | InService |
|---|---|---|---|---|
| B01 | City | 3 | 8.00 | TRUE |
| B02 | Mountain | 21 | 15.00 | TRUE |
| B03 | City | 7 | 10.00 | FALSE |
| B04 | Road | 18 | 12.50 | TRUE |
1 Write the result of SELECT COUNT(BikeID) FROM Bikes WHERE InService = TRUE;
2 Write a query to find the total number of gears on the City bikes.
3 Suggest a validation check and rule for DayRate.
- 13 (B01, B02, B04)
- 2
SELECT SUM(Gears) FROM Bikes WHERE Type = "City";gives 10 (3 + 7) - 3range check: DayRate must be greater than 0