Using
groups
and set functions
in Relational Algebra queries
- Query 1:
- For each department, list the
department name and the
highest salary paid
to an employee in that department
|
- Relations
needed:
- Employee (salary, fname, lname, dno)
- Department (dname, dnumber)
|
- Query plan:
- Combine
the employee and
department information
- Form groups based on
common dname value
- Find the maximum salary value
in each group
|
|
Using
groups
and set functions
in Relational Algebra queries
- Query 1:
- For each department, list the
department name and the
highest salary paid
to an employee in that department
|
- Sample solution:
(with sample input to
make query
concrete)
|
Using
groups
and set functions
in Relational Algebra queries
- Query 1:
- For each department, list the
department name and the
highest salary paid
to an employee in that department
|
- Step 1:
combine (= join) the
input relations:
|
Using
groups
and set functions
in Relational Algebra queries
- Query 1:
- For each department, list the
department name and the
highest salary paid
to an employee in that department
|
- Step 2:
Form groups
with common
dname
and find
max salary in
each group:
|
Query 2:
Find fname and lname of all
employees who work on
more projects
than 'John Smith'
- Relations
needed:
- Works_on
((essn, pno) --- projects
worked on by employee)
- Employee (ssn, fname, lname)
|
- Query plan:
- Find the number of
projects worked on by
John Smith
- Find the number of
projects worked on by
each employee
- Find SSN of employees
who work on more projects
than John Smith
- Find the fname and
lname of these
employees
|
|
Query 2:
Find fname and lname of all
employees who work on
more projects
than 'John Smith'
- Step 1:
Find the
number of projects
worked on by John Smith:
|
Query 2:
Find fname and lname of all
employees who work on
more projects
than 'John Smith'
- Step 2:
Find the
number of projects
worked on by
each
employee:
|
Query 2:
Find fname and lname of all
employees who work on
more projects
than 'John Smith'
- Step 3:
Find the employees
with
more projects
than John Smith:
|
Query 2:
Find fname and lname of all
employees who work on
more projects
than 'John Smith'
- Step 4:
Retrieve the
fname, lname
using
the result in
H3:
|
Query 3:
Find fname and lname of the employees
who have the most number of
dependents
- Relations
needed:
- Dependent
- Employee
|
- Query plan:
- Find the number of
dependents for
each employee
- Find the maximum of
the number of
dependents over all employees
- Find the SSN of
employee(s) who has
the maximum
number of dependents
- Find the fname and
lname of these
employees
|
|
Query 3:
Find fname and lname of the employees
who have the most number of
dependents
- Step 1:
Find the
number of dependents
for
each
employee:
|
Query 3:
Find fname and lname of the employees
who have the most number of
dependents
- Step 2:
Find the
maximum number
of dependents
(over all
employees)
|
Query 3:
Find fname and lname of the employees
who have the most number of
dependents
- Step 3:
Find the
employee (key SSN)
with the
maximum number
of dependents:
|
Query 3:
Find fname and lname of the employees
who have the most number of
dependents
- Step 4:
Retrieve the
fname, lname
using
the result in
H3:
|
❮
❯