Here is what my tables look like:
CREATE TABLE Author(
authorID INT PRIMARY KEY,
name VARCHAR(30)
);
CREATE TABLE book(
ISBN INT PRIMARY KEY,
title VARCHAR(30),
authorID INT,
inventory INT,
paperBack BOOLEAN,
fiction BOOLEAN,
FOREIGN KEY (authorID) REFERENCES Author(authorID)
);
I need to find out which author has written the most books.
I am working with a mixture of the following. I guess I am having trouble piecing it all together…
SELECT authorID, count(*)
from book
group by authorID;
I am not sure how to get the single row that has the highest count and then only get the authorID of that row. Once I have that authorID, I know how to get the name.
If all you want is the ID of the author, then use
ORDER BYandLIMIT 1as others have pointed out. However, if you’re going to want other fields from Author, or if you are interested in multiple authors (second highest count, lowest count, etc), then you should consider joining to a derived table, like this:Would give a result set like this: