Relational Algebra Expressions
Qt 1: Employee Database
Schema:
employee(person_name, street, city)
works(person_name, company_name, salary)
company(company_name, city)
a. Find the names of all employees who work for “First Bank Corporation”.
π_person_name(σ_company_name='First Bank Corporation'(works))
- Explanation: Select rows from the
works
relation where thecompany_name
is “First Bank Corporation” and project theperson_name
from the result.
b. Find the names and cities of residence of all employees who work for “First Bank Corporation”.
π_person_name, city(employee ⨝ (works ⨝ σ_company_name='First Bank Corporation'(works)))
- Explanation:
- Perform a selection on
works
to get employees working for “First Bank Corporation”. - Join the result with
employee
to get the city of residence. - Project the
person_name
andcity
.
- Perform a selection on
c. Find the names, street address, and cities of residence of all employees who work for “First Bank Corporation” and earn more than $10,000.
π_person_name, street, city(employee ⨝ (σ_salary>10000(works ⨝ σ_company_name='First Bank Corporation'(works))))
- Explanation:
- Select rows from
works
where thecompany_name
is “First Bank Corporation” andsalary
is greater than $10,000. - Join this result with
employee
to get the street and city of the employees. - Project the
person_name
,street
, andcity
.
- Select rows from
Qt 2: Bank Database
Schema:
branch(branch_name, branch_city, assets)
customer(customer_name, customer_street, customer_city)
loan(loan_number, branch_name, amount)
borrower(customer_name, loan_number)
account(account_number, branch_name, balance)
depositor(customer_name, account_number)
a. Find all loan numbers with a loan value greater than $10,000.
π_loan_number(σ_amount>10000(loan))
- Explanation: Select rows from
loan
where theamount
is greater than $10,000 and project theloan_number
.
b. Find the names of all depositors who have an account with a value greater than $6,000.
π_customer_name(depositor ⨝ (σ_balance>6000(account)))
- Explanation:
- Select rows from
account
where thebalance
is greater than $6,000. - Join this result with
depositor
to get the names of depositors. - Project the
customer_name
.
- Select rows from
c. Find the names of all depositors who have an account with a value greater than $6,000 at the “Uptown” branch.
π_customer_name(depositor ⨝ (σ_balance>6000(account ⨝ σ_branch_name='Uptown'(branch))))
- Explanation:
- Select rows from
account
where thebalance
is greater than $6,000 and thebranch_name
is “Uptown”. - Join this result with
depositor
to get the names of depositors. - Project the
customer_name
.
- Select rows from
In summary:
- Use selection (σ) to filter rows based on conditions.
- Use projection (π) to choose specific columns.
- Use join (⨝) to combine information from multiple tables.