Full Board-Style Sets
The real thing: a table you have not worked with, followed by parts (a) to (f) worth one or two marks each. Give yourself the paper’s time — about fifteen minutes for a set — and write all six answers before opening any of them.
1The table
Study it the way you would in the hall: the column names, the types you can infer, and above all the blanks.
Pencil Box has no quantity and Eraser has no purchase date. In a real paper those blanks are never accidental — at least one part of the question will turn on them.2Set A — write the query
100, most expensive first.▶Show the answer
▶Show the answer
“Not recorded” is the phrase that means is null. Writing qty = null or qty = 0 both give the wrong answer — the first silently, the second because no item has a quantity of zero.
▶Show the answer
Eight items, five suppliers — Nataraj, Camlin and Milton each supply two.
Box.▶Show the answer
% on both sides, because Box is in the middle of one name and at the end of the others.
▶Show the answer
Here is where the planted blank pays off. Stationery shows count(*) of 5 but its quantities add to 990 from only four items — Pencil Box’s NULL was skipped by sum while still being counted as a row by count(*).
50, with that average rounded to two decimals.▶Show the answer
Both categories qualify. Stationery only just does — four cheap items and one at 120 average to 54.10, just over the line.
3Set B — output and theory
The second half of a paper’s SQL question usually switches from writing queries to reading them.
item table.▶Show the answer
Degree 7 and cardinality 8 — seven columns, eight rows. Degree is the number of columns however many rows come and go.
item, and why?▶Show the answer
icode. It is different for every row and it is never empty, which is exactly what a primary key must guarantee. iname happens to be unique in these eight rows, but nothing stops two items sharing a name later, and supplier already repeats.
select count(*), count(qty), count(pdate) from item;▶Show the answer
8, 7 and 7. count(*) counts rows and gets all eight. count(qty) skips Pencil Box’s NULL, and count(pdate) skips Eraser’s — seven each, but for different rows.
select category, count(*) from item where count(*) > 3 group by category; and it fails. Correct it.▶Show the answer
A condition on an aggregate cannot go in where, which is applied before the grouping exists. It belongs in having:
4Marking yourself honestly
The query would run and answer the question asked. Different but equivalent wording is fine.
Right approach, one slip — a missing quote, > for >=, where instead of having.
Right words in the wrong order, or a query that answers a different question.
The row count and the headings are part of the answer, not decoration.
select do not.A category has 5 items but one has NULL qty. What do count(*) and sum(qty) report for it?
Why is icode a better primary key for item than iname?