ExamBank Scot · Higher Computing Science question bank · All Higher Computing Science questions
11 marks · SQL queries without aggregates
The garage wants to use a query to invoice a customer for the work done on their vehicle. Design a query to display the full name, vehicle registration number and the name of the part for customer ‘L Fletcher’. Field(s) and calculation(s) Table(s) Search criteria Grouping Sort order Write the SQL statement to display the total value of all parts in stock as shown below. Total value 4411.81 The garage would like to display details of all appointments that cost more than the average appointment cost. Write the SQL statement to display the average appointment cost, as shown below. Average cost 168.39 The query from part (i) is saved as ‘avgCost’. Using this query, complete the SQL statement below to display details of all appointments that cost more than the average cost. The details should be displayed in the order shown below. regNo appID make model year cost LR16 JRD AP015 Vauxhall Mokka 2016 246.83 SC15 YHA AP025 Renault Clio 2015 233.74 KK20 DRT AP026 Vauxhall Mokka 2020 210.00 HR19 JWW AP020 Ford Focus 2019 185.23 HK69 TYR AP011 Ford Transit 2019 169.50 SELECT regNo, appID, make, model, year, cost At present only one part can be used in a single repair so the database design would require an additional entity called PartsListing to be added. Complete the diagram below showing the cardinality between the PartsListing entity and the two existing entities. PartsListing A garage offers vehicle services, maintenance, and MOTs. The management uses a relational database to store details of customers, vehicles, appointments and parts. Sample data from the database tables is shown below. Customer custID name telNo 0013 K Amin 05767490813 0017 J Atigah 05534384344 0024 F Hunter 05848534323 0027 H Moore 05583223411 0039 L Fletcher 05748339222 … … … Part partID partName quantity price P001 Air filter 34 11.99 P002 Brake disc 21 61.99 P003 Brake pad 8 68.99 P004 Oil filter 43 20.99 P005 Shock absorbers 13 95.99 … … … … Vehicle regNo custID make model year HK69 TYR 0013 Ford Transit 2019 LR16 JRD 0017 Vauxhall Mokka 2016 SO70 KRF 0013 Kia Creed 2020 GL65 ORJ 0024 Vauxhall Adam 2015 SC15 YHA 0017 Renault Clio 2015 HR19 JWW 0024 Ford Focus 2019 KK20 DRT 0039 Vauxhall Mokka 2020 … … … … … Appointment appID regNo description partID date cost AP011 HK69 TYR Service P001 24/03/2025 169.50 AP015 LR16 JRD Maintenance P002 29/03/2025 246.83 AP021 SO70 KRF Service P004 30/03/2025 78.50 AP020 HR19 JWW Service P001 30/03/2025 185.23 AP023 GL65 ORJ MOT 01/04/2025 54.95 AP025 SC15 YHA MOT P005 02/04/2025 233.74 AP026 KK20 DRT Maintenance P005 02/04/2025 210.00 … … … … … …
• correct fields and name = ‘L Fletcher’ • all four tables Field(s) and Calculation(s) name,regNo, partName Table(s) Customer, Vehicle, Appointment, Part Search Criteria name = ‘L Fletcher’ (custID = 0039) Grouping Sort Order • SELECT with ALIAS and Part table • SUM (quantity * price) SELECT SUM (quantity * price) AS [Total value] FROM Part Disregard WHERE quantity >0 • SELECT AVG(cost) and Alias • FROM Appointment SELECT AVG(cost) as [Average Cost] FROM Appointment • FROM includes Customer, Appointment with Customer.custID = Appointment.custID in WHERE • FROM clause includes ‘avgCost’ • WHERE cost > [Average Cost] • ORDER BY cost DESC; SELECT name, appointmentID, date, description, cost FROM Customer, Appointment, avgCost WHERE Customer.custID = Appointment.custID AND cost > [Average Cost] ORDER BY cost DESC; Award second bullet if candidate has used subquery. SELECT name, appointmentID, date, description, cost FROM Customer, Appointment, WHERE Customer.custID = Appointment.custID AND cost > (SELECT AVG(cost) as [Average Cost] FROM Appointment) ORDER BY cost DESC; Suitable cardinality between Parts:PartListing and Appointment:PartsListing Part/Appointment entities may be shown opposite from below.
Previous question: Higher Computing Science 2025 Question 12