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

DBMS Experiment-6

The document describes experiments with SQL clauses like GROUP BY, HAVING, and ORDER BY. It provides examples of using each clause to aggregate and sort data from tables. The GROUP BY clause groups data by columns. HAVING works with GROUP BY to filter grouped data. ORDER BY sorts the results of a query in ascending or descending order. Bank database tables are created along with sample data to demonstrate queries using these clauses. 25 practice queries on the bank database are provided at the end.

Uploaded by

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

DBMS Experiment-6

The document describes experiments with SQL clauses like GROUP BY, HAVING, and ORDER BY. It provides examples of using each clause to aggregate and sort data from tables. The GROUP BY clause groups data by columns. HAVING works with GROUP BY to filter grouped data. ORDER BY sorts the results of a query in ascending or descending order. Bank database tables are created along with sample data to demonstrate queries using these clauses. 25 practice queries on the bank database are provided at the end.

Uploaded by

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

Experiment No.

6: To study and implement aggregating Data using Group By Clause,


HAVING clause and sort data using Order By clause.
GROUP BY Clause
The GROUP BY clause can be used in a SELECT statement to collect data across multiple
records and group the results by one or more columns.
The syntax for the GROUP BY clause is:
SELECT column1,column2, ... column_n, aggregate_function (expression)
FROM tables
WHERE conditions
GROUP BY column1, column2, ... column_n;
Where
aggregate_function can be a function such as SUM, COUNT, MAX, MIN, AVG etc. 
Example:

1. Display the Average Fees of students department-wise.


SELECT MAJOR, AVG(FEES)
FROM STUDENT
GROUP BY MAJOR;
HAVING Clause
The HAVING clause is used in combination with the GROUP BY clause. It can be used in a
SELECT statement to filter the records that a GROUP BY returns.
The syntax for the HAVING clause is:
SELECT column1, column2, ... column_n, aggregate_function (expression)
FROM tables
WHERE predicates
GROUP BY column1, column2, ... column_n
HAVING condition1 ... condition_n;
1. Display sum of fees of student’s department wise having count more than two for the
same department.
SELECT MAJOR, SUM(FEES)
FROM STUDENT
GROUP BY MAJOR
HAVING COUNT(*)>2;
ORDER BY Clause
ORDER BY clause is used to display the result of a query in a specific order(sorted order).
The sorting can be done in ascending or in descending order. It should be kept in mind that the
actual data in the database is not sorted but only the results of the query are displayed in sorted
order.
Example: SELECT name, city FROM student ORDER BY name;
The above query returns name and city columns of table student sorted by name in
increasing/ascending order.
Example: SELECT * FROM student ORDER BY city DESC;
It displays all the records of table student ordered by city in descending order.
Note:- If order is not specifies that by default the sorting will be performed in ascending
order.

BANK DATABASE
ER DIAGRAM
SCHEMA DIAGRAM:
• BRANCH(branch-name:string, branch-city:string, assets:real)
• ACCOUNT(accno:int, branch-name:string, balance:real)
• DEPOSITOR(customer-name:string, accno:int)
• CUSTOMER(customer-name:string, customer-street:string, customer-city:string)
• LOAN(loan-number:int, branch-name:string, amount:real)
• BORROWER(customer-name:string, loan-number:int)

BRANCH

BNAME CITY ASSETS

DEPOSITOR
ACCOUNT
CNAME ACCNO
ACCNO BNAME BALANCE

CUSTOMER

CNAME CSTREET CCITY

LOAN BORROWER

LNO BNAME AMOUNT CNAME LNO

CREATE ALL THE TABLES

BRANCH TABLE
CREATE TABLE BRANCH (BR_NAME VARCHAR(20) PRIMARY KEY, BR_CITY
VARCHAR(20), ASSETS REAL);

ACCOUNT TABLE
CREATE TABLE ACCOUNT (ACCNO INT PRIMARY KEY, BR_NAME VARCHAR(20),
BALANCE REAL, FOREIGN KEY (BR_NAME) REFERENCES BRANCH (BR_NAME) ON
DELETE CASCADE);

CUSTOMER TABLE
CREATE TABLE CUSTOMER (CUST_NAME VARCHAR(20) PRIMARY KEY,
CUST_STREET VARCHAR (20), CUST_CITY VARCHAR (20));

DEPOSITOR TABLE
CREATE TABLE DEPOSITOR (CUST_NAME VARCHAR (20), ACCNO INT, PRIMARY
KEY (CUST_NAME, ACCNO), FOREIGN KEY (CUST_NAME) REFERENCES
CUSTOMER (CUST_NAME) ON DELETE CASCADE, FOREIGN KEY (ACCNO)
REFERENCES ACCOUNT (ACCNO) ON DELETE CASCADE);

LOAN TABLE
CREATE TABLE LOAN (LOAN_NO INT PRIMARY KEY, BR_NAME VARCHAR (20),
AMOUNT REAL, FOREIGN KEY (BR_NAME) REFERENCES BRANCH (BR_NAME) ON
DELETE CASCADE);

BORROWER TABLE
CREATE TABLE BORROWER (CUST_NAME VARCHAR (20), LOAN_NO INT,
PRIMARY KEY (CUST_NAME, LOAN_NO), FOREIGN KEY (CUST_NAME)
REFERENCES CUSTOMER (CUST_NAME) ON DELETE CASCADE, FOREIGN KEY
(LOAN_NO) REFERENCES LOAN (LOAN_NO) ON DELETE CASCADE);

INSERT INTO TABLES

INSERT INTO BRANCH VALUES


