P2 - Unit 9 - Databases
P2 - Unit 9 - Databases
Customers
* Please note that for any validation code, following must be done
1. Use a while loop to ensure that input is taken in loop until correct input is
given by the user
2. Input must be taken prior to the while loop
3. While loop must have the condition which is not acceptable
4. Inside the while loop output a message that what is expected and take
input again
How to apply Presence check
• In a presence check we are just checking that
something has been input
output ‘ Enter Username’
Input Username
SELECT SUM(Cost)
FROM Receipts
Receipts
SUM(Cost)
8.00 ReceiptID Product Quantity Cost
1 Apples 6 £2.20
Select COUNT ( Quantity) 2 Cheese 1 £2.50
FROM Receipts Where
3 Bread 2 £1.50
Quantity = 6
4 Pears 6 £1.80
Count (Quantity)
2
Practice
Animals
• Look at the table called Animal Height_m Weight_kg
Animals on the right Rhino 1.8 2000
Giraffe 5.5 1800
• create SQL
Emu 1.8 55
statements for the following:
Llama 1.7 200
• Find all animal names in Sea lion 2.4 360
alphabetical order
• Find all animal names and weights that are over 1000 kg
• Find all animals, including all fields that are over 2 m
Practice Answer
Animals
• All animal names in alphabetical order
Animal Height_m Weight_kg
SELECT Animal FROM Animals
ORDER BY Animal ASC Rhino 1.8 2000
Giraffe 5.5 1800
• All animal names and weights
Emu 1.8 55
that are over 1000 kg
SELECT Animal, Weight_kg FROM Llama 1.7 200
Animals WHERE Weight_kg > 1000 Sea lion 2.4 360