0

I have two tables: loan and librarian.

librarian: idLibrarian, libName and libPassword loan: idLoan,...,idLibrarian

Each loan has a idLibrarian. What I'm trying to do is find which librarian has the most loans. I can count number of librarians and number of loans. But i can't figure out how to group all the loans by idLibrarian and find the one with the highest amount of loans.

1 Answer 1

1
select idLibrarian, count(*) from loan group by idLibrarian order by 2 desc

First row will be the librarian with the most loans, etc.

Sign up to request clarification or add additional context in comments.

2 Comments

For some reason i get an error. This is what i've written:s.executeQuery("SELECT idLibrarian, COUNT(*) " + "FROM loan" + "GROUP BY idLibrarian" + "ORDER BY 2 DESC");
Looks like you are missing some whitespaces, between load and GROUP and between idLibrarian and ORDER. "SELECT idLibrarian, COUNT(*) " + "FROM loan" + " GROUP BY idLibrarian " + "ORDER BY 2 DESC". Or you could skip concatenation and write it all in one string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.