Middle+
Write an SQL query to list authors who have more than 10 books (tables: authors, books, many-to-many relationship).
sobes.tech AI
Answer from AI
sql SELECT a.author_id, a.author_name, COUNT(b.book_id) AS books_count FROM authors a JOIN author_book ab ON a.author_id = ab.author_id JOIN books b ON ab.book_id = b.book_id GROUP BY a.author_id, a.author_name HAVING COUNT(b.book_id) > 10;
It is assumed that there are three tables:
- authors (author_id, author_name)
- books (book_id, ...)
- author_book (author_id, book_id) — a many-to-many relationship table.
The query selects authors who have more than 10 related books.