2080 1

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]

Database Management SystemChapter 6: SQLComplex Retrieval Queries

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:

  1. Apply selection (ฯƒ) on Customer table where Address = 'Kathmandu'
  2. 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:

  1. Apply selection (ฯƒ) on Account table where Balance >= 100000
  2. Natural join (โ‹ˆ) the result with Owns on AccountNumber
  3. Natural join (โ‹ˆ) the result with Customer on CustomerID
  4. 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

PartOperation UsedKey Clause
aSelection + ProjectionWHERE Address = 'Kathmandu'
bAggregate (COUNT)COUNT()
cSelection + Join + ProjectionJOIN + WHERE Balance >= 100000
dAggregate (AVG) + GroupingGROUP BY + AVG()

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.