~/ learn/ postgresql/ cards/ Normalization, briefly
1 of 1

Show each author's name and how many distinct books they have (named book_count), ordered by book_count descending then name. This is the normalized link in action.

Show each author's name and how many distinct books they have (named book_count), ordered by book_count descending then name. This is the normalized link in action.

Answer

SELECT a.name, COUNT(b.id) AS book_count FROM authors a LEFT JOIN books b ON b.author_id = a.id GROUP BY a.name ORDER BY book_count DESC, a.name;

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/7536430b-9534-4bb8-b3d0-0a36821e9d25/flashcard utf-8 LF