How can I use SQL to find records where say an author has multiple books with different page counts? -
say have table of books columns author_name, book_name, , page_count.
how write sql find me instance author has written multiple books , @ least 2 books same author have different page counts?
i've managed retrieve list of authors multiple books by
select author_name books group author_name having count(book_name) > 1
which believe that, how check each book compare page counts?
you can try this:
select author_name books group author_name having count(distinct page_count) > 1
this doesn't multiple books, because if there multiple page counts, there multiple books.
for performance reasons, use this:
select author_name books group author_name having min(page_count) <> max(page_count)
usually, count(distinct)
more expensive doing min()
, max()
.
if want list of books, join list. here example using in
subquery:
select b.* books b b.author in (select author_name books group author_name having min(page_count) <> max(page_count) )
Comments
Post a Comment