Question bank › IB Computer Science › A3 Databases

SQL aggregate functions, views and transactions (HL)

Answer on paper first, then reveal the mark scheme one mark at a time.

1HardHL only4 marks

The table below stores exam results.

Result
StudentIDSubjectScore
1CS78
1Maths65
2CS55
2Maths70
3CS90
3Maths82
4CS48
5CS66
5Maths71
6CS72

Write an SQL query that outputs the StudentID and average Score of every student who has at least two results and an average score above 70. [4]

2CoreHL only3 marks

A school library records book loans in the table below. DaysLate is the number of days a book was returned after its due date.

Loan
LoanIDBookIDMemberIDDaysLate
101B4M20
102B7M53
103B4M10
104B2M26
105B9M31
106B7M20
107B2M49

State the output of each of the following SQL queries.

(a) SELECT COUNT(*) FROM Loan WHERE DaysLate > 0; [1]

(b) SELECT MAX(DaysLate) FROM Loan; [1]

(c) SELECT SUM(DaysLate) FROM Loan WHERE MemberID = 'M2'; [1]

3CoreHL only4 marks

A climbing centre stores details of its members in a table Member with fields MemberID, JoinDate, Age and FeesPaid.

For each requirement below, identify the SQL aggregate function that should be used.

(a) The number of members in the table. [1]

(b) The date on which the longest-standing member joined. [1]

(c) The total amount of fees paid by all members. [1]

(d) The mean age of the members. [1]

Stuck on SQL aggregate functions, views and transactions (HL)?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4CoreHL only4 marks

An online shop uses transactions to keep its database accurate.

(a) State the four properties represented by the acronym ACID. [2]

(b) Outline the purpose of the transaction control command COMMIT. [1]

(c) Outline the purpose of the transaction control command ROLLBACK. [1]

5HardHL only5 marks

A homeware shop uses two tables:

Product
ProductIDNameCategoryPrice
P1KettleKitchen24.00
P2ToasterKitchen31.50
P3Desk lampLighting18.00
P4Floor lampLighting55.00
P5RugDecor42.00
OrderLine
OrderIDProductIDQuantity
1P12
1P31
2P41
3P22
3P51
4P34
5P11

Construct an SQL query that outputs each Category and its total sales revenue (quantity × price), for categories whose total revenue is more than 100. The output must list the category with the highest revenue first. [5]

6StandardHL only5 marks

A bank table Account holds three rows: account A with balance 500, account B with balance 200 and account C with balance 50. The following three transactions run one after another.

-- Transaction 1
BEGIN TRANSACTION;
UPDATE Account SET Balance = Balance - 150 WHERE AccNo = 'A';
UPDATE Account SET Balance = Balance + 150 WHERE AccNo = 'B';
COMMIT;

-- Transaction 2
BEGIN TRANSACTION;
UPDATE Account SET Balance = Balance - 400 WHERE AccNo = 'B';
-- the application now reads B's balance:
-- if it is negative it issues ROLLBACK, otherwise COMMIT

-- Transaction 3
BEGIN TRANSACTION;
UPDATE Account SET Balance = Balance + 100 WHERE AccNo = 'C';
UPDATE Account SET Balance = Balance - 100 WHERE AccNo = 'A';
COMMIT;

(a) State the final balance of each of accounts A, B and C. [3]

(b) Explain how the ROLLBACK in Transaction 2 maintains the integrity of the data. [2]

7HardHL only6 marks

A chain of 60 school canteens stores every sale in a table that now holds about 40 million rows. Head office wants a dashboard showing total sales per canteen per week. The sales data is loaded into the database each night.

(a) Distinguish between a virtual view and a materialized (snapshot) view. [4]

(b) Suggest, with a reason, which type of view head office should use for the dashboard. [2]

8StandardHL only5 marks

A school's database holds a table Staff with the fields StaffID, Name, Department, Email, Salary and Active. Active is 'Y' for staff who currently work at the school and 'N' otherwise.

Reception staff need to look up the name, department and email address of current staff only. They must not be able to see salaries.

(a) Construct an SQL statement that creates a view called ReceptionDirectory to meet this requirement. [3]

(b) Outline two benefits of giving reception staff access to the view rather than to the Staff table. [2]

9HardHL only5 marks

A cinema asks customers to rate a film from 1 to 5. Customers may skip the question, in which case Rating is stored as NULL.

Survey
ResponseIDRating
15
24
3NULL
45
53
6NULL
75
85

(a) State the value returned by each of the following. [3]

  • SELECT COUNT(*) FROM Survey;
  • SELECT COUNT(Rating) FROM Survey;
  • SELECT AVG(Rating) FROM Survey;

(b) A manager calculates the mean rating as SUM(Rating) / COUNT(*) and gets a different answer from AVG(Rating). Explain why the two values differ. [2]

10StretchHL only6 marks

A concert venue's database stores the number of unsold tickets for a show in a field Remaining, which is 12. Two booking agents, X and Y, sell tickets at the same time without using transactions. The diagram shows the order in which their operations are carried out.

(a) State the value of Remaining after T6, and the value it should have. [2]

(b) Identify the ACID property that has not been maintained. [1]

(c) Explain how running each sale as a transaction with BEGIN TRANSACTION and COMMIT would prevent this problem. [3]

TimeAgent X (sells 3)Agent Y (sells 5)Stored valueT1reads Remaining (12)12T2reads Remaining (12)12T3calculates 12 − 3 = 912T4calculates 12 − 5 = 712T5writes 99T6writes 7?

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session