In this activity, we will practice translating our human questions into queries our robot counterparts can understand. As usual, you will not be required to turn in anything. For your convenience, we have included a short example of how to typeset relational algebra notation in a Jupyter notebook.
You can use TeX-style commands to include mathematical expressions in Jupyter notebook. In a markdown cell, just include the commands you want wrapped in $ to have an inline expression and $$ if you want centered equation typesetting.
$$basic-cust-accts \leftarrow \Pi_{(name, customer.sin, account-number)}
(\sigma_{customer.sin = account.sin}(customer \times account))$$
| Operation | Symbol | LaTeX Name |
|---|---|---|
| Assign | $\rightarrow$ | rightarrow |
| Select | $\sigma$ | sigma |
| Project | $\Pi$ | Pi |
| Inner product | $\bowtie$ | bowtie |
| Cross product | $\times$ | times |
| Rename | $\rho$ | rho |
| Groupby | $\gamma$ | gamma |
| And | $\wedge$ | wedge |
| Or | $\vee$ | vee |
| Negation | $\neg$ | neg |
Consider the following schema below. The key fields are underlined, and the domain is listed after the field names. The Catalog table contains prices for Balloons sold by different Clowns standing at certain booths in a fair.
Clowns(cid integer, cname text, booth text)
Balloons(bid integer, bshape text, bcolor text)
Catalog(cid integer, bid integer, cost float)
SOLUTION: $\Pi_{cost}(\sigma_{bshape='whale'}Balloons \bowtie_{bid=bid} Catalog)$
SOLUTION: $\Pi_{cname}((\Pi_{pid}\sigma_{bcolor='periwinkle'}Balloons)\bowtie Catalog)\bowtie_{cid=cid} Clowns)$
SOLUTION: $\Pi_{cid}Catalog - \Pi_{cid}((\Pi_{cid}Catalog \times \Pi_{bid}\sigma_{bcolor='periwinkle'}Balloons)-Catalog)$
SOLUTION:
SELECT bshape, cost
FROM Clowns, Balloons, Catalog
WHERE Clowns.cid=Catalog.cid
AND Balloons.bid=Catalog.bid
AND cname='Whompers LeFou'
ORDER BY cost DESC
LIMIT 10;
SOLUTION:
SELECT booth, COUNT(DISTINCT bcolor)
FROM Clowns, Balloons, Catalog
WHERE Clowns.cid=Catalog.cid
AND Balloons.bid=Catalog.bid
GROUP BY booth
SOLUTION:
SELECT booth, avg(cost)
FROM Clowns, Balloons, Catalog
WHERE Clowns.cid=Catalog.cid
AND Balloons.cid=Catalog.cid
AND bcolor='red'
GROUP BY booth
HAVING COUNT(DISTINCT bshape)/COUNT(DISTINCT Clowns.cid) > 3
Recall the schema for the representation of a matrix in SQL: matrix(x, y, value). In lecture, someone asked how the matrix transpose would be defined in SQL. Let's think about the implementation. First, we should think about the definition. Let $M$ denote a matrix and $[M]_{ij}$ be the element in the $i^{th}$ row and $j^{th}$ column. The matrix transpose is defined as
$$[M^T]_{ij}=[M]_{ji}$$
This suggests that all we naively have to do is make a SELECT statement that swaps x and y. Hint: $\rho(Mt, (1\rightarrow y, 2 \rightarrow x), M)$
SOLUTION:
SELECT M.y AS x, M.x AS y, value
FROM M;
SQL query goes here...