('KORMANGALA’ , ‘BENGALURU’ , 20500.3),
(‘SADASHIVANAGAR’ , ’BENGALURU’ , 154329.5),
(‘VITTALNAGAR’ , ’HYDERABAD’ , 350000),
(‘KASTHURINAGAR’ , ’DELHI’ , 125000),
(‘MARUTINAGAR’ , ’HYDERABAD’ , 212351.6),
(‘RAJANKUNTE’ , ’MUMBAI’ , 53535.8);

INSERT INTO ACCOUNT VALUES


(123456 , ’KORMANGALA’ , 5000),
(123457 , ’SADASHIVANAGAR’ , 35000),
(123458 , ’VITTALNAGAR’ , 60000),
(123459 , ’KASTHURINAGAR’ , 255600),
(123460 , ’VITTALNAGAR’ , 37890),
(123461 , ’MARUTINAGAR’ , 20000),
(123462 , ’SADASHIVANAGAR’ , 40000);

INSERT INTO CUSTOMER VALUES


(‘KAVYA’ , ‘SADASHIVANAGAR’ , ‘BENGALURU’),
(‘ABHAY’ , ‘KAMALANAGAR’ , ‘TUMKUR’),
(‘SHEETAL’ , ‘KASTHURINAGAR’ , ‘BENGALURU’),
(‘KSHAMITHA’ , ‘MARUTILAYOUT’ , ‘TUMKUR’),
(‘LIKITH’ , ‘MADHURANAGAR’ , ‘HYDERABAD’),
(‘SACHIN’ , ‘VITTALNAGAR’ , ‘HYDERABAD’);

INSERT INTO DEPOSITOR VALUES


(‘KAVYA’ , 123457),
(‘ABHAY’ , 123456),
(‘KAVYA’ , 123456),
(‘KSHAMITHA’ , 123458),
(‘KSHAMITHA’ , 123460),
(‘LIKITH’ , 123461),
(‘KAVYA’ , 123462);

INSERT INTO LOAN VALUES


(231 , ’SADASHIVANAGAR’ , 50500.5),
(232 , ’VITTALNAGAR’ , 25000),
(233 , ’MARUTINAGAR’ , 60300.3),
(234 , ’KASTHURINAGAR’ , 45000.7),
(235 , ’KORMANGALA’ , 25534);

INSERT INTO BORROWER VALUES


(‘KAVYA’ , 231),
(‘KSHAMITHA’ , 232),
(‘ABHAY’ , 235),
(‘LIKITH’ , 234),
(‘SACHIN’ , 233);

Write and Execute the SQL Queries for the following statements:

1. Find bank accounts with a balance greater than 20000

SELECT ACCNO,BALANCE
FROM ACCOUNT
WHERE BALANCE>20000;

2. Order results in increasing

SELECT ACCNO,BALANCE
FROM ACCOUNT
WHERE BALANCE>20000
ORDER BY BALANCE DESC;

3. Retrieve a list of all bank branch details, ordered by branch city, with each city’s branches
listed in reverse order of assets.

SELECT BR_NAME, BR_CITY, ASSETS


FROM BRANCH
ORDER BY BR_CITY, ASSETS DESC;

4. Find average balance of accounts at “Sadashivanagar” branch.

SELECT AVG(BALANCE)
FROM ACCOUNT WHERE BR_NAME= 'SADASHIVANAGAR';

5. Find the sum of total account balance of any branch.

SELECT branch_name, SUM(balance) AS total_bal


FROM account GROUP BY branch_name;

Practice Queries based on Bank Database

1. Find bank accounts with a balance greater than 20000


2. Display results in increasing order of balance
3. Retrieve a list of all bank branch details, ordered by branch city, with each city’s branches
listed in reverse order of assets
4. Find average balance of accounts at Sadashivanagar branch
5. Find the number of branches that currently have loans
6. Find the number of branches that currently DONT have loans
7. Find branch names of Bengaluru city
8. Find number of accounts present in each branch
9. Find sum of balance of accounts at each branch
10. Find sum of balance of loan accounts at each branch
11. Find the city of a customer with account number 123456
12. Find branch names without account
13. Find the loan amount borrowed by a customer Abhay
14. Find the branch name and balance of a customer kavya with account number 12345
15. Find the loan amount taken by each customer
16. Display the loan details of a customer Kavya
17. Find the city of branch with loan number 100
18. Find the number of accounts of each customer
19. Find customers with an account but not a loan
20. Find all cities with more than two customers living in the city
21. Find all the customers who have at least two accounts at the main branch.
22. Demonstrate how you delete all account tuples at every branch located in a specific city.
23. Find all the customers who have an account at all the branches located in a specific city.
24. Find all the customers with more than one loan
25. Find branches with assets greater than all branches in Bangalore
Solutions:

25. SELECT branch_name FROM branch


WHERE assets > ALL (
SELECT assets FROM branch
WHERE br_city='bangalore');

24. select Cust_Name,count(Loan_no) from borrower group by Cust_Name


having count(Loan_no)>1;

23. SELECT D.CUST_NAME FROM BRANCH B, ACCOUNT A, DEPOSITOR D


WHERE B.BR_NAME=A.BR_NAME AND A.ACCNO=D.ACCNO
AND B.BR_CITY=‘BANGALORE’
GROUP BY D.CUST_NAME
HAVING COUNT (DISTINCT A.BR_NAME) =
(SELECT COUNT (*) FROM BRANCH
WHERE BR_CITY=‘BANGALORE’);

22.DELETE FROM ACCOUNT WHERE BR_NAME IN


(SELECT BR_NAME
FROM BRANCH
WHERE BR_CITY= ‘HYDERABAD’);

21. SELECT D.CUST_NAME FROM DEPOSITOR D, ACCOUNT A


WHERE A.ACCNO=D.ACCNO AND A.BR_NAME= 'SADASHIVANAGAR'
GROUP BY D.CUST_NAME HAVING COUNT (D.CUST_NAME) >=2;

20. SELECT customer_city, COUNT(*) AS num_customers


FROM customer GROUP BY customer_city
HAVING COUNT(*) > 2;

19. select distinct(d.cust_name) from depositor d, borrower b


where d.cust_name not in(select cust_name from borrower);

18.select count(accno),cust_name from depositor group by cust_name;

17. select br_city from branch b, loan l


where l.br_name=b.br_name and l.loan_no=100;

16. select cust_name,l.loan_no,amount from loan l, borrower b


where cust_name='kavya' and b.loan_no=l.loan_no;

15. select cust_name,amount from loan l,borrower b


where b.loan

You might also like