Equivalent of GroupBy and Having clause in Relational Algebra

╄→尐↘猪︶ㄣ 提交于 2019-12-10 21:00:03

问题


I was doing an assignment where I had to convert SQL queries into Relational Algebra queries. I got stuck in converting the group by clause.

Could anyone please tell me how the group by clause can be written in relational algebra?

e.g.:

SELECT job, sal 
  FROM emp
 GROUP BY job
       ;

Thanks!


回答1:


First of all your query is wrong you cannot select something that you did not group unless you use aggregation. I assume you want to get sum of the sal.

job F sum(sal), job(emp).




回答2:


Noting you want to get the sum of salary, in Tutorial D:

SUMMARIZE emp BY { job } ADD ( SUM ( sal ) AS total_sal )

Note aggregation is not a relational operator, hence will not form part of a relational algebra.

As for HAVING, is it a historical anomaly. Before the SQL-92 Standard, it was not possible to write SELECT expressions in the FROM clause (a.k.a derived tables) i.e. you had to do all work in one SELECT expression. Because of SQL's rigid evaluation order, the aggregate value doesn't come into existence after the WHERE clause has been evaluated i.e. it was impossible apply restriction based on aggregated values. HAVING was introduced to address this problem.

But even with HAVING, SQL remained relationally incomplete as regards Codd's until derived tables had been introduced. Derived tables rendered HAVINGredundant but using HAVING is still popular (if Stackoverflow is anything to go by): folk still seem to like to use a single SELECT where possible and SQL's aforementioned rigidity as regards evaluations order (projection is performed last in a SELECT expression) makes derived table usage quite verbose when compared to HAVING.



来源:https://stackoverflow.com/questions/7728012/equivalent-of-groupby-and-having-clause-in-relational-algebra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!