Database Management System Ā· Unit 6 Ā· 8 hrs
SQL
Exam-focused notes for SQL (Database Management System, CSC265): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.
SQL in short
This unit covers SQL itself: how to define tables and constraints, how to write retrieval queries from a simple SELECT up through nested subqueries, how to modify data, and how to define a view. SQL uses table, row and column for what the formal relational model calls relation, tuple and attribute, and it is the standard query language of essentially every commercial DBMS (Oracle, MySQL, SQL Server, PostgreSQL among them).
What this unit covers
- Data Definition and Data Types
- Specifying Constraints
- Basic Retrieval Queries
- Complex Retrieval Queries
- INSERT, DELETE, and UPDATE Statements
- Views
Complex Retrieval Queries
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 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]
Show solved answerHide answer
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() |
2080.1 Ā· 10 marks
Consider a banking database with three tables and primary key underlined as given below: Customer(CustomerID, CustomerName, Address, Phone, Email) Borrows(CustomerID, LoanNumber) Loan(LoanNumber, LoanType, Amount) Write both relational algebra and SQL queries: a. To display name of all customers who live in āLalitpurā in ascending order of name. b. To count total number of customers having loan at the bank. c. To find name of those customers who have loan amount greater than or equal to 500000. d. To find average loan amount of each account type.[10]
Specifying Constraints
Basic Retrieval Queries
2078 Ā· 5 marks
Retirve the TName, SName, SPhone for āABCā school using SQL from given relation as below. [5]
Data Definition and Data Types
2076 Ā· 5 marks
Create two table Courses (CID, Course, Dept) and HoD (Dept, Head) using SQL language with all constraints [Primary key, Foreign key and Referential Integrity]. Assume the types of attributes by your own. [5]
Make Unit 6 stick
Practice CSC265 with flashcards & quizzes