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

Higher Computing Science 2024 Question 14

11 marks · SQL queries without aggregates

The driving school would like a list of the names of all the instructors who have an hourly rate of more than £35 and the names of all their pupils. Complete the SQL statement below to produce this list. SELECT Instructor.name AS [Instructor], Pupil.name AS [Pupil] Sample data from the pupil table is shown below. Pupil pupilRef name address town PU1 P Clifford 21 Clark Street Kilmarnock PU2 N Price 76 Burnside Ave Greenock PU3 M Flood 41 Sinclair Street Greenock PU4 A Singh 92 Rugby Road Kilmarnock PU5 J Wilson 8 Stadium Way Falkirk PU6 M Ali 56 Lime Road Falkirk PU7 S McGuire 18 Craigneuk Ave Airdrie PU8 D McGregor 120 Wallace Place Greenock … … … … The driving school use the following SQL statement to display the number of pupils in each town. SELECT town, COUNT(*) AS [Number Per Town] FROM Pupil GROUP BY town ORDER BY COUNT(*) DESC, town ASC Using the sample data provided, write the expected output from the SQL statement above. town Number Per Town The SQL statement above makes use of the GROUP BY command. Explain why the GROUP BY command is required in the SQL statement above to produce the expected output. The data from the instructor table is shown below. Instructor instructorID name dayOff hourlyRate 001 C Robertson Saturday 35 002 L MacLean Sunday 40 003 T Jack Wednesday 35 004 B Avidal Saturday 36 005 F Shabnam Tuesday 36 The output below shows the average hourly rate of instructors who have their day off at the weekend. Average Hourly Rate 37 Write the SQL statement that would produce the output above. The driving school would like to know the pupilRef of all the pupils who have lessons with the instructor who offers lessons at the cheapest hourly rate. SELECT pupilRef FROM Booking,Instructor WHERE Booking.instructorID = Instructor.instructorID AND hourlyRate = MIN(hourlyRate); When tested the SQL statement did not execute because an aggregate function cannot be included in a WHERE clause in this way. Describe one solution to this problem. A driving school uses a relational database to store details of driving instructors, pupils and bookings in three linked tables. The relational database uses the following three tables. Instructor Pupil Booking instructorID name dayOff hourlyRate pupilRef name address town bookingNo instructorID* pupilRef* date time duration

Marking scheme

• Tables(Instructor, Pupil, Booking) • Condition hourlyRate > 35 • Equi Joins Instructor.instructorID = Booking.instructorID AND Booking.pupilRef = Pupil.pupilRef Example answer: FROM Instructor, Pupil, Booking WHERE hourlyRate > 35 AND Instructor.instructorID = Booking.instructorID AND Booking.pupilRef = Pupil.pupilRef Award bullet 3 if a NATURAL join is used without field names • Totals • Doubly sorted output Example answer: town Number Per Town Greenock Falkirk Kilmarnock Airdrie 3 2 2 1 • Produces a single output for each town • Without a GROUP BY it would produce a single output for only one town • In the SELECT clause the town field is a non-aggregate field Award 1 mark for any one bullet • AVG function on hourlyRate • Alias and table • Criteria for dayOff Example answer: SELECT AVG(hourlyRate) as [Average Hourly Rate] FROM instructor WHERE dayOff = “Saturday” OR dayOff=”Sunday” WHERE dayOFF LIKE “S%” Access: dayOff LIKE “S*” • First query to identify minimum/cheapest hourly rate • First query to be included in a second query (using query name in the FROM and alias in WHERE clause) OR • Use a subquery to identify minimum/cheapest hourly rate (1 mark • Within the WHERE clause Candidate can create a view which saves the results of a query similar to a table in the FROM clause

Previous question: Higher Computing Science 2024 Question 13

Next question: Higher Computing Science 2024 Question 15