I’m doing this exercise where there are 2 tables:
tbl #1 – Fines:
fineID
fineDate
empID
amount
tbl #2 – Complaints:
complaintId
compDate
fineId
description
I am asked to retrieve the fine which had received the most complaints.
The solution provided is this:
SELECT TOP 1 f.fineId, COUNT(c.fineId) comp_count
FROM fines f
JOIN complaints c
ON f.fineId = c.fineId
GROUP BY f.fineId
ORDER BY comp_count DESC;
the output is this:
fineId | comp_count |
---|---|
10001 | 2 |
My initial query is the one below, and it returns the same output as shown above:
SELECT TOP 1 fineId, COUNT(complaintId) comp_count
FROM complaints
GROUP BY fineId
ORDER BY comp_count DESC;
I’m learning SQL and want to understand the rationale behind things. Why did they have to use the fines table, and why did they count the fineId? Is it necessary, and if so, why? I suspect that solution #1 has some sort of purpose; I just don’t understand it.
I’d appreciate your help, thanks!