| Author |
Topic |
|
jsaetrum
Starting Member
7 Posts |
Posted - 2011-09-15 : 16:07:15
|
| Hi.I'm new to SQL, and I'm struggling with finding an expression for the following:"Make a list of customers who have ordered all products that customer 20 has ordered."Any hints, what operators I need to use? I have tried to extract the products the customers have and test for whether 20's products are a subset of the customers'. But without success.Thanks. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
jsaetrum
Starting Member
7 Posts |
Posted - 2011-09-15 : 16:26:56
|
| Well, first I tried to divide it in two relations, like:"Which products does each customer have?"SELECT DISTINCT Customers.customerName, Products.productNameFROM Products, OrderDetails, Orders, CustomersWHERE P.productCode = OrderDetails.productCode andOrderDetails.orderNumber = Orders.orderNumber andOrders.customerNumber = Customers.customerNumberin which the '=' joins the relations, for what product does each customer have.And a relation like:"Which products does customer number 20 have?"SELECT DISTINCT Customers.customerName, productNameFROM Products, OrderDetails, Orders, CustomersWHERE Products.productCode = OrderDetails.productCode andOrderDetails.orderNumber = Orders.orderNumber andOrders.customerNumber = Customers.customerNumber andCustomers.customerNumber = 20And from this point I try to figure out how to find out which customers who have ordered all products that customer 20 has ordered.First I thought maybe if the latter relation (the projected productName) can be tested whether is a subset to each of the groups of the customers products.I may think it wrong, the last try was:SELECT Customers.customerName, Products.productNameFROM Products, OrderDetails, Orders, CustomersWHERE Products.productCode = OrderDetails.productCode andOrderDetails.orderNumber = Orders.orderNumber andOrders.customerNumber = Customers.customerNumberand Customers.customerNumber = 20 andProducts.productName in(SELECT Products.productNameFROM Products, OrderDetails, Orders, CustomersWHERE Products.productCode = OrderDetails.productCode andOrderDetails.orderNumber = Orders.orderNumber andOrders.customerNumber = Customers.customerNumber)J.Saetrum |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
jsaetrum
Starting Member
7 Posts |
Posted - 2011-09-15 : 16:38:12
|
| Thanks for your reply!Yes, I considered to use IN, it seems logical, but you mean I need to use tuple variables and operate on a derived relation and the original?These are just basic exercise questions, so it may not be a big deal using IN. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
jsaetrum
Starting Member
7 Posts |
Posted - 2011-09-15 : 16:54:16
|
| Hmm, I get a superset of the right answer. I got this result earlier.Since it was all the products that 20 has, then I thought each customer had to have a subset which is the set of 20's products.such that {products which 20 has} is in {products for each customer} |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
jsaetrum
Starting Member
7 Posts |
Posted - 2011-09-15 : 17:01:58
|
| Now I tried this:SELECT DISTINCT Customers.customerNameFROM Products, OrderDetails, Orders, CustomersWHERE Products.productCode = OrderDetails.productCode andOrderDetails.orderNumber = Orders.orderNumber andOrders.customerNumber = Customers.customerNumber andProducts.productName in(SELECT Products.productNameFROM Products, OrderDetails, Orders, CustomersWHERE Products.productCode = OrderDetails.productCode andOrderDetails.orderNumber = Orders.orderNumber andOrders.customerNumber = Customers.customerNumberand Customers.customerNumber = 20 ) |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|