0% found this document useful (0 votes)
31 views6 pages

Ex-6 (Bank Database)

The document describes experiments involving using GROUP BY, HAVING, and ORDER BY clauses to aggregate and sort data. It provides the syntax and examples of each clause. The GROUP BY clause groups data by one or more columns and can be used with aggregate functions. The HAVING clause filters groups returned by GROUP BY. The ORDER BY clause sorts the results of a query in ascending or descending order. It also includes an entity relationship diagram and schema for a bank database with tables for branches, accounts, customers, deposits, loans, and borrowers.
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)
31 views6 pages

Ex-6 (Bank Database)

The document describes experiments involving using GROUP BY, HAVING, and ORDER BY clauses to aggregate and sort data. It provides the syntax and examples of each clause. The GROUP BY clause groups data by one or more columns and can be used with aggregate functions. The HAVING clause filters groups returned by GROUP BY. The ORDER BY clause sorts the results of a query in ascending or descending order. It also includes an entity relationship diagram and schema for a bank database with tables for branches, accounts, customers, deposits, loans, and borrowers.
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/ 6

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; //MAJOR = SEC.
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 database bank_database;
Use bank_database;

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


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 C (CUST_NAM CREATE E, LOAN_NO), FOREIGN KEY (CUST_NAME)
REFERENCES CUSTOMER (CUST_NAME) ON DELETE CASCADE, FOREIGN KEY
(LOAN_NO) REFERENCES LOAN (LOAN_NO) ON DELETE CASCADE);

T
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);

You might also like