Its saying I have no row selected?

Viewed 586

It's saying I have no row selected.

This is the question: Find the member ID, last name, and first name of the members who have never borrowed any books in the past or currently.

This is the schema, Primary Keys are bold.

Book(bookID, ISBN, title, author, publish-year, category)

Member(memberID, lastname, firstname, address, phone-number, limit)

CurrentLoan(memberID, bookID, loan-date, due-date)

History(memberID, bookID, loan-date, return-date)

Members can borrow books from the library. The number of books they can borrow is limited by the “limit” field of the Member relation (it may differ for different members). The category of a book includes fiction, non-fiction, children’s and reference. The CurrentLoan table represents the information about books that are currently checked out. When the book is returned to the library, the record will be removed from CurrentLoad relation, and will be inserted into History relation with the return-date. A library may have more than one copy of the same book, in which case each copy has its own bookID, but all copies share the same ISBN.

This is my code:

CREATE TABLE Book
    (bookID INT,
    ISBN INT,
    title varchar (25),
    author  varchar (20),
    publish_year INT,
    category varchar(20),
    PRIMARY KEY (bookID));  

CREATE TABLE Member
    (memberID INT,
    lastname varchar (20),
    firstname varchar (20),
    address varchar(20),
    phone_number INT,   
    limit_ INT,
    PRIMARY KEY (memberID));

CREATE TABLE CurrentLoan
    (memberID INT ,
    bookID INT,
    loan_date DATE,
    due_date DATE,
    PRIMARY KEY (memberID, bookID),
    FOREIGN KEY (memberID) REFERENCES Member(memberID),
    FOREIGN KEY (bookID) REFERENCES Book(bookID));


CREATE TABLE History
    (memberID INT,
    bookID INT,
    loan_date DATE,
    return_date DATE,
    PRIMARY KEY (memberID, bookID, loan_date),
    FOREIGN KEY (memberID) REFERENCES Member(memberID),
    FOREIGN KEY (bookID) REFERENCES Book(bookID));

INSERT INTO Book VALUES (10, 1113312336, 'The Dog', 'Jack Crow', 1990, 'fiction');
INSERT INTO Book VALUES (12, 2221254896, 'Worms', 'Jim Kan', 2013, 'childrens');
INSERT INTO Book VALUES (13, 3332546987, 'Crow', 'Jan Flo', 2000, 'fiction'); 
INSERT INTO Book VALUES (14, 4443456215, 'Big Dog', 'Lan Big', 1993, 'children'); 
INSERT INTO Book VALUES (15, 5552314569, 'Green Apple', 'Theo Brown', 1978, 'children');
INSERT INTO Book VALUES (16, 6664581631, 'Red Bean', 'Khang Nk', 2017, 'fiction');
INSERT INTO Book VALUES (17, 7771452369, 'XML and XQuery Knowledge', 'Author Le', 2017, 'non-fiction');
INSERT INTO Book VALUES (18, 8881245525, 'The Dark Room', 'Jack Se', 2017, 'fiction');
INSERT INTO Book VALUES (19, 9991123546, 'Lonely Mens', 'Geen Brown', 2014, 'refrence');
INSERT INTO Book VALUES (20, 1122112356, 'XML or XQuery', 'Heart Le', 2002, 'fiction');

INSERT INTO Member VALUES (001, 'Lee', 'Nancy', 'Brownlea Drive', 1254896325, 2);
INSERT INTO Member VALUES (002, 'Le', 'Ray', '10th Street', 1234561256, 2);
INSERT INTO Member VALUES (003, 'Kan', 'Charlie', '5th Street', 1234567236, 2);
INSERT INTO Member VALUES (004, 'Brown', 'Joe', 'Elm Street', 1234567845, 2);
INSERT INTO Member VALUES (005, 'Smith', 'John', '33 East', 1234567890, 2);

INSERT INTO CurrentLoan VALUES (005, 10, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (005, 19, '13-JAN-17', '15-NOV-17');
INSERT INTO CurrentLoan VALUES (003, 16, '14-FEB-17', '12-MAR-17');
INSERT INTO CurrentLoan VALUES (004, 15, '12-OCT-17', '09-NOV-17');
INSERT INTO CurrentLoan VALUES (005, 18, '13-APR-17', '12-MAY-17');

INSERT INTO History VALUES (001, 10, '14-Jan-17', '04-OCT-17');
INSERT INTO History VALUES (002, 19, '12-Jan-17', '04-NOV-17');
INSERT INTO History VALUES (003, 13, '14-APR-17', '08-OCT-17');
INSERT INTO History VALUES (005, 20, '14-MAY-17', '04-DEC-17');

My query is:

SELECT Member.memberID, lastname, firstname 
    FROM Member MINUS(
                    SELECT Member.memberID, lastname, firstname
                    FROM Member, CurrentLoan
                    WHERE Member.memberID = CurrentLoan.memberID
                    UNION
                    SELECT Member.memberID, lastname, firstname
                    FROM Member, History
                    WHERE Member.memberID = History.memberID);
1 Answers

There's a couple of ways to do this:

You can use an anti-join, such as:

SELECT m.MEMBERID,
       m.LASTNAME,
       m.FIRSTNAME
  FROM MEMBERS m
  WHERE m.MEMBERID NOT IN (SELECT DISTINCT MEMBERID
                             FROM CURRENTLOAN
                           UNION ALL
                           SELECT DISTINCT MEMBERID
                             FROM HISTORY);

Another way (and my preferred method) to do what you want is:

SELECT DISTINCT m.MEMBERID,
                m.LASTNAME,
                m.FIRSTNAME
  FROM MEMBERS m
  LEFT OUTER JOIN (SELECT DISTINCT MEMBERID
                     FROM (SELECT MEMBERID
                             FROM CURRENTLOAN
                           UNION ALL
                           SELECT MEMBERID
                             FROM HISTORY)) u
    ON u.MEMBERID = m.MEMBERID
  WHERE u.MEMBERID IS NULL;

However, given the data you've shown this query, as well as your original query, should return zero rows. SQLFiddle here

Note that if you comment out the current loan to member 004, then "Joe Brown" is returned SQLFiddle here

Best of luck.

Related