Skip to content
Advertisement

Aggregate Functions And GROUP BY

I have following query as homework for sql:-

Write a query that tells how many books each author has written. On each row provide first the authorid, followed by the number of books. Order the listing so that the author with the most books written is at the top, and at the bottom are those that have written the fewest number of books. The authors that the same number of books written are further ordered by their AuthorID’s in an increasing order.

I have already tried different options via replacing groupby, orderby and count() functions.

Your result

Expected result

Advertisement

Answer

You are missing this condition:

The authors that the same number of books written are further ordered by their AuthorID’s in an increasing order.

That requires a second ORDER BY key:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement