Databases (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
Question 3
A database table called Employees stores information about employees. The table has the following columns: EmployeeID (integer), EmployeeName (text), Department (text), and Salary (decimal).
SELECT Department, AVG(Salary)
FROM Employees
GROUP BY Department;
What does this SQL script do?
This SQL script calculates the average salary for each department in the Employees table.
- SELECT Department, AVG(Salary): This specifies that we want to retrieve the Department and the average Salary.
- FROM Employees: This indicates that we are querying the Employees table.
- GROUP BY Department: This groups the rows in the table based on the values in the Department column. The AVG(Salary) function is then applied to each group, calculating the average salary for each department.
2.
A local charity wants to track donations received. Each donation has a DonationID, the name of the donor, the amount donated, and the date of the donation. The charity also wants to track the types of donations (e.g., cash, cheque, goods). Define a single-table database schema to store this information. Clearly state the fields and their data types.
A single-table database schema for this scenario would be a table named Donations. This table would contain the following fields:
- DonationID (Integer, Primary Key): A unique identifier for each donation.
- DonorName (Text): The name of the donor.
- AmountDonated (Decimal): The amount of money donated.
- DonationDate (Date): The date the donation was received.
- DonationType (Text): The type of donation (e.g., Cash, Cheque, Goods).
Each row in this table represents a single donation. The fields capture all the necessary information about the donation in a single table. This is suitable for a small charity with a relatively simple data structure.
Table Structure:
| DonationID | DonorName | AmountDonated | DonationDate | DonationType |
3.
A program is designed to track the number of items in a shopping cart. The number of items can be any whole number, from 0 to a large number. Suggest a suitable data type for storing the number of items in the shopping cart and explain your choice. Also, briefly discuss any potential limitations of this data type in this context.
The most suitable data type for storing the number of items in a shopping cart is integer. This is because the number of items is a whole number. Integers are efficient for storing whole numbers and can accommodate a large range of values, suitable for a shopping cart.
A potential limitation of using an integer is that it can only store whole numbers. If the shopping cart needs to store fractional quantities of items (which is unlikely in a typical shopping scenario), an integer would not be suitable. However, for the given context of 'number of items', an integer is perfectly appropriate and efficient.