LibraryDB is a database system that keeps track of information concerning the books and their circulation in an imaginary library.
The schema for the LibraryDB database is given below:(note that the book_copy relation keeps track of the physical copies of the books in the library collection.)
- borrow(transactionID, personID*, borrowdate, duedate, returndate)
- author(authorID, firstname, middlename, lastname)
- book_copy(bookID, bookdescID*)
- book(bookdescID, title, subtitle, edition, voltitle, volnumber, language, place, year, isbn, dewey, subjectID*)
- borrow_copy(transactionID*, bookID*)
- person(personID, firstname, middlename, lastname, address, city, postcode, phonenumber, emailaddress, studentno, idcardno)
- publisher(publisherID, publisherfullname)
- written_by(bookdescID*, authorID*, role)
- published_by(bookdescID*, publisherID*, role)
- subject(subjectID, subjecttype)
I tried to write two types of SQL statements in SQLite to display the title of books that were never borrowed.
One using the LEFT OUTER JOIN clause:
SELECT title
FROM book INNER JOIN book_copy
     ON book.BOOKDESCID = book_copy.BOOKDESCID
        LEFT OUTER JOIN borrow_copy
        ON borrow_copy.BOOKID = book_copy.BOOKID
        WHERE borrow_copy.TRANSACTIONID IS NULL;
which returned 323 rows.
One without using the OUTER JOIN clause:
SELECT title 
FROM book 
WHERE bookdescID IN 
      (SELECT bookdescID 
       FROM book_copy 
       WHERE bookID NOT IN 
             (SELECT bookID 
              FROM borrow_copy));
which only returned 298 rows.
Why these two SQL statements return two different results?
Which one is correct in displaying the title of books that were never borrowed?
 
    