~/ learn/ postgresql/ cards/ Normalization & Data Modeling
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/0e8f1ad8-1b89-49cd-bbae-749c3fec8e71/flashcard utf-8 LF