ExamBank Scot · Higher Computing Science question bank · All Higher Computing Science questions
12 marks · Aggregate functions and grouping
Design a query to display the most expensive multi‑bag and family bag as shown below. type Most Expensive Item Multi‑bag 1.50 Family bag 1.89 Field(s) and calculation(s) Tables(s) and query Snack Search criteria Grouping Sort order The manager wants to half the price of all products with stock levels of more than 500. Write the SQL statement that would make these changes. A query is written to display the total amount of stock for each type of snack. The expected ordered output is shown below. type Total Stock Multi‑bag 2528 Single 2279 Family bag 2190 When the SQL statement below is tested the actual output does not match the expected output. SELECT type, COUNT(stock) FROM Snack GROUP BY type; Re‑write the SQL statement to produce the expected output. Write the SQL statement to remove all records of snacks that include ‘Salt’ in any part of their product name. A shop uses a relational database to keep track of stock. One of the database tables is shown below. Snack productID productName type weight price stock 001 Baked Cheese Snacks Single 37.5 0.65 565 002 Sweet Chilli Flakes Single 28 0.65 300 003 Chicken Crisps Single 32.5 0.55 240 004 Ready Salted Crisps Single 30 0.58 654 005 Cheese Straws Single 12 0.70 120 006 Tomato Puffs Single 20 0.70 400 007 Prawn Cocktail Crisps Multi‑bag 78.6 1.00 335 008 Wheat Crunch Crisps Multi‑bag 138 1.00 356 009 Assorted Flat Crisps Multi‑bag 153 1.50 545 010 Variety Pack Multi‑bag 150 0.56 678 011 Salted Pack Multi‑bag 150 1.30 614 012 Salted Pretzels Family bag 80 0.45 450 013 Tortilla Chips Family bag 200 0.46 456 014 Aberdeen Angus Crisps Family bag 225 1.89 684 015 Salt and Pepper Shakes Family bag 150 0.99 600
Field(s) and Calculation(s) • Use of both type and alias [Most Expensive Item] • Maximum aggregate function on the price field Search Criteria • Use of complex condition / wildcard Grouping • type 1 mark for each bullet Search criteria can also use a wildcard, for example type LIKE “*bag” Allow use of % for wildcard. Allow use of ‘maximum’ in the design of a query. Example answer: Field(s) and calculation(s) type, MAX(price) AS [Most Expensive Item] Tables(s) and query Snack Search criteria WHERE type = "Multi-bag" or type="Family bag" Grouping type Sort order • UPDATE Snack table • SET price to new value (price*0.5 OR price/2) • WHERE criteria based on stock level 1 mark for each bullet Example answer: UPDATE Snack SET price = 0.5*price WHERE stock > 500 • SELECT clause with type and SUM(stock) • Alias as [Total Stock] with FROM and GROUP BY unchanged • ORDER BY with sum(stock) in descending order 1 mark for each bullet Example answer: SELECT type, sum(stock) AS [Total Stock] FROM Snack GROUP BY type ORDER BY sum(stock) DESC • DELETE FROM Snack • WHERE criteria on product name uses both wildcards 1 mark for each bullet Example answers: DELETE FROM Snack WHERE productName LIKE "*Salt*"; Delete FROM Snack WHERE productName LIKE "%Salt%"; Note "*salt*" or “%salt%” is allowed.
Previous question: Higher Computing Science 2021 Question 11