0

How can I express a sql query in a relational algebra/calculus

SQL

SELECT  * 
  FROM Product a 
  LEFT JOIN Offer b ON a.ProductID = b.ProductID
 WHERE b.ProductID IS NULL

SELECT  * 
  FROM Dealer a 
  JOIN Offer b ON a.DealerID = b.DealerID
  JOIN Product p ON b.ProductID = p.ProductID
 WHERE p.ProductName = Armaniwear
Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75

1 Answers1

0

Have you had a look at this: Relational Algebra Calculator?

Effectively, you just have to create the "relations" (based on your tables) there and it will help with the rest of the things.

So, for instance you first go to Group Editor and create your relations as follows:

group:MyGroup

Products = {
    ProductId:number, ProductName:string
}

Dealer = {
    DealerId:number, DealerName:string
}


Offer = {
    OfferId:number, OfferName:string, DealerId:number, ProductId:number
}

Now, go to the SQL tab and input your query:

SELECT * FROM Dealer
         INNER JOIN Offer 
                 ON (Dealer.DealerId = Offer.DealerId)
         INNER JOIN Products
                 ON (Offer.ProductId = Products.ProductId)  
   WHERE Products.ProductName = 'Armaniwear';


The editor doesn't seem to accept aliases for the tables in the query. However, I believe the trade-off isn't as bad when you get the final result as:

σ Products.ProductName = 'Armaniwear' Dealer
⨝ ( Dealer.DealerId = Offer.DealerId ) Offer 
⨝ ( Offer.ProductId = Products.ProductId ) Products
Dhruv Saxena
  • 1,336
  • 2
  • 12
  • 29
  • So I rewrote all my queries without aliases. However, in some cases I am struggling to get the corresponding relational algebra via the calculator: select ProductName from table_Offer join table_product on table_Offer.ProductID = table_product.ProductID where price > 1000 and table_Offer.productid is not null group by ProductName having count(*) >= 2 –  Jan 25 '17 at 00:12
  • The last part of the SQL "having count (*) >=2" is proving hard to compute: π ProductName π ProductName σ Price > 1000 and table_offer.ProductID ≠ null table_offer ⨝ table_offer.ProductID = table_product.ProductID table_product (Missing having count(*) >= 2) –  Jan 25 '17 at 00:14
  • Another example: SQL select ProductName from table_offer join table_product on table_offer.ProductID = table_product.ProductID where table_offer.ProductID is not null group by ProductName order by count(*) desc –  Jan 25 '17 at 00:19
  • I cant get the calculator to compute "top 100" and "order by count(*) desc" So I left them both out: π ProductName π ProductName σ table_offer.ProductID ≠ null table_offer ⨝ table_offer.ProductID = table_product.ProductID table_product –  Jan 25 '17 at 00:22
  • Finally, If I have used recursion in one of my SQL queries, is it possible to convert that to relational algebra? I have come across recursive relational algebra, not sure they are connected. –  Jan 25 '17 at 00:30
  • @C.Eze Hi, may I ask if you had a chance to look at the comments I made on your post before? I'd found a way to work around `count`. For example, query: `SELECT ProductName, COUNT(ProductName) AS test FROM Offer JOIN Products ON Offer.ProductId = Products.ProductId WHERE Price > 1000 AND Products.ProductId IS NOT NULL GROUP BY ProductName HAVING test >= 2` and therefore.... – Dhruv Saxena Jan 25 '17 at 16:12
  • Relational algebra: `π ProductName, test σ test ≥ 2 γ ProductName; COUNT(ProductName)→test σ Price > 1000 and Products.ProductId ≠ null Offer ⨝ Offer.ProductId = Products.ProductId Products` – Dhruv Saxena Jan 25 '17 at 16:12
  • As for top 100, we add `LIMIT` to the query: `SELECT ProductName, COUNT(ProductName) AS test FROM Offer JOIN Products ON Offer.ProductId = Products.ProductId WHERE Price > 1000 AND Products.ProductId IS NOT NULL GROUP BY ProductName HAVING test >= 2 LIMIT 100`. And so the output produced for relational algebra is: `σ rownum() > 0 and rownum() ≤ 100 π ProductName, test σ test ≥ 2 γ ProductName; COUNT(ProductName)→test σ Price > 1000 and Products.ProductId ≠ null Offer ⨝ Offer.ProductId = Products.ProductId Products` – Dhruv Saxena Jan 25 '17 at 16:16
  • I'm not sure about recursion, but if you may want to share an example or two, we could try and run those through. Cheers! – Dhruv Saxena Jan 25 '17 at 16:17
  • Thanks for these - Really appreciate your assistance. All makes sense now. If I was to express this query in relational calculus, what would it look like: select * from table_product, table_category where table_product.CategoryID = table_category.CategoryID and table_category.CategoryName='electronics' –  Jan 28 '17 at 10:38
  • I'm curious, was this really a difficult query to run on the Relational Algebra Calculator? Did you give it a try? May I ask, what errors did you get and where were you stuck? I didn't create a new table called `Category`, but `Offers` comes pretty close to write something similar and therefore produce the kind of output needed. Query: `SELECT * from Products, Offer where Products.ProductID = Offer.ProductID and Offer.OfferName='electronics' ` and Relational Algebra: `σ Products.ProductId = Offer.ProductId and Offer.OfferName = 'electronics' Products ⨯ Offer` – Dhruv Saxena Jan 28 '17 at 13:56
  • No it wasn't and yes I did give it a try. The calculator is case sensitive for starters. So one could easily make a mistake when typing in sql queries, however, the error message tells you where you are going wrong which is helpful. The examples queries above were not computed by the calculator. It wasn't computing parts of the queries as explained earlier. However, once you rewrote the query. it worked smoothly –  Jan 28 '17 at 17:08
  • As for the recursion example, please see below: ;with cte_a as ( select CategoryID ,CategoryName ,ParentCategoryID from table_category where CategoryID = ParentCategoryID union all select b.CategoryID ,b.CategoryName ,b.ParentCategoryID from cte_a join table_category b on cte_a.CategoryID = b.ParentCategoryID and cte_a.CategoryID <> cte_a.ParentCategoryID ) select * from cte_a –  Jan 28 '17 at 17:14
  • I'm sorry, I don't get it... I didn't make any extensive changes in your previous query. Only changed the table / column names and used the rest of it as it is. Case sensitive bit is easy to spot - like you say, and as long as the relations are defined, all you have to do is come up with a query (of course, it won't compose one for you) and input it into the calculator to produce a Relational Algebraic Expression. The recursion example included in the comment above appears a bit alien according to MySQL syntax. Please have a look at [this link](http://stackoverflow.com/q/16513418/2298301) – Dhruv Saxena Jan 28 '17 at 18:29
  • Once you're sure about the syntax, I'd suggest that you run the query on a computer first to ensure that it produces the desired results. Having done that, you could take the query to the calculator to help with Relational Algebra, while keeping in mind that it won't accept table aliases, although column aliases seem to work fine. – Dhruv Saxena Jan 28 '17 at 18:33