diff --git a/DBMS/SQL/Week5/writeup.md b/DBMS/SQL/Week5/writeup.md index d00433b..e578dd7 100644 --- a/DBMS/SQL/Week5/writeup.md +++ b/DBMS/SQL/Week5/writeup.md @@ -411,6 +411,20 @@ DAA 2 ``` ### List the departments whose all course text books are published by a particular publisher. +```sql +SQL> select distinct dept + 2 from course + 3 where not exists( + 4 select * + 5 from text natural join book_adoption + 6 where book_adoption.course# = course.course# + 7 and publisher!='Pearson'); + +DEPT +------------------------------ +ICT +``` + ### Find the students who have enrolled for course of more than one department @@ -418,13 +432,119 @@ DAA 2 ### List the department which adopts all the books from the particular publisher +```sql +SQL> select distinct dept + 2 from course natural join Book_Adoption + 3 where book_isbn not in + 4 (select book_isbn + 5 from course natural join book_adoption join text using(book_isbn) + + 6 where publisher !='Pearson' + 7 ); + +DEPT +------------------------------ +ICT + +SQL> select distinct dept + 2 from course + 3 where not exists + 4 ( + 5 select * + 6 from book_adoption natural join text + 7 where publisher !='Cengage' + 8 and course.course# =book_adoption.course# + 9 ); + +DEPT +------------------------------ +EVS +CS +``` + ### List the books which are adopted by the course as well as enrolled by the student +```sql +SQL> select booktitle + 2 from text + 3 where book_isbn in + 4 ( + 5 select distinct book_isbn + 6 from book_adoption join enroll using(book_isbn) + 7 ); + +BOOKTITLE +-------------------- +DBMS vol 1 +DBMS vol 2 +DAA vol 2 +COA vol 666 +DSD vol 99 +Math vol 66 +Poetry vol 0 + +7 rows selected. +``` + ### List the courses which has adapted at least two books from a specific publisher +```sql +SQL> select course#,cname + 2 from text natural join book_adoption join course using(course#) + 3 where publisher='Pearson' + 4 group by (course#,cname) + 5 having count(book_isbn)>=2; + + COURSE# CNAME +---------- ------------------------------ + 1102 DAA +``` + ### Identify the students who are enrolled for maximum number of books. +```sql +SQL> select regno,name,count(book_isbn) + 2 from enroll natural join student + 3 group by regno,name having count(book_isbn)>= all + 4 (select count(book_isbn) + 5 from enroll natural join student + 6 group by regno); + +REGNO NAME COUNT(BOOK_ISBN) +---------- -------------------- ---------------- +456 Aadit 2 +123 Ashwin 2 +789 Shrikanth 2 +``` + ### List the publishers along with the number of books published by them. +```sql +SQL> select publisher,count(book_isbn) + 2 from (select * from text) + 3 group by publisher; + +PUBLISHER COUNT(BOOK_ISBN) +-------------------- ---------------- +Penguin 1 +Cengage 2 +OML 1 +Pearson 3 +Classmate 1 + +SQL> +SQL> select publisher,count(book_isbn) + 2 from text + 3 group by publisher; + +PUBLISHER COUNT(BOOK_ISBN) +-------------------- ---------------- +Penguin 1 +Cengage 2 +OML 1 +Pearson 3 +Classmate 1 +``` + ### List the students who enrolled for all the books adopted by their course