T
T
TemaKam2021-01-26 16:38:53
SQL
TemaKam, 2021-01-26 16:38:53

How to display a list of books that all library subscribers have read?

It is necessary to display a list of books that all subscribers have read
EytJFGg.jpg
. That is, from the lend table, you need to get such book_code values ​​\u200b\u200bthat all subscribers took (bilet_number) - so that this table contains records that each bilet_number took this particular book_code
Here is how to do it, I can’t understand in any way

It won’t work, because 1 subscriber could take the book several times

SELECT book_code FROM lend
GROUP BY book_code
HAVING COUNT(book_code) = (SELECT COUNT(bilet_number) FROM abonents)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-01-26
@TemaKam

You should use the distinct predicate along with the count function :

select book_code, count(distinct bilet_number) lends
from lends
group by book_code
having count(distinct bilet_number) = (select count(distinct bilet_number) from abonents)

Check SQL query

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question