Write Queries Using Aggregate Functions in Relational Algebra



The aggregate functions are follows −

  • max()

  • min()

  • sum()

  • average()

  • count()

Consider the following tables −

Employee − Emp(empid,ename,salary)

Empid Empname Salary
E1 Ravi 4000
E2 Sanjay 3500
E3 Sruthi 3000
E4 Alok 3000
E5 Pritam 3000

Dept − Dept(deptid,dname)

Depid Dname
D1 Cse
D2 Ece
D3 Civil

Project − Project(projected,pname)

Projectid Pname
P1 Database
P2 networking

Worksin − Worksin(empid,depid)

Empid Depid
E1 P1
E2 P2
E3 P1
E4 P2
E5 P2

Assign − Assign(empid,projectid)

Empid Projectid
E1 P1
E2 P2
E3 P1
E4 P2
E5 P2

Example 1

Display the details of the employee who works in the ece department.

Step 1

Use the below mentioned syntax. Here, T1= deptid of Ece

T1=∏deptid(σdname=ece(dept))

Output

Deptid
D2

Step 2

Here, ?= {T1.deptid=worksin.deptid} and T2= all empid of Ece

T2= ∏empid(T1?worksin)

Output

Empid
E3
E4

Step 3

Here, T3=(T2?emp), ?={T2.empid=emp.empid} and T3= details of all employees of Ece

Empid Empname Salary
E3 Smruthi 3000
E4 Alok 3000

Example 2

Display all names of employees who work on database projects.

Step 1

Use the command mentioned below −

T1=∏projectid(σpname=database(project))

Output

Projectid
P1

Step 2

Use the command given below −

T2= ∏empid(T1?assign)

Output

Empid
E1
E2

Step 3

Use the command given below −

T3= ∏empname(T2?emp)

Output

Empname
Ravi
Smruti
Updated on: 2021-07-06T15:47:50+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements