r/DatabaseHelp • • Oct 02 '16

Help with SQL server 2014 Select clauses.

I have an assignment that has been driving me crazy, I have been researching online with helpful information but I believe is beyond my comprehension.

I have two tables one has information about items containing the price of the item, the second has information about the vendor with company name information.

The statement I am supposed to write is going to give me results with data that has greater than 1000$ in price and contain the name "new" in the vendor list.

So far I have:

select ItemID, ItemDescription, CompanyName

from ITEM, VENDOR

Where ItemPrice > 1000 Having like 'New';

I kept getting syntax errors and again have tried to research the problem with no luck please help!

1 Upvotes

8 comments sorted by

View all comments

Show parent comment

3

u/Grundy9999 Oct 02 '16

So since this is a school assignment I will use the socratic method - would you expect your subquery statement to select the company named "SuperNew Inc."? Why or why not? Also, which is more efficient - using an implicit cross-join plus subquery, or specifying an explicit join and putting multiple conditions in the where clause?

1

u/_Royalty_ Oct 04 '16

Since it's a couple of days later, can you (or someone) post your preferred solution? It would be helpful for others trying to learn like myself.

I'll try to answer your question though. His subquery wouldn't be effective because he neglected to use a wildcard operator, right? He would need .... LIKE 'New%' instead. Or am I barking up the wrong tree?

1

u/Grundy9999 Oct 05 '16

There are others here far more knowledgeable than me, but those are the issues I spotted. Assuming that there is an implicit relationship with ItemId as the primary and foreign key, my first attempt would probably be:

select ItemID, ItemDescription, CompanyName from ITEM, VENDOR Where Item.ItemPrice > 1000 and Vendor.CompanyName like "New%";

But I may have missed an issue.

And if a relationship needed to be made between the tables, then a join would have to be specified.