SQL Experiment 4 Queries
1. Display number of books published in the current year.
select count(*) from titles where to_char(sysdate,'yyyy') = to_char(pubdate,'yyyy');
2. Display publisher ID and maximum price for books on subject ORACLE.
select pubid, max(price) from title where subid = 'ora' group by pubid;
3. Display month and number of books published for each month in the current year.
select to_char(pubdate,'mm'), count(*) nobooks from titles where to_char(sysdate,'yyyy') =
to_char(pubdate,'yyyy') group by to_char(pubdate,'mm');
4. Display month and number of books published for each month in the current year.
select to_char(pubdate,'mm'), count(*) nobooks from titles where to_char(sysdate,'yyyy') =
to_char(pubdate,'yyyy') group by to_char(pubdate,'mm');
5. Display Publisher name and number of books published.
select pname, count(*)from publishers p, titles t where p.pubid = t.pubid group by pname;
6. Display the author who wrote more than 3 titles in last two years.
select auid from titleauthors ta, titles t where months_between(sysdate,pubdate) <= 24 group
by auid having count(*) > 3;
7. Display author name and maximum price of all the books written by the authors.
select aname, max(price) from authors a, titleauthors ta, titles t where t.titleid = ta.titleid and
a.auid = ta.auid group by aname;
8. Display authors who wrote a book on ORACLE.
select * from authors where auid in (select auid from titleauthors where titleid in(select titleid
from titles where subid = ora') );
9. Display subjects in which minimum price <300 maximum price >600.
select * from subjects where subid in( select subid from titles group by subid having min(price)
< 300) and subid in ( select subid from titles group by subid having max(price) > 600);
10. Display the author who has written the costliest book.
select * from authors where auid = ( select auid from titleauthors where titleid = (select titleid
from titles where price = (select max(price) from titles) ) );
11. Display the author who wrote a book with price >500.
select * from authors where auid = ( select auid from titleauthors where titleid = (select titleid
from titles where price > 500) );
12. Display the subject for which we have more than 3 books published in the current year.
select * from subjects where subid in (select subid from titles where to_char(pubdate,'yyyy') =
to_char(sysdate,'yyyy') group by subid having count(*) > 3);
13. Display author name and title of the books for which author is the lead author.
select aname,title from titles t, authors a, titleauthors ta where t.titleid = ta.titleid and a.auid =
ta.auid and importance = 1;
14. Display the authors who wrote a book in the previous year but not in the current year.
select * from authors where auid in ( select auid from titleauthors ta, titles t where ta.titleid =
t.titleid and to_chat(pubdate,'yyyy') = to_char(sysdate,'yyyy') - 1)and auid not in ( select auid
from titleauthors ta, titles t where ta.titleid = t.titleid and to_chat(pubdate,'yyyy') =
to_char(sysdate,'yyyy') );
15. Display year the highest price by taking into account books that are written by more than
one author.
select to_char(pubdate,'yyyy') , max(price) from titles where titleid in (select titleid from
titleauthors group by titleid having count(*) > 1) group by to_chat(pubdate,'yyyy');