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
 Database Design and Application Architecture
 Need help with my SQL select statements. Please!

Author  Topic 

dlmagers10
Starting Member

48 Posts

Posted - 2010-10-11 : 15:53:13
Suppose you have a table by the name of BOOK B columns including BOOK_CODE, TITLE, PUBLISHER_CODE, TYPE, PRICE, PAPERBACK.

Then you have another table called PUBLISHER P columns including PUBLISHER_CODE, PUBLISHER_NAME, CITY

Question:
List the book title for each book that has the type PSY and that is published by Berkley Publishing?

I am working with multiple tables.

I need someone to help me and explain maybe. Please.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-11 : 15:55:21
Please post what you have so far.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dlmagers10
Starting Member

48 Posts

Posted - 2010-10-11 : 15:56:57
SELECT TITLE
FROM BOOK
WHERE EXISTS
(SELECT *
FROM PUBLISHER P
WHERE TITLE = 'Berkley'
and TYPE = 'PSY');
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-11 : 15:58:42
Use a JOIN instead, linking the PUBLISHER_CODE columns together.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dlmagers10
Starting Member

48 Posts

Posted - 2010-10-11 : 15:59:04
SELECT TITLE
FROM BOOK
WHERE EXISTS
(SELECT *
FROM PUBLISHER P, book b
WHERE p.publisher_name = 'Berkley'
and b.TYPE = 'PSY');
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-11 : 16:01:15
This will get you started:

select *
from BOOK b
join PUBLISHER p
ON b.PUBLISHER_CODE = p.PUBLISHER_CODE

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dlmagers10
Starting Member

48 Posts

Posted - 2010-10-11 : 16:06:39
Should I use an EXISTS?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-11 : 16:08:33
No, a join is all you need. You now just need to add your WHERE clause.

Please note that we will only assist you with your homework and not do your assignment for you. So I'm giving you hints as you need to complete this work.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dlmagers10
Starting Member

48 Posts

Posted - 2010-10-11 : 16:11:15
Thank you very much Tara. This semester is definitely a struggle for me. I am in my second year of school trying to achieve my associates degree in Web Technologies. Thanks again.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-11 : 16:12:42
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dlmagers10
Starting Member

48 Posts

Posted - 2010-10-11 : 22:06:50
Tara,

By the way, I wanted to let you know that I figured it out. I was using 'Berkley' as 'Publisher_code' when I should of used 'BP' in my where statement. The common factor was not Publisher_Name rather Publisher_code. I got the whole select statement as:

SELECT TITLE
FROM BOOK B
JOIN PUBLISHER P
ON B.PUBLISHER_CODE = P.PUBLISHER_CODE
WHERE P.PUBLISHER_CODE LIKE 'BP'
AND B.TYPE LIKE 'PSY';

which gave me the correct answer.

Thank you again for helping me.

DM
Go to Top of Page
   

- Advertisement -