Test Index

CBSE Class 12 Computer Science 2026 Paper Compartment Get it on Google PlayDownload on the App Store

© examsnet.com
Question : 34 of 37
Marks: +1, -0
Consider a database LIBRARY_CATALOG containing the following two tables – PUBLISHERS and BOOKS given below :
Table : PUBLISHERS
PublIDNameCountryGenre
101Grand BooksUSAFiction
102World Lit PressUKNon-Fiction
103Tech TomeGermanySci-Fi
104Story WeaverUSAFiction
105Global InsightsCanadaNon-Fiction
Table : BOOKS
BookIDTitlePublIDPublYearPages
3001The Last Horizon1012018450
3002Echoes of Tomorrow1032020320
3003A Brief History1022015580
3004Whispers in the Wind1012019280
3005Starship Odyssey1032021400
3006The Silent Shore1042022350
3007Understanding AI1032019290
Note : The attribute Pages in the table BOOKS represents the total number of pages in a book, PublID represents the Publisher's Identification Number, PublYear represents the Year of Publication.
Write the SQL queries for the following :
(I) (a) To display Title of all BOOKS and their corresponding PublYear which belong to the 'Fiction' type of Genre (from PUBLISHERS table) through Equi Join.
OR
(b) To display Title of all BOOKS and their corresponding PublYear which belong to the 'Fiction' type of Genre (from PUBLISHERS table) through Natural Join.
(II) To display Name of all PUBLISHERS and their corresponding Title of BOOKS in ascending order of PublYear.
(III) To delete all records from the table BOOKS whose PublYear is older than 2020.
(IV) To open the database LIBRARY_CATALOG and then display the names of all tables present in the database.
Solution:  
Concept: The common column of the two tables is PublID. An equi join names both tables and matches them with an explicit equality condition in WHERE; a natural join matches automatically on the columns that have the same name (here only PublID) and shows that column once.
(I)(a) Equi Join:
SELECT Title, PublYear
FROM BOOKS, PUBLISHERS
WHERE BOOKS.PublID = PUBLISHERS.PublID
AND Genre = 'Fiction';
OR (I)(b) Natural Join:
SELECT Title, PublYear
FROM BOOKS NATURAL JOIN PUBLISHERS
WHERE Genre = 'Fiction';
Either query gives the books of publishers 101 and 104:
TitlePublYear
The Last Horizon2018
Whispers in the Wind2019
The Silent Shore2022
(II) Join on PublID and sort by PublYear (ascending is the default):
SELECT Name, Title
FROM PUBLISHERS P, BOOKS B
WHERE P.PublID = B.PublID
ORDER BY PublYear;
(The first row is World Lit Press, A Brief History (2015) and the last is Story Weaver, The Silent Shore (2022).)
(III) "Older than 2020" means published before 2020, i.e. PublYear < 2020 (2020 itself is not older).
DELETE FROM BOOKS
WHERE PublYear < 2020;
(This removes books 3001, 3003, 3004 and 3007.)
(IV) USE selects (opens) a database; SHOW TABLES lists the tables in the current database.
USE LIBRARY_CATALOG;
SHOW TABLES;
Key point: In an equi join the join condition (BOOKS.PublID = PUBLISHERS.PublID) must be written; without it every book is paired with every publisher (a Cartesian product).
© examsnet.com
Advertisements
Go to Question: