You are on page 1of 5

Shikhar Shaurya CSE-C 2100320100146

EXPERIMENT 8

AIM -: Implementation of Subqueries/Nested Query


For tables depositor (cust_id, acc_no.), borrower (cust_id, loan_num), customer (cust_id,
cust_name,cust_city), account(acc_no, branch_name,balance) and branch(branch_name,
branch_city, assets)
1. Find customer ids of those customers who are borrower from the banks and who appear in the list of
account holders.
2. Find those customer names who are borrower.
3. Find the name of the customers who have a loan from the bank, but do not have an
account at the bank. (Hint: use NOT IN)
4. Get the Customer Id and name of those customers who have both account and loan
from the bank.
5. Get Branch Name of the branch having highest average balance amongst all branches.

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

Here's what this query does:


We select the distinct cust_id 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 customer IDs
that also appear in the list of account holders. To do this, we use a subquery that selects distinct cust_id
from the depositor table. The result will be a list of customer IDs of those customers who are borrowers
from the banks and also appear in the list of account holders.

Solution 2: To find those customer names who are borrower.


SELECT DISTINCT
C.cust_nameFROM customer
C
INNER JOIN borrower B ON C.cust_id = B.cust_id;
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). The result will be a list of customer names who are borrowers

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

AIM: Implementation of Views & Indexing

For a given set of relation tables perform the following

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:

Drop a Basic View (Without CHECK


OPTION):
Shikhar Shaurya CSE-C 2100320100146

DROP VIEW EmployeeView;


a) To drop a view with the WITH CHECK OPTION, you can use the same DROP VIEW statement. For
example, if you want to delete the "FemaleEmployeeView" view:
Drop a Basic View (With CHECK OPTION):
DROP VIEW FemaleEmployeeView;

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.

b) Select Specific Columns:


To retrieve specific columns from the view, specify the column names in your query:
SELECT First_name, L_name, Salary FROM EmployeeView;
This query will return only the "First_name," "L_name," and "Salary" columns for all employees in the
view.

c) Filtering and Sorting:


You can also apply filters and sorting to your queries. For example:
SELECT First_name, L_name,
SalaryFROM EmployeeView
WHERE Salary >
50000 ORDER BY
Salary DESC;
This query retrieves the "First_name," "L_name," and "Salary" columns for employees with a salary
greater than 50,000 and sorts the results in descending order of salary.

d) Joining with Other Tables:


You can join the view with other tables in your database as needed. For example:
SELECT E.First_name, E.L_name,
D.Dept_NameFROM EmployeeView E
INNER JOIN Department D ON E.Dept_No = D.Dept_No;
This query retrieves the "First_name" and "L_name" of employees along with the "Dept_Name" from a
"Department" table by matching the "Dept_No" column.

You might also like