Go Back   CodingForums.com > :: Server side development > MySQL > Other Databases

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2013, 01:46 PM   PM User | #1
simon47
New to the CF scene

 
Join Date: Jan 2013
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
simon47 is an unknown quantity at this point
Question structured query language (sql)

can someone please help me with this sql coding.... the diagram is given below..

List the all products which haven’t been bought by any customer....
(i tried this but i m not sure if this is correct)

SELECT ProductID, CustomerID
FROM Products , Customers
WHERE ProductID NOT IN (SELECT CustomerID);
Attached Thumbnails
Click image for larger version

Name:	sql.jpg
Views:	48
Size:	48.3 KB
ID:	11929  

Last edited by simon47; 02-13-2013 at 04:47 AM..
simon47 is offline   Reply With Quote
Old 02-12-2013, 01:58 PM   PM User | #2
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
3.

Code:
SELECT p.ProductID
FROM Products p left join Sales s on p.ProductID = s.ProductID
WHERE s.ProductID is null
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search
BubikolRamios is offline   Reply With Quote
Old 02-12-2013, 02:23 PM   PM User | #3
simon47
New to the CF scene

 
Join Date: Jan 2013
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
simon47 is an unknown quantity at this point
Quote:
Originally Posted by BubikolRamios View Post
3.

Code:
SELECT p.ProductID
FROM Products p left join Sales s on p.ProductID = s.ProductID
WHERE s.ProductID is null
the customer table is missing from the code section...customer table isnt mentioned anywhere in code?
simon47 is offline   Reply With Quote
Old 02-12-2013, 02:52 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,748
Thanks: 4
Thanked 2,466 Times in 2,435 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by simon47 View Post
the customer table is missing from the code section...customer table isnt mentioned anywhere in code?
Why would it need to be? Your query specifies to find products that haven't been bought by customers. Since customers doesn't have any direct relationship to product, there's no need to include the customers table as anything not bought will not have a reference within the sales table.
So all you need to find is anything within the product table that has no related record within sales. Since the product has never been sold, there is no reason to collect any Customer information from it.

This is clearly homework as well, so he really shouldn't have given you the answer to the third one. The first two are simply aggregates and groups.

This is also access, which is. . . not great SQL wise. MySQL code will not run properly within it for two reasons: the functions are different for anything proprietary, and Access is incredibly easy to confuse if you start joining more than two tables together.
So this belongs in the other databases section.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 02-13-2013, 04:49 AM   PM User | #5
simon47
New to the CF scene

 
Join Date: Jan 2013
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
simon47 is an unknown quantity at this point
well i just wanted to learn that why the customer table is not mentioned in the code...but thanks anyways.....
simon47 is offline   Reply With Quote
Old 02-14-2013, 09:21 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Probably the LEFT JOIN answer is beyond where he is in the class or book, anyway.

Probably the instructor is expecting him to use a NOT IN( ) clause at this stage in the class.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-14-2013, 09:37 PM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,530
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
Probably the LEFT JOIN answer is beyond where he is in the class or book, anyway.

Probably the instructor is expecting him to use a NOT IN( ) clause at this stage in the class.
That's one of the problems with giving answers to homework - the answer will almost certainly include code that is well beyond that so far covered in the course. Of course that makes it far easier for the teacher to tell who cheated since anyone using code that is beyond where they are in class would need to be able to explain exactly how that code works in order to prove that they really are ahead of the class and if they can't then it will be obvious they cheated. Most likely the teacher already knows who is ahead anyway and so wouldn't even need to ask.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:25 PM.


Advertisement
Log in to turn off these ads.