Professional Documents
Culture Documents
9 Databases Reduced
9 Databases Reduced
9 Databases Reduced
BETCHOO
O-LEVEL
9 DATABASES
9.1 Databases
9.1.1 Single-table databases
A database is a structured collection of data that allows people to extract information in a way that
meets their needs. Data can include text, numbers and pictures.
For example, a simple database of items for sale could include a description, the price and a picture
of the item.
A single-table database contains only one table. A table consists of many records. The number of
records in a table will vary, as new records can be added and deleted from a table as required. Each
record consists of several fields. The number of fields in table is fixed so each record contains the
same number of fields. An easy way to remember this is:
These are the database data types you need to be able to use.
1
A. BETCHOO
O-LEVEL
9 DATABASES
Each primary key contains a unique value; it must contain data values that are never repeated in the
table.
Here are some examples of the SQL statements you need to be able to use.
Operator Description
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
= Equal to
<> Not equal to
Operator
AND
OR
2
A. BETCHOO
O-LEVEL
9 DATABASES
FROM Movie
Name Rating
Shaun of the Dead 8.7
Big 8.5
• The two fields - Name and Rating have been extracted from the Movie table and then the
records have been filtered by Rating.
• This example uses the > operator to search for records where the rating is greater than 8.4.
• There are several other comparison operators which can be used to create the filter criteria in
the WHERE line of a SQL query.
3
A. BETCHOO
O-LEVEL
9 DATABASES
FROM Movie
Name Rating
Moana 8.1
• The two fields Name and Rating have been extracted from the Movie table and the records
have been filtered by both Genre and Certificate
• This query uses the AND logical operator to include multiple criteria in the WHERE line of the
SQL query
• Another logical operator which can be used in the WHERE statement is OR
o For example, WHERE Genre=”Comedy” OR Genre=”Family”
ORDER BY
You can enter a fourth line to the statement using the ORDER BY command, followed by ASC or DESC
• If you enter ASC the results of the query will be sorted in ascending order
• If you enter DESC the results of the query are sorted in descending order
Example
SELECT Name,Genre, Certificate, Rating
FROM Movie
4
A. BETCHOO
O-LEVEL
9 DATABASES
The query has returned four fields and all records because there were no WHERE criteria. The
records are sorted by Name alphabetically.
If numbers are sorted in ascending order they go from the lowest number at the top of the table to
the highest number at the bottom
Example
SELECT SUM(QuantityInStock)
FROM ProductTable;
This query will add up all of the numbers in the QuantityInStock field.
Example
SELECT COUNT(*)
FROM ProductTable
This query will count all the records with a price greater than 2.
This is because there are three products with a price greater than £2 (Chips, Beans, Bananas)
5
A. BETCHOO
O-LEVEL
9 DATABASES
Worked Example 2:
A database table, Dogs2023, is used to keep a record of all dogs registered at a veterinary practice.
Write the structured query language (SQL) to return the name and breed of all Female dogs.
FROM Dogs2023
Write an SQL query to find out the name and breed of all dogs aged 10 years old or older.
FROM Dogs2023
Write an SQL query to return the name and breed of all male dogs in ascending order by their
name.
FROM Dogs2023
Write an SQL query to count the number of female dogs aged less than 5 years old.
SELECT COUNT(*)
FROM Dogs2023