Question bank › IB Computer Science › A3 Databases

SQL: DDL/DML, queries across two tables and updating data

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

1Core3 marks

Use the tables below.

Tutor
TutorIDTutorName
1Mr Khan
2Ms Reid
3Mr Cole
Student
StudentIDNameYearTutorID
1Aisha121
2Ben132
3Chloe121
4Dev133
5Elin122
6Femi121
Result
StudentIDSubjectScore
1CS78
1Maths65
2CS55
2Maths70
3CS90
3Maths82
4CS48
5CS66
5Maths71
6CS72

Write an SQL query to list the names of all Year 12 students in reverse alphabetical order. [3]

2Hard5 marks

Use the tables below.

Student
StudentIDNameYearTutorID
1Aisha121
2Ben132
3Chloe121
4Dev133
5Elin122
6Femi121
Result
StudentIDSubjectScore
1CS78
1Maths65
2CS55
2Maths70
3CS90
3Maths82
4CS48
5CS66
5Maths71
6CS72

Construct an SQL query to output the Name and CS Score of every student in tutor group 1 (TutorID = 1) who scored at least 75 in CS. [5]

3Standard5 marks

A hotel stores its bookings in the table below. BookingDate is stored as text in the form YYYY-MM-DD.

Booking
BookingIDRoomIDBookingDateGuests
100142027-03-022
100272027-03-051
100342027-03-092
1004122027-03-114
100572027-03-122

(a) Construct an SQL statement to add booking 1006 for room 12 on 2027-03-14 for 2 guests. [2]

(b) Booking 1003 now has 3 guests. Construct an SQL statement to make this change. [2]

(c) The BookingDate column is indexed. Outline why changing values in an indexed column has a performance cost. [1]

Stuck on SQL: DDL/DML, queries across two tables and updating data?

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.

4Core6 marks

A school library is setting up a database. The following SQL statements are run in order.

S1  CREATE TABLE Book (ISBN CHAR(13) PRIMARY KEY, Title VARCHAR(80), Copies INTEGER);
S2  INSERT INTO Book VALUES ('9780000000001', 'Dune', 3);
S3  ALTER TABLE Book ADD Shelf VARCHAR(5);
S4  UPDATE Book SET Copies = 4 WHERE ISBN = '9780000000001';

(a) For each of the statements S1 to S4, state whether it is an example of a data definition language (DDL) or a data manipulation language (DML) statement. [4]

(b) A technician needs to clear out the Book table. Outline the difference between running DELETE FROM Book; and running DROP TABLE Book; [2]

5Core4 marks

A streaming service stores details of films in the table below.

Film
FilmIDTitleGenreYearRating
1The Long RoadDrama20147.2
2Road to MarsSci-Fi20196.4
3Silent RiverDrama20218.1
4Mars RisingSci-Fi20167.8
5Crossing PathsComedy20185.9
6River RoadDrama20156.8

(a) State the output of the following query. [2]

SELECT Title
FROM Film
WHERE Title LIKE '%Road%' AND Year BETWEEN 2015 AND 2020
ORDER BY Year ASC;

(b) State the output of the following query. [2]

SELECT Title, Rating
FROM Film
WHERE NOT Genre = 'Drama' OR Rating > 8
ORDER BY Rating DESC;
6Standard5 marks

An online plant shop uses two tables.

Customer
CustIDNameTown
1PriyaLeeds
2TomYork
3GraceLeeds
4OmarHull
5LenaYork
Orders
OrderIDCustIDProductQty
5011Fern2
5022Cactus1
5033Fern1
5041Orchid1
5055Fern3
5064Bonsai1
5073Fern2

CustID in Orders is a foreign key referencing Customer.

(a) Construct an SQL query that lists each town that has had at least one order for a Fern. Each town must appear only once, in alphabetical order. [4]

(b) Explain how the output would change if the query did not remove duplicate towns (for example, if the keyword DISTINCT were left out). [1]

7Standard7 marks

A city bike-hire scheme stores its bikes in the table below. HourlyRate is in pounds.

Bike
BikeIDTypeHourlyRateStatus
B01Road4.00Available
B02E-bike8.00Repair
B03E-bike8.00Available
B04Mountain5.50Retired
B05Road4.00Retired
B06E-bike9.00Available

(a) The hourly rate of every E-bike that is not under repair is to increase by £1.50. Construct an SQL statement to make this change. [3]

(b) Construct an SQL statement to remove all retired bikes from the table. [2]

(c) The manager suggests adding an index on HourlyRate because customers often search by price. Explain one disadvantage of indexing this column, with reference to part (a). [2]

8Hard6 marks

A music festival stores its line-up in two tables.

Band
BandIDBandNameGenre
1Iron KitesRock
2Velvet TidePop
3Static BloomRock
4Northern OwlsFolk
Slot
SlotIDBandIDStageStartTime
111Main18:30
122Main16:00
133Main14:15
144Field15:00
153Field20:00

A programmer wants the name and start time of every Rock band playing on the Main stage, earliest start time first. They write:

SELECT BandName, StartTime
FROM Band, Slot
WHERE Genre = Rock
AND Stage = 'Main'
ORDER BY StartTime DESC;

The query contains three errors. Identify each error and state the correction needed. [6]

9Hard6 marks

A veterinary clinic's database has the structure shown in the diagram. Each owner can have many pets; each pet has exactly one owner. The Owner table already exists.

(a) Construct the SQL statement that creates the Pet table. PetID is a whole number, PetName (which must always be entered) is text of up to 20 characters, Species is text of up to 15 characters, and OwnerID links each pet to its owner. [4]

The Owner table currently holds owners with OwnerID 201 and 202 only. A receptionist runs:

INSERT INTO Pet VALUES (2, 'Pepper', 'Cat', 305);

(b) Explain why the database management system rejects this statement. [2]

Owner OwnerID (PK) Surname Phone Pet PetID (PK) PetName Species OwnerID (FK) owns
10StretchHL only8 marks

A tennis club stores members and court bookings. BookDate is stored as text in the form YYYY-MM-DD.

Member
MemberIDNameLevel
1HanaJunior
2KofiSenior
3MayaSenior
4LuisJunior
Booking
BookingIDMemberIDCourtBookDate
1212027-02-27
2122027-03-01
3212027-03-03
4332027-03-04
5222027-03-10
6112027-03-12
7312027-03-15
8232027-03-18
9122027-03-31
10412027-03-20
11322027-04-01
12212027-03-28

(a) Construct an SQL query that shows the name of each member and the number of bookings they made in March 2027, for members with at least three bookings in that month only. The member with the most bookings must appear first. [6]

(b) State the output of your query for the data shown. [2]

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