tubvajlis tubvajlis - 1 year ago 118
SQL Question

which operator would be best to use for solving this query

I'm trying to solve this question and I'm not sure what operator to use. I was thinking of using nested query.

This is the question: Find the lastname and firstname of members who have borrowed both books titled “XML and XQuery” and “XQuery: The XML Query Language” either currently or in the past.

This is my code:

CREATE TABLE Book
(bookID INT,
ISBN INT,
title varchar (30),
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', '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, 'XQuery: The XML Query Language', 'Heart Le', 2002, 'fiction');
INSERT INTO BOOK VALUES (21, 1212121212, 'The Giving Tree', 'Shel Silverstein', 1964, 'fiction');
INSERT INTO BOOK VALUES (22, 1212121212, 'The Giving Tree', 'Shel Silverstein', 1964, 'fiction');
INSERT INTO BOOK VALUES (23, 1313131313, 'The Lazy Dog', 'Jake Red', 2016, 'children');
INSERT INTO BOOK VALUES (24, 1313131313, 'The Lazy Dog', 'Jake Red', 2016, 'children');
INSERT INTO BOOK VALUES (25, 1414141414, 'The Very Blue Boy', 'Ben Jen', 2006, 'fiction');

INSERT INTO Member VALUES (001, 'Lee', 'Nancy', 'Brownlea Drive', 1254896325, 10);
INSERT INTO Member VALUES (002, 'Le', 'Ray', '10th Street', 1234561256, 2);
INSERT INTO Member VALUES (003, 'Kan', 'Charlie', '5th Street', 1234567236, 8);
INSERT INTO Member VALUES (004, 'Brown', 'Joe', 'Elm Street', 1234567845, 9);
INSERT INTO Member VALUES (005, 'Smith', 'John', '33 East', 1234567890, 3);
INSERT INTO Member VALUES (006, 'Khang', 'Nkaujyi', '358 Spencer', 2145345625, 5);
INSERT INTO Member VALUES (007, 'Jones', 'David', '4th Street', 14812365789, 10);

INSERT INTO CurrentLoan VALUES (001, 20, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (002, 19, '13-JAN-17', '15-NOV-17');
INSERT INTO CurrentLoan VALUES (003, 17, '14-FEB-17', '12-MAR-17');
INSERT INTO CurrentLoan VALUES (005, 15, '12-OCT-17', '09-NOV-17');
INSERT INTO CurrentLoan VALUES (005, 18, '13-APR-17', '12-MAY-17');
INSERT INTO CurrentLoan VALUES (007, 25, '02-OCT-17', '25-DEC-17');

INSERT INTO History VALUES (004, 17, '14-Jan-17', '04-JUN-17');
INSERT INTO History VALUES (003, 19, '12-Jan-17', '04-NOV-17');
INSERT INTO History VALUES (002, 13, '14-APR-17', '08-OCT-17');
INSERT INTO History VALUES (005, 20, '14-MAY-17', '04-DEC-17');
INSERT INTO History VALUES (007, 12, '02-OCT-16', '25-DEC-16');
COMMIT;


Here is my query:

SELECT Book.title, lastname, firstname
FROM Book, Member
WHERE Book.title like '%XML%XQuery%' or title like '%XQuery%XML%'
AND EXISTS (SELECT Book.bookID, Member.memberID
FROM Member, Book, CurrentLoan
WHERE Book.bookID = CurrentLoan.bookID
AND Member.memberID = CurrentLoan.memberID);

Answer Source

See if this query works for you.

select m.firstname, m.lastname
from (select memberID, bookID from CurrentLoan union all select memberID, bookID from History) a
join Member m on m.memberID = a.memberID
join Book b on b.bookID = a.bookID
where b.title IN ('XML and XQuery', 'XQuery: The XML Query Language');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download