Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Create a Relation

Author  Topic 

pooja28
Starting Member

6 Posts

Posted - 2011-03-17 : 01:11:18
I created the following tables and inserted few values.

create table books(bookid integer primary key, booktitle varchar(20), year integer, publisherid integer foreign key references publisher(publisherid), price integer, number integer)

create table publisher(publisherid integer primary key, publishername varchar(20)) create table author(authorid integer primary key, authorname varchar(20))

create table bookauthor(bookid integer foreign key references books(bookid), authorid integer references author(authorid), earnings integer )

create table bookreference(bookid integer foreign key references books(bookid), referencebook varchar(20), times integer)

create table reviewer(reviewerid integer primary key, reviewername varchar(20))

create table bookreview(bookid integer foreign key references books(bookid), reviewerid integer foreign key references reviewer(reviewerid), score integer)

Now, I want to solve following query in SQL Server 2000. Find all the books published in 2003 and reviewed by both ‘Sammer Tulpule’ and ‘Hemant Mahta’ . I am not getting any idea about query. How can I write it?

Thanks,
Pooja

Thanks & Regards,
Pooja

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2011-03-17 : 01:30:44
SELECT B.BOOKITITLE FROM BOOKREVIEW BR
LEFT JOIN BOOKS B
ON BR.BOOKID=B.BOOKID
LEFT JOIN REVIEWER R
ON R.REVIEWERID=BR.REVIEWERID
WHERE B.YEAR=2003 AND
R.REVIEWERNAME IN('SAMMER TULPULE','HEMANT MAHTA')
This should do it.
Go to Top of Page

pooja28
Starting Member

6 Posts

Posted - 2011-03-17 : 08:53:02
@ahmeds08, your query is write, it shows the name of books published in 2003 and reviewed by one of the both reviewers. But, i actually want to display all books in 2003 and reviewed by both reviewers not one of them.

Thanks & Regards,
Pooja
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2011-03-17 : 14:55:11
A small change in ahmeds code:

SELECT B.booktitle FROM BOOKREVIEW BR
INNER JOIN BOOKS B
ON BR.BOOKID=B.BOOKID
INNER JOIN REVIEWER R
ON R.REVIEWERID=BR.REVIEWERID
WHERE B.YEAR=2003 AND
R.REVIEWERNAME IN('SAMMER TULPULE','HEMANT MAHTA')
GROUP BY B.booktitle having count(*) =2 -- 2 indicate the count of Reviewer

Regards,
Bohra

I am here to learn from Masters and help new bees in learning.
Go to Top of Page
   

- Advertisement -