ExamBank Scot · Higher Computing Science question bank · All Higher Computing Science questions

Higher Computing Science 2022 Question 11

9 marks · SQL queries without aggregates

Complete the design of a query to produce this output. Field(s) and calculation(s) forename, surname, referralDate, specialist Tables(s) Search criteria Grouping Sort order referralDate DESC Perfect Eyes wants to know which customers have more than the average loyalty points. Write the SQL statement to display the average loyalty points of the customers, as shown below. Average Points 38.4 The query from part (i) is saved as ‘AvgPointsQuery’. Using this query, complete the SQL statement to display the customers who have more than the average loyalty points, in order from highest to lowest as shown below. forename surname loyaltyPoints opticianName Julia Smith 77 Mr K Madhok Margaret Rennie 73 Mr M Ali Robin Li 51 Miss C Srigor Steven Rycroft 50 Miss C Srigor … … … … SELECT forename, surname, loyaltyPoints, opticianName Perfect Eyes is an optician that has branches throughout Scotland. It uses a relational database consisting of three linked tables to store data about customers, opticians and specialist referrals. Extracts from the three tables are shown below. Customer customerID opticianID forename surname loyaltyPoints address town AW3212 KM101 Amy Wilson 24 8 Pelken Road Paisley JP2323 CS878 Joyce Peden 47 42 Bewston Road Ayr JS9767 KM101 Julia Smith 77 32 Bracken Road Paisley KC1123 MS221 Katy Carenduff 11 12 Main Street Melrose LL3234 CS878 Robin Li 51 21 Manse Court Largs MR8766 JS232 Margaret Rennie 73 63 Royal Crescent Dalry SR7123 CS878 Steven Rycroft 50 22 Markston Place Ayr … … … … … … … Optician opticianID opticianName opticianAddress opticianTown KM101 Mr K Madhok South Road Paisley KM321 Mr M Ali Main Road Troon MS221 Mrs M Saunders St Dunstan’s Park Melrose … … … … Referral referralID customerID referralDate specialist P12121 AW3212 12/02/2022 Gerard McGowan Eye Clinic H92743 HS3433 14/02/2022 JK Optometrist CXR222 JP2323 28/04/2022 Eye Clinic at Newmains U32349 JS9767 26/04/2022 Gerard McGowan Eye Clinic … … … … A query is required to list customers who were referred in April 2022 to any specialist that includes ‘Eye Clinic’ in its name. The list should be displayed with the most recent referral date first, as shown below. forename surname referralDate specialist Joyce Peden 28/04/2022 Eye Clinic at Newmains Julia Smith 26/04/2022 Gerard McGowan Eye Clinic Margaret Rennie 01/04/2022 University Hospital Eye Clinic

Marking scheme

• Referral, Customer • specialist Like “*Eye Clinic*” • (AND) referralDate Like "*/04/2022" Tables(s) Referral, Customer Search criteria specialist Like "%Eye Clinic%" AND referralDate Like "%/04/2022" OR specialist Like "*Eye Clinic*" AND referralDate Like "*/04/2022" 1 mark for each bullet In design accept other expressions for like eg = Access allows: referralDate Like "??/04/2022" SQL allows: referralDate Like "_ _/04/2022" Ignore any content in Grouping. • aggregate function AVG on loyaltyPoints • alias and table 1 mark for each bullet SELECT avg(loyaltyPoints) as ‘Average Points’ FROM Customer • FROM Customer, Optician, avgPointsQuery • equi-join: WHERE Customer.opticianID = Optician.optiicanID • loyaltyPoints > ‘Average Points’ • loyaltyPoints descending 1 mark for each bullet SELECT forename, surname, loyaltyPoints, opticianName FROM Customer, Optician, avgPointsQuery WHERE Customer.opticianID = Optician.optiicanID AND loyaltyPoints > ‘Average Points’ ORDER BY loyaltyPoints DESC

Previous question: Higher Computing Science 2022 Question 10

Next question: Higher Computing Science 2022 Question 12