Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Subject:
Topics:
Avg
score:
–
Topics
done:
0/32

SQL queries

Practise 39 exam-style questions on SQL queries for Cambridge IGCSE Computer Science (Paper 2). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with an AI tutor on hand if you get stuck.

  • 39 questions
  • 1–6 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

1 mark

The table shows the database table Bookings for a hotel.

Give the output from this SQL query:

SELECT SUM(Cost)
FROM Bookings
WHERE RoomType = 'Double';
Table: Bookings. Table with columns "BookingID", "Surname", "RoomType", "Nights", "Breakfast", "Cost". BookingID: B101; Surname: Khan; RoomType: Double; Nights: 3; Breakfast: TRUE; Cost: 285.00 | BookingID: B102; Surname: Lopez; RoomType: Single; Nights: 1; Breakfast: FALSE; Cost: 65.00 | BookingID: B103; Surname: Murray; RoomType: Family; Nights: 5; Breakfast: TRUE; Cost: 600.00 | BookingID: B104; Surname: Novak; RoomType: Double; Nights: 2; Breakfast: FALSE; Cost: 170.00 | BookingID: B105; Surname: Owusu; RoomType: Single; Nights: 4; Breakfast: FALSE; Cost: 260.00 | BookingID: B106; Surname: Patel; RoomType: Family; Nights: 2; Breakfast: FALSE; Cost: 220.00 | BookingID: B107; Surname: Quinn; RoomType: Double; Nights: 6; Breakfast: TRUE; Cost: 570.00.

Model answer

1025.00

Explanation

Three bookings are for a Double room: 285.00 + 170.00 + 570.00 = 1025.00. SUM returns one total, not a list of values.

Mark scheme
  1. 1 mark

    1025 (1025.00)

Question 2

2 marks

The table shows the database table Weather.

Give the output from each of these two SQL queries.

Query 1:

SELECT SUM(Rainfall)
FROM Weather
WHERE Town = 'Cardiff';

Query 2:

SELECT COUNT(RecordID)
FROM Weather
WHERE Rainfall = 0.0;
Table: Weather. Table with columns "RecordID", "Town", "Day", "MaxTemp", "Rainfall", "Sunny". RecordID: W1; Town: Bristol; Day: Mon; MaxTemp: 18.5; Rainfall: 2.4; Sunny: FALSE | RecordID: W2; Town: Bristol; Day: Tue; MaxTemp: 21.0; Rainfall: 0.0; Sunny: TRUE | RecordID: W3; Town: Cardiff; Day: Mon; MaxTemp: 17.0; Rainfall: 3.1; Sunny: FALSE | RecordID: W4; Town: Cardiff; Day: Tue; MaxTemp: 20.5; Rainfall: 0.6; Sunny: TRUE | RecordID: W5; Town: Bristol; Day: Wed; MaxTemp: 23.5; Rainfall: 0.0; Sunny: TRUE | RecordID: W6; Town: Cardiff; Day: Wed; MaxTemp: 22.0; Rainfall: 1.2; Sunny: FALSE | RecordID: W7; Town: Bath; Day: Mon; MaxTemp: 19.5; Rainfall: 0.0; Sunny: TRUE | RecordID: W8; Town: Bath; Day: Tue; MaxTemp: 24.0; Rainfall: 0.3; Sunny: TRUE.

Model answer

Query 1: 4.9
Query 2: 3

Explanation

Query 1 adds Cardiff's rainfall values: 3.1 + 0.6 + 1.2 = 4.9. Query 2 counts the records with no rain, W2, W5 and W7, giving 3.

Mark scheme
  1. 1 mark

    Query 1: 4.9

  2. 1 mark

    Query 2: 3

Question 3

3 marks

The table shows the database table Orders for a café.

Write an SQL query to output the total quantity of Latte that has been ordered.

Table: Orders. Table with columns "OrderNo", "Item", "Category", "Quantity", "Price". OrderNo: 1; Item: Latte; Category: Drink; Quantity: 2; Price: 3.20 | OrderNo: 2; Item: Croissant; Category: Food; Quantity: 1; Price: 2.10 | OrderNo: 3; Item: Tea; Category: Drink; Quantity: 3; Price: 1.80 | OrderNo: 4; Item: Muffin; Category: Food; Quantity: 2; Price: 2.60 | OrderNo: 5; Item: Latte; Category: Drink; Quantity: 1; Price: 3.20 | OrderNo: 6; Item: Panini; Category: Food; Quantity: 1; Price: 5.50 | OrderNo: 7; Item: Smoothie; Category: Drink; Quantity: 2; Price: 3.90 | OrderNo: 8; Item: Tea; Category: Drink; Quantity: 1; Price: 1.80.

Model answer

SELECT SUM(Quantity)
FROM Orders
WHERE Item = 'Latte';

Explanation

The total quantity is found by adding the Quantity values, which needs SUM. COUNT would only give the number of Latte orders (2), not the number of lattes ordered (3).

Mark scheme
  1. 1 mark

    SELECT SUM(Quantity)

    Do not accept COUNT.

  2. 1 mark

    FROM Orders

  3. 1 mark

    WHERE Item = 'Latte'

Question 4

4 marks

The table shows the database table Students, which stores each student's test marks.

A teacher wants a query that gives exactly this output:
Cerys 91
Femi 82
Hugo 74
Dev 70

Write an SQL query that would produce this output.

Table: Students. Table with columns "StudentID", "Name", "Form", "Maths", "English". StudentID: S01; Name: Asha; Form: 10A; Maths: 72; English: 65 | StudentID: S02; Name: Bilal; Form: 10B; Maths: 45; English: 58 | StudentID: S03; Name: Cerys; Form: 10A; Maths: 88; English: 91 | StudentID: S04; Name: Dev; Form: 10B; Maths: 39; English: 70 | StudentID: S05; Name: Erin; Form: 10A; Maths: 61; English: 49 | StudentID: S06; Name: Femi; Form: 10B; Maths: 77; English: 82 | StudentID: S07; Name: Grace; Form: 10B; Maths: 52; English: 47 | StudentID: S08; Name: Hugo; Form: 10A; Maths: 68; English: 74.

Model answer

SELECT Name, English
FROM Students
WHERE English >= 70
ORDER BY English DESC;

Explanation

The numbers in the output match the English marks, so the fields are Name and English. The four students are the only ones with 70 or more in English, and the marks go from highest to lowest, so the sort is descending on English.

Mark scheme
  1. 1 mark

    SELECT Name, English

    Both fields in this order, no others.

  2. 1 mark

    FROM Students

  3. 1 mark

    A WHERE condition that selects exactly these four students, e.g. WHERE English >= 70

    Accept any condition giving exactly these records, e.g. English > 65 or English >= 66. Do not accept English > 70 (excludes Dev).

  4. 1 mark

    ORDER BY English DESC

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

AI Tutor Hints first, answers after you try

Hi! Stuck? Ask me for a hint and I’ll help you work it out. Once you’ve checked your answer I can explain exactly where the marks are.