Try using a subquery to select the highest rated agent, then join it to your CLIENT
table to select the names of the associated clients. Something like this:
SELECT ClientName
FROM CLIENT.ClientName
JOIN (
SELECT AgentID FROM AGENT ORDER BY AgentRating DESC LIMIT 1
) sq ON sq.AgentID = CLIENT.AgentID
The subquery (SELECT AgentID FROM AGENT ORDER BY AgentRating DESC LIMIT 1) sq
selects the AgentID
column from the AGENT
table
Then with ORDER BY AgentRating DESC
it orders that column by the AgentRating
descending placing the highest rating at the top of the results.
Then the LIMIT 1
limits the rows returned to 1, giving you only 1 (the first returned) record from the AGENT
table that we just ordered to put the highest rated agent at the top.
Then when you JOIN
that result from the subquery with your CLIENT
table on the AgentID
, you will only get results in your CLIENT
table maching the selected AgentID
from the subquery.