Question 1 Report
A warehouse uses a database table called STOCK to track inventory.
| ItemCode | Description | Supplier | QuantityInStock | ReorderLevel | UnitCost |
|---|---|---|---|---|---|
| WH001 | Steel Bolt M8 | MetalCo | 500 | 100 | 0.15 |
| WH002 | Rubber Washer | SealParts | 80 | 200 | 0.08 |
| WH003 | Copper Wire 2m | MetalCo | 250 | 50 | 1.20 |
| WH004 | Plastic Clip | SealParts | 150 | 100 | 0.05 |
| WH005 | Aluminium Sheet | MetalCo | 30 | 25 | 4.50 |
(a) Write an SQL query to find all items where QuantityInStock is below the ReorderLevel. [2]
(b) Write an SQL INSERT statement to add a new item with code 'WH006', description 'Nylon Strap', supplier 'SealParts', quantity 400, reorder level 150, and unit cost 0.30. [3]
(c) Write an SQL query to find the total value of stock (QuantityInStock multiplied by UnitCost) for each supplier. [3]
(d) State two fields where a range check should be applied and describe the valid range for each. [2]
(e) Write a SQL query to count the total number of records in the table. [2]
(f) Write a SQL query to find the average value of a numeric field for each category, showing only categories with more than 2 records. [3]
(g) Write a SQL query to update the status of all records that match a given condition. [2]
(h) Describe two advantages of using a database rather than a flat file for this data. [2]
(i) Write a SQL query to display all records sorted in descending order by a numeric field, limited to the first 5 results. [1]
(a) SQL query for items below reorder level: [2]
SELECT *
FROM STOCK
WHERE QuantityInStock < ReorderLevelThis compares two fields within the same record. From the data, only WH002 (Rubber Washer: quantity 80, reorder level 200) satisfies the condition where 80 < 200.
(b) SQL INSERT statement for a new item: [3]
INSERT INTO STOCK (ItemCode, Description, Supplier,
QuantityInStock, ReorderLevel, UnitCost)
VALUES ('WH006', 'Nylon Strap', 'SealParts',
400, 150, 0.30)All six values must be provided in the correct order matching the field list. String values are enclosed in single quotes; numeric values are written directly.
(c) SQL query for total stock value per supplier: [3]
SELECT Supplier, SUM(QuantityInStock * UnitCost)
FROM STOCK
GROUP BY SupplierThe expression QuantityInStock * UnitCost calculates the value for each item, and SUM totals these across each supplier group.
| Supplier | Total Value |
|---|---|
| MetalCo | 510.00 |
| SealParts | 13.90 |
Working:
(d) Two fields where a range check should be applied: [2]
(e) SQL query to count total records: [2]
SELECT COUNT(*)
FROM STOCKCOUNT(*) counts all rows in the table regardless of any NULL values. This would return 5 for the given data.
(f) SQL query for average per category with a minimum group size: [3]
SELECT Supplier, AVG(UnitCost)
FROM STOCK
GROUP BY Supplier
HAVING COUNT(*) > 2GROUP BY groups records by Supplier, AVG calculates the mean UnitCost within each group, and HAVING filters to show only groups with more than 2 records. In this data, only MetalCo (3 items) would appear; SealParts (2 items) would be excluded.
(g) SQL query to update records matching a condition: [2]
UPDATE STOCK
SET QuantityInStock = 0
WHERE QuantityInStock < ReorderLevelThe UPDATE statement modifies existing records. SET specifies the new value, and WHERE determines which records are affected. Without a WHERE clause, all records would be updated.
(h) Two advantages of using a database over a flat file: [2]
(i) SQL query for top 5 results sorted descending: [1]
SELECT *
FROM STOCK
ORDER BY UnitCost DESC
LIMIT 5ORDER BY UnitCost DESC sorts from highest to lowest cost. LIMIT 5 restricts the output to the first 5 records. This is useful for finding the most expensive items.
Everything you need to excel in your exams