Authors Who Don't Write Books Anymore

   

Write an SQL query to fetch all the authors who don't write books anymore from the table.



Input :-

Authors table :-
ID is the Primary Key column for the Authors table.

ID

Name

1

Sunny

2

Vicky

3

Adarsh

4

Chetan


Books Table :-
ID is the Primary Key column for the Books table.

ID

AuthorID

1

2

2

3


Output :-

Authors

Sunny

Chetan



Query :-
select Name as 'Authors'
from Authors
where id not in (select AuthorID from Books);

Explanation : - 

This SQL query retrieves the names of authors who haven't written any books.


The outermost part of the query returns the result set. In this case, it selects the column Name from the table ‘Authors’. The query has an inner query which selects the AuthorID from the ‘Books’ table.


The NOT IN operator is used to exclude the rows where the AuthorID from the ‘Authors’ table matches any AuthorID in the subquery result set. In other words, it retrieves the authors whose IDs are not found in the AuthorID column of the Books table. Finally, the result set will contain the names of authors who do not have any corresponding records in the ‘Books’ table.


To summarise, this query retrieves the names of authors who have not written any books by comparing the AuthorID values between the ‘Authors’ and ‘Books’ tables and returning only the authors that do not have a matching AuthorID in the Books table.






No comments

darkmode