Professional Documents
Culture Documents
EXPERIMENT 8
Pre-requisites:
With the provided tables depositor, borrower, customer, account, and branch, you can write SQL queries to find the
branch name of the branch having the highest average balance among all branches. Here's how you have to structure
these tables:
depositor Table:
(cust_id (Foreign Key referencing cust_id in the customer table), acc_no (Foreign Key referencing acc_no in
the account table))
borrower Table:
(cust_id (Foreign Key referencing cust_id in the customer table), loan_num)
customer Table:
(cust_id (Primary Key), cust_name, cust_city)
account Table:
(acc_no (Primary Key), branch_name (Foreign Key referencing branch_name in the branch table), balance)
branch Table:
(branch_name (Primary Key), branch_city, assets)
Solution 1: To find customer ids of those customers who are borrower from the banks and who appear in
thelist of account holders.
SELECT DISTINCT
C.cust_id FROM
customer C
INNER JOIN borrower B ON C.cust_id =
B.cust_id WHERE C.cust_id IN (
SELECT DISTINCT
D.cust_id FROM
depositor D
);
Shikhar Shaurya CSE-C 2100320100146
Solution 3: To find the name of the customers who have a loan from the bank, but do not have an
account atthe bank. (Hint: use NOT IN)
SELECT DISTINCT
C.cust_name FROM
customer C
INNER JOIN borrower B ON C.cust_id =
B.cust_id WHERE C.cust_id NOT IN (
SELECT DISTINCT
D.cust_id FROM
depositor D
);
In this query:
We select the distinct cust_name from the customer table (aliased as C) who are borrowers (INNER JOIN with the
borrower table). In the WHERE clause, we filter the results to include only those customers whose cust_id is not in
the list of customer IDs that have accounts at the bank. To do this, we use a subquery that selects distinct cust_id
from the depositor table. The result will be a list of customer names who have a loan from the bank but do not
have an account at the bank.
Solution 4: To Get the Customer Id and name of those customers who have both account and loan from
the bank.
SELECT DISTINCT C.cust_id,
C.cust_name FROM customer C
WHERE C.cust_id IN (
SELECT DISTINCT
D.cust_id FROM
depositor D
) AND C.cust_id IN (
SELECT DISTINCT
Shikhar Shaurya CSE-C 2100320100146
B.cust_id FROM
borrower B
);
In this query:We select the distinct cust_id and cust_name from the customer table (aliased as C). In the WHERE
clause, we use two subqueries: The first subquery selects distinct cust_id from the depositor table to find
customers who have an account at the bank. The second subquery selects distinct cust_id from the borrower table
to find customers who have a loan from the bank. We use AND to ensure that a customer's cust_id is present in
both subqueries, indicating that they have both an account and a loan from the bank. The result will be a list of
Customer IDs and names of customers who have both an account and a loan from the bank.
Solution 5: To Get Branch Name of the branch having highest average balance amongst all branches.
SELECT
branch_name
FROM (
SELECT B.branch_name, AVG(A.balance) AS
avg_balance FROM account A
INNER JOIN branch B ON A.branch_name =
B.branch_name GROUP BY B.branch_name
ORDER BY avg_balance
DESC LIMIT 1
) AS branch_with_highest_avg_balance;
In this query:
We start with an inner subquery that joins the account and branch tables, calculates the average balance for each
branch, and then orders the results in descending order of average balance using ORDER BY avg_balance DESC.
We use LIMIT 1 to limit the result to only the first row, which will have the branch with the highest average
balance. Finally, we select the branch_name from the subquery, which represents the branch with the highest
average balance. The result will be the branch name of the branch with the highest average balance among all
branches.
Shikhar Shaurya CSE-C 2100320100146
Experiment: 9
To create views for the "Employee" relation, you can use SQL commands to define the views with and without
using “CHECK OPTION” clause. A view is a virtual table that can be queried like a regular table. Here's an
example of how to create both types of views for the "Employee" relation:
1)
a) Create a Basic View (Without CHECK OPTION):
In this view, we'll create a basic view that allows you to query the "Employee" table without any restrictions.
CREATE VIEW
EmployeeView AS SELECT
* FROM Employee;
This view (EmployeeView) will contain all the data from the "Employee" table, and you can query it like a regular
table.
b) Create a Basic View (With CHECK OPTION):
In this view, we'll create a view with the WITH CHECK OPTION, which enforces that any rows inserted or
updated through the view must meet the specified conditions. For example, let's say you want to create a view
that only includes female employees:
CREATE VIEW
FemaleEmployeeView AS SELECT
* FROM Employee
WHERE Sex = 'F'
WITH CHECK
OPTION;
In this view (FemaleEmployeeView), only female employees will be included, and any attempts to insert or
update rows through this view that do not meet the condition (Sex = 'F') will result in an error. Views provide a
convenient way to present data from the underlying tables while allowing you to control access and enforce data
integrity rules.
2)To drop a view in SQL, you can use the DROP VIEW statement followed by the name of the view you
want toremove. Here's the SQL command to drop views based on your "Employee" relation. To drop a basic
view, use the following SQL command:
3)you have a view named "EmployeeView" that represents the "Employee" relation, you can query it as
follows:
a) Select All Employees:
To retrieve all employees from the view:
SELECT * FROM EmployeeView;
This query will return all columns and all rows from the "EmployeeView" view, effectively showing you all
employee records.