Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi,

This question was asked to me in interview.

Tables:
Student: SID,SNMAE
Book: BID,BNAME,BPRICE,SID
Author: AID,ANAME,BID

Question:
Write query which give list of student who take books who's price is >1000 & Book having more than one Author.

Please help me to write above mentioned query.
Posted
Updated 17-Dec-13 17:45pm
v2
Comments
joginder-banger 17-Dec-13 23:54pm    
I hope you used primary key and Foreign key concept. if you are not used..before start the query used the key concept.other wise duplicate value showing your result.
joginder-banger 17-Dec-13 23:54pm    
hit to reply
Yogi ,Pune 17-Dec-13 23:59pm    
Yes I used Primary key & Foreign key concept.
joginder-banger 18-Dec-13 0:02am    
check it solution...
What have you tried and where is the problem?

SQL
SELECT S.SNAME
FROM Student as S
INNER JOIN Book as B
  ON S.SID=B.SID
INNER JOIN Author as A
  ON B.BID=A.BID
WHERE B.BPRICE>1000
GROUP BY A.BID,S.SNAME
HAVING COUNT(A.BID)>1
 
Share this answer
 
v2
Comments
Tom Marvolo Riddle 18-Dec-13 0:49am    
5!
Yogi ,Pune 20-Dec-13 3:56am    
When I write below mentioned code

Select * From Student_Master
Inner Join Book_Master
On Student_Master.SID=Book_Master.SID
Inner Join Author_Master
On Book_Master.BID=Author_Master.BID
Where Book_Master.BPRICE>1000
Group By Author_Master.BID
Having COUNT(Author_Master.BID)>1

SQL throws en error :
Msg 8120, Level 16, State 1, Line 1
Column 'Student_Master.SID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Tables:
Student: SID,SNMAE
Book: BID,BNAME,BPRICE,SID
Author: AID,ANAME,BID

As I mentioned above I want to write query which give list of student who take books who's price is >1000 & Book having more than one Author.
LaxmikantYadav 26-Dec-13 1:05am    
I have updated query now, please check and let me know.
SQL
SELECT DISTINCT(s1.SNAME) FROM Student s1 JOIN BOOK b1 ON
s1.SID = b1.SID WHERE b1.BPRICE > 1000 AND EXISTS
(
SELECT COUNT(a.AID) FROM Book b2 JOIN Author a ON
b2.BID = a.BID WHERE s1.SID = b2.SID GROUP BY a.AID HAVING COUNT(a.AID) > 1
)
 
Share this answer
 
i think after you reading this some idea your problem.

C#
select * from Student st
inner join Book id on st.SID =id.sid
inner join Author at on at.bid=st.bid


for any query hit to reply.
 
Share this answer
 
Comments
TrushnaK 18-Dec-13 0:11am    
where is your condition
price is >1000 & Book having more than one Author.
joginder-banger 18-Dec-13 0:14am    
not a complete script ....i have all ready mention some idea...not a complete solution.
Yogi ,Pune 23-Dec-13 4:55am    
I am facing problem how to write statement which give book having multiple author
Yogi ,Pune 23-Dec-13 4:54am    
Where Book_Master.BPRICE>1000
Group By Author_Master.BID
Having COUNT(Author_Master.BID)>1
TrushnaK 23-Dec-13 5:29am    
which problem you faceing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900