CSC265 ยท 2080 ยท 10 marks
Consider a banking database with three tables and primary keys underlined as given below: Customer(CustomerID, CustomerName, Address, Phone, Email) Owns(CustomerID, AccountNumber) Account(AccountNumber, AccountType, Balance) Write both relational algebra and SQL queries: a. To display name of all cu
Consider a banking database with three tables and primary keys underlined as given below: Customer(CustomerID, CustomerName, Address, Phone, Email) Owns(CustomerID, AccountNumber) Account(AccountNumber, AccountType, Balance) Write both relational algebra and SQL queries: a. To display name of all customers who live in 'Kathmandu'. b. To count total number of customers. c. To find name of those customers who have balance greater than or equal to 100000. d. To find average balance of each account type.[10]
Relational Algebra and SQL Queries for Banking Database
Given Schema
- Customer(<u>CustomerID</u>, CustomerName, Address, Phone, Email)
- Owns(<u>CustomerID</u>, <u>AccountNumber</u>)
- Account(<u>AccountNumber</u>, AccountType, Balance)
a. Display name of all customers who live in 'Kathmandu'
Relational Algebra
ฯ CustomerName (ฯ Address = 'Kathmandu' (Customer))
Steps:
- Apply selection (ฯ) on Customer table where Address = 'Kathmandu'
- Apply projection (ฯ) to retrieve only CustomerName
SQL Query
SELECT CustomerName
FROM Customer
WHERE Address = 'Kathmandu';
b. Count total number of customers
Relational Algebra
โฑ COUNT(CustomerID) (Customer)
Note: Aggregate functions use the aggregate operator โฑ (script F) in relational algebra.
SQL Query
SELECT COUNT(CustomerID) AS TotalCustomers
FROM Customer;
c. Find name of customers who have balance greater than or equal to 100000
Relational Algebra
ฯ CustomerName (Customer โ Owns โ (ฯ Balance >= 100000 (Account)))
Steps:
- Apply selection (ฯ) on Account table where Balance >= 100000
- Natural join (โ) the result with Owns on AccountNumber
- Natural join (โ) the result with Customer on CustomerID
- Apply projection (ฯ) to retrieve only CustomerName
SQL Query
SELECT DISTINCT C.CustomerName
FROM Customer C
JOIN Owns O ON C.CustomerID = O.CustomerID
JOIN Account A ON O.AccountNumber = A.AccountNumber
WHERE A.Balance >= 100000;
DISTINCT is used to avoid duplicate customer names if a customer owns multiple qualifying accounts.
d. Find average balance of each account type
Relational Algebra
AccountType โฑ AVG(Balance) (Account)
The attribute before โฑ denotes the grouping attribute (GROUP BY equivalent).
SQL Query
SELECT AccountType, AVG(Balance) AS AverageBalance
FROM Account
GROUP BY AccountType;
Summary Table
| Part | Operation Used | Key Clause |
|---|---|---|
| a | Selection + Projection | WHERE Address = 'Kathmandu' |
| b | Aggregate (COUNT) | COUNT() |
| c | Selection + Join + Projection | JOIN + WHERE Balance >= 100000 |
| d | Aggregate (AVG) + Grouping | GROUP BY + AVG() |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.