Database query MySQL with nested logical negation?

Good time of day. There are three tables:

books (id, title)
users (id, first_name, last_name, age)
users_books (id, user_id, book_id)
The table users_books contains information about the users purchased books. You should select the list of users who did not buy a book with a certain name.
My request:

SELECT 
 DISTINCT users.first_name, 
 users.last_name, 
 users.id 
FROM 
 users 
 Users_books INNER JOIN ON users.id = users_books.user_id 
WHERE 
 users_books.user_id NOT IN (
 SELECT 
 `user_id` 
 FROM 
 `users_books` 
 WHERE 
 users_books.book_id = (SELECT id FROM books where title = "Book")
 ) 
GROUP BY 
 users_books.user_id


Is it possible to execute a query optimally?
July 9th 19 at 10:18
1 answer
July 9th 19 at 10:20
Normal request only WHERE necessary to correct a bit
SELECT 
 DISTINCT users.first_name, 
 users.last_name, 
 users.id 
FROM 
 users 
 Users_books INNER JOIN ON users.id = users_books.user_id 
WHERE 
 users_books.book_id NOT IN (SELECT id FROM books where title = "Book" ) 
GROUP BY 
 users_books.user_id
The first domestic General WHERE it is necessary to remove - aniya.Kertzmann65 commented on July 9th 19 at 10:23
Can you give a complete example? ATO is something I do not understand... - nikita.Stracke commented on July 9th 19 at 10:26
: corrected comment - aniya.Kertzmann65 commented on July 9th 19 at 10:29
Thank you. - nikita.Stracke commented on July 9th 19 at 10:32

Find more questions by tags DatabasesMySQL