+ 1

Internediate SQL, lesson joins, last exercise, Number of books

I'm on the last exercise of sql intermediate (Number of books), lesson called Joins, my code is SELECT Authors.name, COUNT (Authors.name) FROM Authors LEFT JOIN Books ON Authors.id=Books.author_id GROUP BY Authors.name ORDR BY count DESC My code finds 1 piece Stefen King and 1 piece Mark Twain, but is should be O due to the solution. Why is my code showing 1 book for King and Twain? Your Output name,count F. Scott Fitzgerald,3 Jane Austen,3 William Shakespeare,2 Stephen King,1 Miguel de Cervantes,1 Mark Twain,1 Herman Melville,1 Expected Output name,books F. Scott Fitzgerald,3 Jane Austen,3 William Shakespeare,2 Miguel de Cervantes,1 Herman Melville,1 Stephen King,0 Mark Twain,0

9th Nov 2023, 9:02 AM
Christophe
3 Answers
+ 3
Christophe , first you didn't name right the column -> your column is "count", instead of "books". The other thing - it's mentioned in the description of the task that not every author is associated with a book. How do you resolve it in your query?
9th Nov 2023, 9:17 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 3
Available Tables Columns of the Books table: id, name, year, author_id. Columns of the Authors table: id, name. Task Write a query to get the <author names> and the <number of books> they have in the Books table. In your SQL statement, the SELECT clause is not correct. COUNT(Authors.name) is not the required field. You have to count how many books the author has, not how many authors from table Authors. The answer have Stephen King and Mark Twain in the result because they exist in table Authors, but they don't have a book in table Books.
9th Nov 2023, 9:36 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Thanks a lot, the white cat, i saw i forgot the as books Thanks a lot, Wong Hei Ming, i undertand to count the Books.id and not the Authors.name
9th Nov 2023, 10:25 AM
Christophe