Using Loan(LoanID, BookID, MemberID, LoanDate, Returned), construct an SQL query that shows each MemberID and the number of loans that member has not yet returned (Returned = 0), only for members with more than one such loan, from the most loans to the fewest.
Show the markscheme
Markscheme (4 marks)
SELECT MemberID, COUNT(*) FROM LoanWHERE Returned = 0(filters the rows before grouping).GROUP BY MemberIDHAVING COUNT(*) > 1andORDER BY COUNT(*) DESC(condition on the count in HAVING, not in WHERE).
A full-marks answer
SELECT MemberID, COUNT(*) AS NotReturnedFROM LoanWHERE Returned = 0GROUP BY MemberIDHAVING COUNT(*) > 1ORDER BY NotReturned DESC;WHERE removes the returned loans before the groups are formed; GROUP BY makes one group per member; HAVING keeps only the groups whose count is greater than 1, which WHERE could not do because the counts do not exist until the rows are grouped. On the library data the result is member 3 with 2 loans.