A warehouse uses a database table called STOCK to track inventory. ItemCode Description Supplier QuantityInStock ReorderLevel UnitCost WH001 Steel Bolt M8 M...

Assessment: Computer Science (9-1) 0984 | Paper 2 Mock 01 | Algorithms, Programming and Logic Subject: Computer Science (9-1) - 0984

Question 1 Report

A warehouse uses a database table called STOCK to track inventory.

ItemCodeDescriptionSupplierQuantityInStockReorderLevelUnitCost
WH001Steel Bolt M8MetalCo5001000.15
WH002Rubber WasherSealParts802000.08
WH003Copper Wire 2mMetalCo250501.20
WH004Plastic ClipSealParts1501000.05
WH005Aluminium SheetMetalCo30254.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]

Answer Details

(a) SQL query for items below reorder level: [2]

SELECT *
FROM STOCK
WHERE QuantityInStock < ReorderLevel

This 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 Supplier

The expression QuantityInStock * UnitCost calculates the value for each item, and SUM totals these across each supplier group.

SupplierTotal Value
MetalCo510.00
SealParts13.90

Working:

  • MetalCo: (500 x 0.15) + (250 x 1.20) + (30 x 4.50) = 75 + 300 + 135 = 510
  • SealParts: (80 x 0.08) + (150 x 0.05) = 6.40 + 7.50 = 13.90

(d) Two fields where a range check should be applied: [2]

  1. QuantityInStock: Must be >= 0 (cannot have negative stock).
  2. UnitCost: Must be > 0 (an item cannot have a zero or negative cost).

(e) SQL query to count total records: [2]

SELECT COUNT(*)
FROM STOCK

COUNT(*) 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(*) > 2

GROUP 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 < ReorderLevel

The 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]

  1. Data can be queried using SQL to retrieve specific records quickly without reading the entire file, making searches efficient even with large datasets.
  2. Multiple users can access and update the data simultaneously with access controls, preventing conflicts and restricting sensitive information to authorized personnel.

(i) SQL query for top 5 results sorted descending: [1]

SELECT *
FROM STOCK
ORDER BY UnitCost DESC
LIMIT 5

ORDER 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.

Download The App On Google Playstore

Everything you need to excel in your exams

Green Bridge CBT Mobile App
Personalized AI Learning Chat Assistant
200,000+ Exam Questions Across IGCSE, JAMB, WAEC & NECO
Over 3,900 Lesson Notes
Offline Support - Learn Anytime, Anywhere
Green Bridge Timetable
Literature Summaries & Potential Questions
Track Your Performance & Progress
In-depth Explanations for Comprehensive Learning