-1

Im having trouble on a homework question, im finding it difficult to understand the logic behind what to do.

Q: Write a query which will list the customer name and book title for the customer who has purchased the most expensive book.

Relational Schema Customer(CID, Name, Address)

Orders(Ordernum, CID, Order_date, Cardnum)

Orderlist(Ordernum, ISBN, Qty, Ship_date)

Book(ISBN, Title, Author, Qty_in_stock, Price)

this is my attempt:

rename(expensive,projection cid (orders) join Orderlist join (selection price < d.price ((book) cross-product Pd Book))

exp - projection price (Book)

projection name Customer join exp

  • 2
    You should regard what code have you tried. – SaidbakR Jun 02 '18 at 20:20
  • not really a helpful response – Tumelo Dakarai Jun 02 '18 at 20:25
  • Please read & act on hits googling 'stackexchange homework' & explain what you think is right & what you are not sure about & why. Read the edit help & format your code. There is no 1 RA or RA query language, please give a reference to what you are supposed to use & what operators of it you are allowed to use. But your code doesn't make sense. (Do you want an assignment? Rename what to what? Etc.) But anyway this is a faq, see [ask] & the downvote arrow mouseover text. Explain how other questions don't help with the part you are unsure about. [PS](https://stackoverflow.com/a/24425914/3404097) – philipxy Jun 02 '18 at 21:49
  • Possible duplicate of [How can I find MAX with relational algebra?](https://stackoverflow.com/questions/5493691/how-can-i-find-max-with-relational-algebra) – philipxy Oct 15 '19 at 23:10

2 Answers2

2

Your attempt with the cross-product and price comparison shows you're thinking in the right direction, but not quite there yet. After you get all the books for which cheaper books exist, how do you get the books for which NO cheaper books exist? Working from the inside out, you need to do that before you join to Orderlist, Orders and Customer to find the buyer.

Take care with your syntax and order of operations. As it is, your answer is difficult to make sense of. For example, what are Pd, d, and exp? Why rename something to expensive and then never use that term? How does projection cid (orders) join with Orderlist if the two expressions have no attributes in common? It should help to write out the header of the result of every sub-expression (working from the inside out) to visualize the sequence of operations.

When you want to break a complex RA expression into multiple steps, you can assign the result of one expression to a variable to use in the next expression. Remember, this is algebra just like in grade school, only with more complex values and operators.

reaanb
  • 9,806
  • 2
  • 23
  • 37
0
T1(price)<--Ƴmax(price)(Book)
T2<--Πisbn,title,price(Book natural join T1)
T3<--(T2 natural join orderlist) natural join orders
T4<--πname,title(T3 natural join Customer)
Rachit
  • 17
  • 1