MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 10th, 2012, 01:20 PM
Armis Armis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 6 Armis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 8 m 29 sec
Reputation Power: 0
Need help with SQL statements

Since the beggining of me starting to study SQL, I've been having trouble. I need to write statements.
my relations are :
SUPPLIER(SupplierID, SupplierName, Phone, Address)
INVENTORY(ProductID, ProductName, Price, SupplierID)

So far I've created the 2 tables and put few records in.
Create table Supplier
(
SupplierID Int Not Null,
SupplierName VarChar (15) Not null,
Phone Int (10) Not Null,
Address VarChar (30) Not Null,
Constraint Supplier_pk Primary Key (SupplierID)
);

Create table Inventory
(
ProductID Int Not NULL,
ProductName VarChar(30) NOT NULL,
Price Char(8) Not NULL,
SupplierID Int not null,
Constraint Inventory_pk Primary Key (ProductID),
Constraint Inventory_fk Foreign Key (SupplierID)
References Supplier (SupplierID)
);

Insert into Supplier Values (1234, 'Costco', 5553338888 , 'Avenue 5');
Insert into Inventory Values (0987, 'Gum', '4.99', 1234);


What I need help with is... the statements. I can write simple ones, but these look way above of what I know.
a) Write an SQL statement to display each SupplierID and beside it, how many different products the store carries by that supplier. Use an alias.
b)Write an SQL statement to display the supplierID, productID, and price for all products that cost $5 or less. The products should be displayed by increasing supplierID, and within each supplierID, by decreasing productID.
c) Write an SQL statement to add a column to the INVENTORY relation which will contain the quantity on hand of a product. The column should be named Quantity. Then, populate the column with values.
d) assume "part c" was done. Write an SQL statement to display ProductID, Name of product, Name of supplier, and InventoryValue for every product. Use an alias.
e)In the SUPPLIER relation, the Phone attribute contains exactly 10 characters, the first 3 of which are the area code. Write an SQL statement to display the SupplierID and Name(of the supplier) for all suppliers whose phone number doesn't contain a local area code (703,571).

Please help if you can. Thanks.

Reply With Quote
  #2  
Old December 10th, 2012, 07:27 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 54 m 49 sec
Reputation Power: 4140
Quote:
Originally Posted by Armis
I can write simple ones, but these look way above of what I know.
those actually are pretty simple

give them a try, an honest try, and we'll help correct them

but we aren't going to do your homework for you
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old December 10th, 2012, 07:47 PM
Armis Armis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 6 Armis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 8 m 29 sec
Reputation Power: 0
Yeah I've tried. Gotten 3 done but not sure if they are right.

B)
Select SupplierID, ProductID, Price
from Supplier, Inventory
where price <= 5
order by supplierID, productID Desc;

C)
alter table Inventory
Add Quantity Int;
Update Inventory
Set Quantity = 10
where ProductID = 0987;

E)
Select SupplierID, SupplierName
from Supplier
where phone <> (571,703);

Reply With Quote
  #4  
Old December 10th, 2012, 09:01 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 54 m 49 sec
Reputation Power: 4140
b) you've got a cross join there, because you forgot the join condition

c) looks okay

e) you can't have a column value compared to two values like that

Reply With Quote
  #5  
Old December 10th, 2012, 09:10 PM
Armis Armis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 6 Armis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 8 m 29 sec
Reputation Power: 0
Quote:
Originally Posted by r937
b) you've got a cross join there, because you forgot the join condition

c) looks okay

e) you can't have a column value compared to two values like that


Any tips on how to fix them and how to do A and D?

Reply With Quote
  #6  
Old December 11th, 2012, 03:51 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 54 m 49 sec
Reputation Power: 4140
to fix b), add the join condition that properly joins Supplier and Inventory

to fix c), you have to figure out a way to isolate the fist few digits of an INTEGER phone number

i would use a range test --
Code:
WHERE NOT phone BETWEEN 5710000000 AND 5719999999
  AND NOT phone BETWEEN 7030000000 AND 7039999999


for a), use GROUP BY and COUNT

for d), it's another simple join

Last edited by r937 : December 11th, 2012 at 03:53 AM.

Reply With Quote
  #7  
Old December 11th, 2012, 09:58 AM
Armis Armis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 6 Armis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 8 m 29 sec
Reputation Power: 0
Quote:
Originally Posted by r937
to fix b), add the join condition that properly joins Supplier and Inventory

to fix c), you have to figure out a way to isolate the fist few digits of an INTEGER phone number

i would use a range test --
Code:
WHERE NOT phone BETWEEN 5710000000 AND 5719999999
  AND NOT phone BETWEEN 7030000000 AND 7039999999


for a), use GROUP BY and COUNT

for d), it's another simple join


for A) would I use the alias for the products or supplier? That's what confuses me, doing the alias statements.
for D) again, alias for inventory value or product?

Reply With Quote
  #8  
Old December 11th, 2012, 10:17 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 54 m 49 sec
Reputation Power: 4140
the aliases that the questions want you to use are column aliases

write the queries and you'll see why

Reply With Quote
  #9  
Old December 11th, 2012, 01:07 PM
Armis Armis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 6 Armis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 8 m 29 sec
Reputation Power: 0
B) How i joined the supplier and inventory
Select SupplierID, ProductID, Price
from Supplier. SupplierID = Inventory. SupplierID
where price <= 5
order by supplierID, productID Desc;


for C) I did this
Select SupplierID, SupplierName
from Supplier
where not phone >= 703000000 and phone <= 703999999
and not phone >= 571000000 and phone <= 571999999;


for A) i came up with this
select SupplierID
from Supplier;
select P as productID
from Inventory
group by productID;
having count(*) > 1000;

D)
Select productID, productName, supplierName, InventoryValue
from Supplier, Inventory;
Select P AS ProductID
from Inventory
order by productName;

Reply With Quote
  #10  
Old December 11th, 2012, 02:42 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 54 m 49 sec
Reputation Power: 4140
you need to do some studying and/or revising, because your sql has serious errors

from Supplier. SupplierID = Inventory. SupplierID
is not a valid FROM clause

where not phone >= 703000000 and phone <= 703999999
and not phone >= 571000000 and phone <= 571999999;

is logically incorrect (see my solution above)

select SupplierID
from Supplier;
select ...

two consecutive sql statements is not invalid, but it's nowhere near close to a solution for this question
also, P as productID is not the right way to assign an alias

your solutions look really slapped together, and like i said, you need to put more work into learning sql

Reply With Quote
  #11  
Old December 11th, 2012, 02:52 PM
Armis Armis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 6 Armis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 8 m 29 sec
Reputation Power: 0
Well I have these statements page and I have to come up with solutions for the ABDCE.

But Thanks for your help. There's no way I'm gonna understand this.
Thanks.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Need help with SQL statements

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap