0% found this document useful (0 votes)
50 views8 pages

R 73

1. Shraddha Ajay Lalbegee submitted an assignment on SQL queries involving designing SQL objects like tables, views, indexes, and sequences and writing 10 SQL queries on a suitable database application using SQL DML statements. 2. The document then shows the output of running various SQL queries on a bank database called 'sbank' to retrieve data from tables like branch, account, loan, borrower, customer, and depositor. 3. Examples of queries run include finding branch names from the loan table, loan numbers for loans over Rs. 12000 from the Camp branch, customers who have loans with their details, and customers with only accounts or both accounts and loans.

Uploaded by

mitalikonde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views8 pages

R 73

1. Shraddha Ajay Lalbegee submitted an assignment on SQL queries involving designing SQL objects like tables, views, indexes, and sequences and writing 10 SQL queries on a suitable database application using SQL DML statements. 2. The document then shows the output of running various SQL queries on a bank database called 'sbank' to retrieve data from tables like branch, account, loan, borrower, customer, and depositor. 3. Examples of queries run include finding branch names from the loan table, loan numbers for loans over Rs. 12000 from the Camp branch, customers who have loans with their details, and customers with only accounts or both accounts and loans.

Uploaded by

mitalikonde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

-------------------------------------------------------------------------------

Name: Shraddha Ajay Lalbegee


Batch: R
Roll no: 73
PRN No: F22123002
Assignment no: A-02
Assignment Title: SQL Queries:
a. Design and Develop SQL DDL statements which demonstrate the use of SQL
objects such as Table, View, Index, Sequence, Synonym, different constraints
etc.
b. Write at least 10 SQL queries on the suitable database application using SQL
DML statements.
-------------------------------------------------------------------------------

C:\Users\LENOVO>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 60
Server version: 8.0.34 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its


affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use sbank;


Database changed
mysql> show tables;
+-----------------+
| Tables_in_sbank |
+-----------------+
| account |
| borrower |
| branch |
| customer |
| depositor |
| loan |
+-----------------+
6 rows in set (0.01 sec)

mysql> describe customer;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| cust_name | varchar(25) | NO | PRI | NULL | |
| cust_street | varchar(50) | YES | | NULL | |
| cust_city | varchar(30) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> describe borrower;


+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| cust_name | varchar(20) | YES | MUL | NULL | |
| loan_no | varchar(30) | YES | MUL | NULL | |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> describe depositor;


+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| cust_name | varchar(20) | YES | MUL | NULL | |
| acc_no | int | YES | MUL | NULL | |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> describe loan;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| loan_no | varchar(30) | NO | PRI | NULL | |
| branch_name | varchar(30) | YES | MUL | NULL | |
| amount | int | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> describe branch;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| branch_name | varchar(20) | NO | PRI | NULL | |
| branch_city | varchar(20) | YES | | NULL | |
| assets | int | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> describe account;


+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| Acc_no | int | NO | PRI | NULL | auto_increment |
| branch_name | varchar(20) | YES | MUL | NULL | |
| balance | int | YES | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> select * from branch;


+---------------+-------------+--------+
| branch_name | branch_city | assets |
+---------------+-------------+--------+
| Andheri | Mumbai | 1000 |
| Bandra | Mumbai | 900 |
| Bund Garden | Pune | 100 |
| Camp | Pune | 2000 |
| Hadapsar | Pune | 3000 |
| Koregaon Park | Pune | 3000 |
| Mira Road | Mumbai | 2000 |
| Pimpri | Pune | 1000 |
| Pimpri Gurav | Pune | 1000 |
| Vadgaon | Pune | 3000 |
+---------------+-------------+--------+
10 rows in set (0.00 sec)

mysql> select * from account;


+--------+---------------+---------+
| Acc_no | branch_name | balance |
+--------+---------------+---------+
| 1 | Bund Garden | 12000 |
| 1234 | Pimpri | 10000 |
| 2379 | Mira Road | 30000 |
| 4321 | Koregaon Park | 4000 |
| 4567 | Camp | 10000 |
| 4590 | Bandra | 8000 |
| 5678 | Pimpri Gurav | 20000 |
| 7654 | Vadgaon | 50000 |
| 8876 | Andheri | 5000 |
| 9876 | Hadapsar | 90000 |
+--------+---------------+---------+
10 rows in set (0.00 sec)

mysql> select * from loan;


+---------+---------------+--------+
| loan_no | branch_name | amount |
+---------+---------------+--------+
| ab12 | Andheri | 10000 |
| cd34 | Bandra | 5000 |
| ef56 | Bund Garden | 20000 |
| gh78 | Hadapsar | 30000 |
| ij90 | Camp | 90000 |
| kl91 | Mira Road | 50000 |
| mn23 | Koregaon Park | 40000 |
| op87 | Vadgaon | 60000 |
| qr43 | Pimpri Gurav | 7000 |
| st56 | Pimpri | 90000 |
+---------+---------------+--------+
10 rows in set (0.00 sec)

mysql> select * from borrower;


+-----------+---------+
| cust_name | loan_no |
+-----------+---------+
| Saniya | st56 |
| Rida | op87 |
| Ashwini | qr43 |
| Muskan | mn23 |
| Simran | kl91 |
| Prachi | ef56 |
| Aishwarya | cd34 |
| Tisha | gh78 |
| Tiya | ij90 |
+-----------+---------+
10 rows in set (0.00 sec)

mysql> select * from customer;


+-----------+---------------+-----------+
| cust_name | cust_street | cust_city |
+-----------+---------------+-----------+
| Aishwarya | Bandra | Mumbai |
| Ashwini | Shivaji Nagar | Pune |
| Muskan | Vishrantwadi | Pune |
| Prachi | Mira Road | Mumbai |
| Rida | Vadgaon | Pune |
| Saniya | Camp | Pune |
| Shraddha | Pimpri Gurav | Pune |
| Simran | Viman Nagar | Pune |
| Tisha | Pimpri | Pune |
| Tiya | Hadapsar | Pune |
+-----------+---------------+-----------+
10 rows in set (0.00 sec)

mysql> select * from depositor;


+-----------+--------+
| cust_name | acc_no |
+-----------+--------+
| Shraddha | 5678 |
| Rida | 7654 |
| Saniya | 9876 |
| Simran | 8876 |
| Prachi | 4590 |
| Ashwini | 1234 |
| Tisha | 4201 |
| Aishwarya | 2379 |
| Tiya | 4567 |
| Muskan | 4321 |
+-----------+--------+
10 rows in set (0.00 sec)

1) Find the names of all branches in loan relation.

mysql> Select DISTINCT branch_name FROM loan;


+---------------+
| branch_name |
+---------------+
| Andheri |
| Bandra |
| Bund Garden |
| Camp |
| Hadapsar |
| Koregaon Park |
| Mira Road |
| Pimpri |
| Pimpri Gurav |
| Vadgaon |
+---------------+
10 rows in set (0.00 sec)

2) Find all loan numbers for loans made at Camp Branch with loan amount > 12000.

mysql> Select loan_no FROM loan WHERE branch_name = 'Camp' AND amount > 1200;
+---------+
| loan_no |
+---------+
| ij90 |
+---------+
1 row in set (0.00 sec)

3) Find all customers who have a loan from bank. Find their names,loan_no and
loan amount.

mysql> Select customer.cust_name, borrower.loan_no, loan.amount FROM borrower


JOIN customer ON borrower.cust_name = customer.cust_name JOIN loan ON
borrower.loan_no = loan.loan_no;
+-----------+---------+--------+
| cust_name | loan_no | amount |
+-----------+---------+--------+
| Shraddha | ab12 | 10000 |
| Saniya | st56 | 90000 |
| Rida | op87 | 60000 |
| Ashwini | qr43 | 7000 |
| Muskan | mn23 | 40000 |
| Simran | kl91 | 50000 |
| Prachi | ef56 | 20000 |
| Aishwarya | cd34 | 5000 |
| Tisha | gh78 | 30000 |
| Tiya | ij90 | 90000 |
+-----------+---------+--------+
10 rows in set (0.00 sec)

4) List all customers in alphabetical order who have loan from Camp branch

mysql> Select customer.cust_name FROM borrower JOIN customer ON


borrower.cust_name = customer.cust_name JOIN loan ON borrower.loan_no =
loan.loan_no WHERE loan.branch_name = 'Camp' ORDER BY customer.cust_name;
+-----------+
| cust_name |
+-----------+
| Tiya |
+-----------+
1 row in set (0.00 sec)

5) Find all customers who have an account or loan or both at bank.

mysql> Select DISTINCT cust_name FROM


(SELECT cust_name FROM depositor UNION Select cust_name FROM borrower)
AS Allcustomers;
+-----------+
| cust_name |
+-----------+
| Aishwarya |
| Ashwini |
| Muskan |
| Prachi |
| Rida |
| Saniya |
| Shraddha |
| Simran |
| Tisha |
| Tiya |
+-----------+
10 rows in set (0.00 sec)

6) Find all customers who have both account and loan at bank.

mysql> Select depositor.cust_name FROM depositor JOIN borrower ON


depositor.cust_name = borrower.cust_name;
+-----------+
| cust_name |
+-----------+
| Aishwarya |
| Ashwini |
| Muskan |
| Prachi |
| Rida |
| Saniya |
| Shraddha |
| Simran |
| Tisha |
| Tiya |
+-----------+
10 rows in set (0.00 sec)

7) Find all customers who have account but no loan at the bank

mysql> Select cust_name FROM depositor WHERE cust_name NOT IN (SELECT cust_name
FROM borrower);
+-----------+
| cust_name |
+-----------+
| Shraddha |
+-----------+
1 row in set (0.00 sec)

8) Find the average account balance at each branc

mysql> Select branch_name, AVG(balance) AS avg_balance FROM account GROUP BY


branch_name;
+---------------+-------------+
| branch_name | avg_balance |
+---------------+-------------+
| Andheri | 5000.0000 |
| Bandra | 8000.0000 |
| Bund Garden | 12000.0000 |
| Camp | 10000.0000 |
| Hadapsar | 90000.0000 |
| Koregaon Park | 4000.0000 |
| Mira Road | 30000.0000 |
| Pimpri | 10000.0000 |
| Pimpri Gurav | 20000.0000 |
| Vadgaon | 50000.0000 |
+---------------+-------------+
10 rows in set (0.00 sec)

9) Find no. of depositors at each branch.

mysql> Select account.branch_name,count(depositor.cust_name) AS No_of_Depositor


FROM account LEFT JOIN depositor ON account.acc_no=depositor.acc_no GROUP BY
account.branch_name;
+---------------+-----------------+
| branch_name | No_of_Depositor |
+---------------+-----------------+
| Andheri | 1 |
| Bandra | 1 |
| Bund Garden | 1 |
| Camp | 1 |
| Hadapsar | 1 |
| Koregaon Park | 1 |
| Mira Road | 1 |
| Pimpri | 1 |
| Pimpri Gurav | 1 |
| Vadgaon | 1 |
+---------------+-----------------+
10 rows in set (0.00 sec)

10) Find name of Customer and city where customer name starts with Letter P.

mysql> Select cust_name,cust_city FROM customer WHERE cust_name LIKE "P%";


+-----------+-----------+
| cust_name | cust_city |
+-----------+-----------+
| Prachi | Mumbai |
+-----------+-----------+
1 row in set (0.00 sec)

11) Display distinct cities of branch

mysql> Select DISTINCT branch_city FROM branch;


+-------------+
| branch_city |
+-------------+
| Mumbai |
| Pune |
+-------------+
2 rows in set (0.00 sec)

12) Find the branches where average account balance > 12000

mysql> Select branch_name FROM account GROUP BY branch_name HAVING AVG(balance)


> 12000;
+--------------+
| branch_name |
+--------------+
| Hadapsar |
| Mira Road |
| Pimpri Gurav |
| Vadgaon |
+--------------+
4 rows in set (0.00 sec)

13) Find number of tuples in customer relation

mysql> Select COUNT(*) FROM customer;


+----------+
| COUNT(*) |
+----------+
| 10 |
+----------+
1 row in set (0.01 sec)

14) Calculate total loan amount given by bank.

mysql> Select SUM(amount) FROM loan;


+-------------+
| SUM(amount) |
+-------------+
| 402000 |
+-------------+
1 row in set (0.00 sec)

15) Delete all loans with loan amount between 1300 and 1500.

mysql> Delete FROM loan WHERE amount BETWEEN 1300 AND 1500;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from loan;


+---------+---------------+--------+
| loan_no | branch_name | amount |
+---------+---------------+--------+
| ab12 | Andheri | 10000 |
| cd34 | Bandra | 5000 |
| ef56 | Bund Garden | 20000 |
| gh78 | Hadapsar | 30000 |
| ij90 | Camp | 90000 |
| kl91 | Mira Road | 50000 |
| mn23 | Koregaon Park | 40000 |
| op87 | Vadgaon | 60000 |
| qr43 | Pimpri Gurav | 7000 |
| st56 | Pimpri | 90000 |
+---------+---------------+--------+
10 rows in set (0.00 sec)

16) Delete all tuples at every branch located in Pimpri.

mysql> Delete FROM branch WHERE branch_city ='Pimpri';


Query OK, 0 rows affected (0.00 sec)

mysql> select * from branch;


+---------------+-------------+--------+
| branch_name | branch_city | assets |
+---------------+-------------+--------+
| Andheri | Mumbai | 1000 |
| Bandra | Mumbai | 900 |
| Bund Garden | Pune | 100 |
| Camp | Pune | 2000 |
| Hadapsar | Pune | 3000 |
| Koregaon Park | Pune | 3000 |
| Mira Road | Mumbai | 2000 |
| Pimpri Gurav | Pune | 1000 |
| Vadgaon | Pune | 3000 |
+---------------+-------------+--------+
9 rows in set (0.00 sec)

You might also like