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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -