Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DBMS Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

Activity 1:

Database : Student (DDL, DML statements)


Table 1: Student

Name Regno Class Major


Charan 17 1 CS
Divya 8 2 CS

Table 2: Course

CourseName CourseNumber CreditHours Department


DBMS CS1310 4 CS
SQL CS3320 4 CS
Testing ENGG2410 3 ENGG
JAVA CS3380 3 CS

Table 3: Section

SectionIndentifier CourseNumber Year Instructor


85 ENGG2410 98 DSG
92 CS1310 98 Bhairav
102 CS3320 99 HSN
112 ENGG2410 99 Manish
119 CS1310 99 Bhairav
135 CS3380 99 Lavanya

Table 4: Grade_report

Regno Section_identifier Grade


17 112 B
17 119 C
8 85 A
8 92 A
8 102 B
8 135 A

A. Create Tables using create statement


B. Insert rows to individual tables using insert statement
C. Alter table section add new field section and update the records
D. Delete Divya’s grade report
E. Drop the table section

QUERIES
1|Page
a. Create Tables using create statement

Table 1:-

SQL> create table student(name varchar(20),regno varchar(10),class number(2),major varchar(5),primary


key(regno));

Table created.

Table 2:-

SQL> create table course(coursename varchar(30),coursenumber varchar(10),credithours


number(2),department varchar(15),primary key (coursenumber));

Table created.

Table 3:-

SQL> create table section (sectionid number(5),courseno varchar(10),year number(5),instructor


varchar(15),primary key(sectionid),foreign key(courseno) references course(coursenumber));

Table created.

Table 4:-

SQL> create table grade_report(regno varchar(10),section_id number(5),grade varchar(1),foreign key(regno)


references student(regno),foreign key(section_id) references section(sectionid));

Table created.

b. Insert rows to individual tables using insert statement

Table 1:-

SQL> insert into student values ('Charan','17',1,'CS');

1 row created.

SQL> insert into student values ('Divya','8',2,'CS');

1 row created.

SQL> select * from student;

NAME REGNO CLASS MAJOR


-------------------- ---------- --------- -----
Charan 17 1 CS
Divya 8 2 CS
2|Page
Table 2:-

SQL> insert into course values ('intro to computer science','CS1310',4,'CS');

1 row created.

SQL> insert into course values('SQL','CS3320',4,'CS');

1 row created.

SQL> insert into course values('Testing','Engg2410',3,'Engg');

1 row created.

SQL> insert into course values('JAVA','CS3380',3,'CS');

1 row created.

SQL> select * from course;

COURSENAME COURSENUMB CREDITHOURS DEPARTMENT


------------------------------ ---------- ----------- ---------------
DBMS CS1310 4 CS
SQL CS3320 4 CS
Testing Engg2410 3 Engg
JAVA CS3380 3 CS

Table 3:-

SQL> insert into section values(85,'Engg2410',98,'DSG');

1 row created.

SQL> insert into section values(92,'CS1310',98,'Bhairav');

1 row created.

SQL> insert into section values(102,'CS3320',99,'HSN');

1 row created.

SQL> insert into section values(112,'Engg2410',99,'Manish');

1 row created.

SQL> insert into section values(119,'CS1310',99,'Bhairav');

1 row created.
3|Page
SQL> insert into section values(135,'CS3380',99,'Lavanya');

1 row created.

SQL> select * from section;

SECTIONID COURSENO YEAR INSTRUCTOR


--------- ---------- --------- ---------------
85 Engg2410 98 DSG
92 CS1310 98 Bhairav
102 CS3320 99 HSN
112 Engg2410 99 Manish
119 CS1310 99 Bhairav
135 CS3380 99 Lavanya

6 rows selected.

Table 4:-

SQL> insert into grade_report values('17',112,'a');

1 row created.

SQL> insert into grade_report values('17',119,'c');

1 row created.

SQL> insert into grade_report values('8',85,'a');

1 row created.

SQL> insert into grade_report values('8',92,'a');

1 row created.

SQL> insert into grade_report values('8',102,'b');

1 row created.

SQL> insert into grade_report values('8',135,'a');

1 row created.

SQL> select * from grade_report;

REGNO SECTION_ID GRADE


---------- --------------- ---------
17 112 a
17 119 c
4|Page
8 85 a
8 92 a
8 102 b
8 135 a

6 rows selected.

c. Alter table section add new field section and update the records

Update:-

SQL> desc section;

Name Null? Type


------------------------------- -------- ----
SECTIONID NOT NULL NUMBER(5)
COURSENO VARCHAR2(10)
YEAR NUMBER(5)
INSTRUCTOR VARCHAR2(15)

SQL> alter table section add section varchar(2);

Table altered.

SQL> desc section;

Name Null? Type


------------------------------- -------- ----
SECTIONID NOT NULL NUMBER(5)
COURSENO VARCHAR2(10)
YEAR NUMBER(5)
INSTRUCTOR VARCHAR2(15)
SECTION VARCHAR2(2)

Update :-

SQL> update section set section='a' where sectionid=85;

1 row updated.

SQL> update section set section='b'where sectionid=92;

1 row updated.

SQL> update section set section='a' where sectionid=102;

1 row updated.

SQL> update section set section='b' where sectionid=112;

5|Page
1 row updated.

SQL> update section set section='a' where sectionid=119;

1 row updated.

SQL> update section set section='a' where sectionid=135;

1 row updated.

SQL> select * from section;

SECTIONID COURSENO YEAR INSTRUCTOR SE


--------- ---------- --------- --------------- --
85 Engg2410 98 DSG a
92 CS1310 98 Bhairav b
102 CS3320 99 HSN a
112 Engg2410 99 Manish b
119 CS1310 99 Bhairav a
135 CS3380 99 Lavanya a

6 rows selected.

d. Delete Divya’s grade report

SQL> select * from grade_report;

REGNO SECTION_ID GRADE


---------- ----------- -------
17 112 a
17 119 c
8 85 a
8 92 a
8 102 b
8 135 a

6 rows selected.

SQL> delete grade_report where regno=8;

4 rows deleted.

SQL> select * from grade_report;

REGNO SECTION_ID GRADE


---------- ---------- --------
17 112 a
17 119 c

6|Page
e. Drop the table section

SQL> drop table section cascade constraints;

Table dropped.

7|Page
Activity 2: (Select clause, Arithmetic Operators)
Database: employee
Create Following tables and insert tuples with suitable constraints

EMPLOYEE
EMPID FIRSTANAME LASTNAME Hire_Date ADDRESS CITY
1001 Charan Kumar 11-May-06 83 first street California
1002 Abhi Shek 25-Feb-08 842 Vine Ave Bengaluru
1012 Meher Khanam 12-Sep-05 33 Elm St. California
1015 Naveen Varma 19-Dec-06 11 Red Road California
1016 Pavan Reddy 22-Aug-07 440 MG Road New Delhi
1020 Yogeesh Achar 07-Jun-08 9 Bandra Mumbai

EMPSALARY
EMPID SALARY BENEFITS DESIGNATION
1001 10000 3000 CEO
1002 8000 1200 COO
1012 20000 5000 CFO
1015 6500 1300 Manager
1016 6000 1000 Manager
1020 8000 1200 COO

Write queries for the following


1. To display FIRSTNAME, LASTNAME, ADDRESS AND CITY of all employees living in CALIFORNIA.
2. To display the content of employee table in descending order of FIRSTNAME
3. Select FIRSTNAME and SALARY of COO
4. To display the FIRSTNAME,LASTNAME, AND TOTAL SALARY of all employees from the table EMPLOYEE and
EMPSALARY. Where TOTAL SALARY is calculated as SALARY+BENEFITS
5. List the Names of employees, who are more than 1 year old in the organization
6. Count number of distinct DESINGATION from EMPSALARY
7. List the employees whose names have exactly 6 characters
8. Add new column PHONE_NO to EMPLOYEE and update the records
9. List employee names, who have joined before 15-Jun-08 and after 16-Jun-07
10. Generate Salary slip with Name, Salary, Benefits, HRA-50%, DA-30%, PF-12%, Calculate gross. Order the
result in descending order of the gross.

QUERIES

To create table “Employee”:

SQL> create table Employee(empid number(10), firstname varchar(20), lastname varchar(20), Hire_date date,
address varchar(20), city varchar(20), priAbhi key(empid));
Table created
8|Page
To create table “Empsalary”:
SQL> create table Empsalary(eid number(10), salary number, benefits number, designation varchar(20), foreign
key(eid) references Employee(empid));
Table created.

To insert values into the table “Employee”:


SQL> insert into Employee values(1001,'Charan','Kumar','11-May-06','83 first street', 'California');
1 row created.

SQL> insert into Employee values(1002,'Abhi', 'Shek', '25-Feb-08','842 Vine Avenue', 'Bengaluru');
1 row created.

SQL> insert into Employee values(1012, 'Meher','Khanam','12-Sep-05','33 Elm St.','California');


1 row created.

SQL> insert into Employee values(1015, 'Naveen','Varma','19-Dec-06','11 Red Road', 'California');


1 row created.

SQL> insert into Employee values(1016, 'Pavan', 'Reddy','22-Aug-07','440 MG Road','New Delhi');


1 row created.

SQL> insert into Employee values(1020, 'Yogeesh', 'Achar','07-Jun-08','9 Bandra','Mumbai');


1 row created.

To insert values into the table “Empsalary”:

SQL> insert into Empsalary values(&eid,&salary,&benefits,'&designation');


Enter value for eid: 1001
Enter value for salary: 10000
Enter value for benefits: 3000
Enter value for designation: CEO
1 row created.

SQL> /
Enter value for eid: 1002
Enter value for salary: 8000
Enter value for benefits: 1200
Enter value for designation: COO
1 row created.

SQL>/
Enter value for eid: 1012
Enter value for salary: 20000
9|Page
Enter value for benefits: 5000
Enter value for designation: CFO
1 row created.

SQL> /
Enter value for eid: 1015
Enter value for salary: 6500
Enter value for benefits: 1300
Enter value for designation: Manager
1 row created.

SQL> /
Enter value for eid: 1016
Enter value for salary: 6000
Enter value for benefits: 1000
Enter value for designation: Manager
1 row created.

SQL> /
Enter value for eid: 1020
Enter value for salary: 8000
Enter value for benefits: 1200
Enter value for designation: COO
1 row created.

SQL> select * from employee;

EMPID FIRSTNAME LASTNAME HIRE_DATE ADDRESS CITY


------------ ------------- ------------------ ---------------- ------------- ----------
1001 Charan Kumar 11-MAY-06 83 first street California
1002 Abhi Shek 25-FEB-08 842 Vine Avenue Bengaluru
1012 Meher Khanam 12-SEP-05 33 Elm St. California
1015 Naveen Varma 19-DEC-06 11 Red Road California
1016 Pavan Reddy 22-AUG-07 440 MG Road New Delhi
1020 Yogeesh Achar 07-JUN-08 9 Bandra Mumbai

6 rows selected.

SQL> select * from empsalary;


EID SALARY BENEFITS DESIGNATION
--------- --------- --------- - -------------------
1001 10000 3000 CEO
1002 8000 1200 COO
1012 20000 5000 CFO
1015 6500 1300 Manager

10 | P a g e
1016 6000 1000 Manager
1020 8000 1200 COO

6 rows selected.

Queries:-

1. To display FIRSTNAME,LASTNAME,ADDRESS and CITY of all employees


living in CALIFORNIA.

SQL> select firstname,lastname,address,city from employee where city='California';

FIRSTNAME LASTNAME ADDRESS CITY


-------------------- -------------------- -------------------- --------------------
Charan Kumar 83 first street California
Meher Khanam 33 Elm St. California
Naveen Varma 11 Red Road California

2. To display the content of employee table in descending order of FIRSTNAME.


SQL> select * from employee order by firstname desc;

MPID FIRSTNAME LASTNAME HIRE_DATE ADDRESS CITY


--------- --------- ----------- -------------------- --------- --------------------
1016 Pavan Reddy 22-AUG-07 440 MG Road New Delhi
1012 Meher Khanam 12-SEP-05 33 Elm St. California
1015 Naveen Varma 19-DEC-06 11 Red Road California
1020 Yogeesh Achar 07-JUN-08 9 Bandra Mumbai
1002 Abhi Shek 25-FEB-08 842Vine Avenue Bengaluru
1001 Charan Kumar 11-MAY-06 83 first street California

3. Select FIRSTNAME and SALARY of COO;

SQL> select firstname,salary from employee,empsalary where employee.empid=empsalary.eid and


designation='COO';

FIRSTNAME SALARY
-------------------- ---------
Abhi 8000
Yogeesh 8000

4. To display the FIRSTNAME,LASTNAME and TOTALSALARY of all employees from the table EMPLOYEE
and EMPSALARY. Wher total salary is calculated as SALARY+BENEFITS.

SQL> select firstname,lastname,(salary+benefits) as totalsalary from employee,empsalary where


employee.empid=empsalary.eid;

11 | P a g e
FIRSTNAME LASTNAME TOTALSALARY
-------------------- -------------------- -----------
Charan Kumar 13000
Abhi Shek 9200
Meher Khanam 25000
Naveen Varma 7800
Pavan Reddy 7000
Yogeesh Achar 9200

6 rows selected.

4. List the names of employees, who are more than 1 year old in the organization.

SQL> select firstname from employee where months_between(sysdate,hire_date)>12;

FIRSTNAME
--------------------
Charan
Abhi
Meher
Naveen
Pavan
Yogeesh

6 rows selected.

6. Count number of distinct DESIGNATION from EMPSALARY;

SQL> select count(distinct designation) as no_of_designation from empsalary;

NO_OF_DESIGNATION
-----------------
4

7.List the employees whose names have exactly 6 characters.

SQL> select firstname from employee where length(firstname)=6;

FIRSTNAME
-------------------
Charan
Pavan
Yogeesh

8. Add new column PHONE_NO to EMPLOYEE and update the records.

12 | P a g e
SQL> alter table employee add ph_no varchar(10);

Table altered.

SQL> desc employee;

Name Null? Type


--------------- ------------ ------------
EMPID NOT NULL NUMBER
FIRSTNAME VARCHAR2(20)
LASTNAME VARCHAR2(20)
HIRE_DATE DATE
ADDRESS VARCHAR2(20)
CITY VARCHAR2(20)
PH_NO VARCHAR2(10)

SQL> update employee set ph_no=+1 2356 where empid=1001;

1 row updated.

SQL> update employee set ph_no=9986625485 where empid=1002;

1 row updated.

SQL> update employee set ph_no=9741737354 where empid=1012;

1 row updated.

SQL> update employee set ph_no=9481067721 where empid=1015;

1 row updated.

SQL> update employee set ph_no=9345785423 where empid=1016;

1 row updated.

SQL> update employee set ph_no=8775666345 where empid=1020;

1 row updated.

SQL> select * from employee;

EMPID FIRSTNAME LASTNAME HIRE_DATE ADDRESS CITY PH_NO


--------- ---------------- ------------------ ------------------ --------------- ---------- ----------
1001 Charan Kumar 11-MAY-06 83 first street California +1 2356
1002 Abhi Shek 25-FEB-08 842 Vine Avenue Bengaluru 986625
1012 Meher Khanam 12-SEP-05 33 Elm St. California 737354
1015 Naveen Varma 19-DEC-06 11 Red Road California 994810
1016 Pavan Reddy 22-AUG-07 440 MG RoadNew Delhi 693457
13 | P a g e
1020 Yogeesh Achar 07-JUN-08 9 Bandra Mumbai 996345

6 rows selected.

9. List employee names,who have joined before 15-jun-08 and 16-jun-07.

SQL> select firstname from employee where hire_date between '16-jun-07' and '15-jun-08';

FIRSTNAME
--------------------
Abhi
Pavan
Yogeesh

10. Generate salary slip with FIRSTNAME,SALARY,BENEFITS,HRA-50%,DA-30%,PF-12%,Calculate GROSS order


the result in descending order of the GROSS.

SQL> select firstname,salary,benefits,salary*0.5 hra,salary*0.3 da,salary*0.12 pf,salary+salary*0.5+salary*0.3-


salary*0.12 as gross from employee,empsalary where employee.empid=empsalary.eid order by gross desc;

FIRSTNAME SALARY BENEFITS HRA DA PF GROSS


-------------------- --------- --------- --------- --------- --------- ---------
Meher 20000 5000 10000 6000 2400 33600
Charan 10000 3000 5000 3000 1200 16800
Abhi 8000 1200 4000 2400 960 13440
Yogeesh 8000 1200 4000 2400 960 13440
Naveen 6500 1300 3250 1950 780 10920
Pavan 6000 1000 3000 1800 720 10080

6 rows selected.

14 | P a g e
Activity 3: (Logical, Relational Operators)
Database: Library
Create Following tables and insert tuples with suitable constraints

Table: Books

Book_Id Book_name Author_Name Publishers Price Type Quantity


C0001 The Klone and I Lata Kappor EPP 355 Novel 5
F0001 The Tears William Hopkins First Publ 650 Fiction 20
T0001 My First C++ Brain & Brooke ERP 350 Text 10
T0002 C++ Brainworks A.W.Rossaine TDH 350 Text 15
F0002 Thunderbolts Ana Roberts First Publ. 750 Fiction 50

Table : Issued

Book_Id Quantity_Issued
T0001 4
C0001 5
F0001 2
T0002 5
F0002 8

Write queries for the following


1. To show Book name, Author name and price of books of First Publ. publisher
2. Display Book id, Book name and publisher of books having quantity more than 8 and price less than 500
3. Select Book id, book name, author name of books which is published by other than ERP publishers and
price between 300 to 700
4. Generate a Bill with Book_id, Book_name, Publisher, Price, Quantity, 4% of VAT “Total”
5. Display book details with book id’s C0001, F0001, T0002, F0002 (Hint: use IN operator)
6. Display Book list other than, type Novel and Fiction
7. Display book details with author name starts with letter ‘A’
8. Display book details with author name starts with letter ‘T’ and ends with ‘S’
9. Select BookId, BookName, Author Name , Quantity Issued where Books.BooksId = Issued.BookId
10. List the book_name, Author_name, Price. In ascending order of Book_name and then on descending
order of price

Create table :-

Table 1:-

SQL> create table books (book_id varchar(10), book_name varchar(15), author_name varchar(15), publishers
varchar(10),price number, type varchar(10), quantity number, primary key(book_id));

Table created.

15 | P a g e
Table 2:-

SQL> create table issued(b_id varchar(10), Qty_issued number, foreign key(b_id) references books(book_id));

Table created.

Insert values into tables:-

Table 1:-

SQL> insert into books values('&book_id','&book_name', '&author_name','&publishers', &price,


'&type',&quantity);

Enter value for book_id: C0001


Enter value for book_name: The Klone and I
Enter value for author_name: Lata Kappor
Enter value for publishers: EPP
Enter value for price: 355
Enter value for type: Novel
Enter value for quantity: 5

1 row created.

SQL> /
Enter value for book_id: F0001
Enter value for book_name: The Tears
Enter value for author_name: William Hopkins
Enter value for publishers: First Publ
Enter value for price: 650
Enter value for type: Fiction
Enter value for quantity: 20

1 row created.

SQL> /
Enter value for book_id: T0001
Enter value for book_name: My First C++
Enter value for author_name: Brain & Brooke
Enter value for publishers: ERP
Enter value for price: 350
Enter value for type: Text
Enter value for quantity: 10

16 | P a g e
1 row created.

SQL> /
Enter value for book_id: T0002
Enter value for book_name: C++ Brainworks
Enter value for author_name: A.W.Rossaine
Enter value for publishers: TDH
Enter value for price: 350
Enter value for type: Text
Enter value for quantity: 15

1 row created.

SQL> /
Enter value for book_id: F0002
Enter value for book_name: Thunderbolts
Enter value for author_name: Ana Roberts
Enter value for publishers: First Publ
Enter value for price: 750
Enter value for type: Fiction
Enter value for quantity: 50

1 row created.

Table 2:-

SQL> insert into issued values('&b_id',&qty_issued);


Enter value for b_id: T0001
Enter value for qty_issued: 4

1 row created.

SQL> /
Enter value for b_id: C0001
Enter value for qty_issued: 5

1 row created.

SQL> /
Enter value for b_id: F0001
Enter value for qty_issued: 2
1 row created.

SQL> /
17 | P a g e
Enter value for b_id: T0002
Enter value for qty_issued: 5

1 row created.

SQL> /
Enter value for b_id: F0002
Enter value for qty_issued: 8

1 row created.

SQL> select * from books;

BOOK_ID BOOK_NAME AUTHOR_NAME PUBLISHERS PRICE TYPE QUANTITY


---------- --------------- --------------- ---------- --------- ---------- -------------------------------------------
C0001 The Klone and I Lata Kappor EPP 355 Novel 5
F0001 The Tears William Hopkins First Publ 650 Fiction 20
T0001 My First C++ Brain & Brooke ERP 350 Text 10
T0002 C++ Brainworks A.W.Rossaine TDH 350 Text 15
F0002 Thunderbolts Ana Roberts First Publ 750 Fiction 50

SQL> select * from issued;

B_ID QTY_ISSUED
---------- ----------
T0001 4
C0001 5
F0001 2
T0002 5
F0002 8

Queries:-

1. To show Book name, Author name and price of books of First Publ. publisher

SQL> select book_name,author_name,price from books where publishers='First Publ';

BOOK_NAME AUTHOR_NAME PRICE


--------------- --------------- -------------------------------------------
The Tears William Hopkins 650
Thunderbolts Ana Roberts 750

18 | P a g e
2. Display Book id, Book name and publisher of books having quantity more than 8 and price less than
500.

SQL> select book_id,book_name,publishers from books where quantity>8 and price<500;

BOOK_ID BOOK_NAME PUBLISHERS


---------- --------------- ---------------------------------------
T0001 My First C++ ERP
T0002 C++ Brainworks TDH

3. Select Book id, book name, author name of books which is published by other than ERP publishers and
price between 300 to 700.

SQL> select book_id,book_name,author_name from books where publishers<>'ERP' and (price between 300 and
700);

BOOK_ID BOOK_NAME AUTHOR_NAME


---------- --------------- ----------------------------------------
C0001 The Klone and I Lata Kappor
F0001 The Tears William Hopkins
T0002 C++ Brainworks A.W.Rossaine

4. Generate a Bill with Book_id, Book_name, Publisher, Price, Quantity, 4% of VAT “Total”.

SQL> select book_id,book_name,publishers,price,quantity,price+price*0.04 as total from books,issued where


books.book_id=issued.b_id;

BOOK_ID BOOK_NAME PUBLISHERS PRICE QUANTITY TOTAL


---------- --------------- ---------- --------- --------- ----------------------------------------------------------------
T0001 My First C++ ERP 350 10 364
C0001 The Klone and I EPP 355 5 369.2
F0001 The Tears First Publ 650 20 676
T0002 C++ Brainworks TDH 350 15 364
F0002 Thunderbolts First Publ 750 50 780

5. Display book details with book id’s C0001, F0001, T0002, F0002 (Hint: use IN operator)

SQL> select * from books where book_id in ('C0001','F0001','T0002','F0002');

BOOK_ID BOOK_NAME AUTHOR_NAME PUBLISHERS PRICE TYPE QUANTITY


---------- --------------- --------------- ---------- --------- ---------- ------------------------------------------
F0002 Thunderbolts Ana Roberts First Publ 750 Fiction 50

19 | P a g e
T0002 C++ Brainworks A.W.Rossaine TDH 350 Text 15
F0001 The Tears William Hopkins First Publ 650 Fiction 20
C0001 The Klone and I Lata Kappor EPP 355 Novel 5

6. Display Book list other than, type Novel and Fiction.

SQL> select book_name from books where type<>'novel' and type<>'fiction';

BOOK_NAME
---------------
The Klone and I
The Tears
My First C++
C++ Brainworks
Thunderbolts

7. Display book details with author name starts with letter ‘A’.

SQL> select * from books where author_name like 'A%';

BOOK_ID BOOK_NAME AUTHOR_NAME PUBLISHERS PRICE TYPE QUANTITY


---------- --------------- --------------- ---------- --------- ---------- ------------------------ --------------
T0002 C++ Brainworks A.W.Rossaine TDH 350 Text 15
F0002 Thunderbolts Ana Roberts First Publ 750 Fiction 50

8. Display book details with author name starts with letter ‘T’ and ends with ‘S’.

SQL> select * from books where author_name like 'T%S';


no rows selected

9. Select BookId, BookName, Author Name , Quantity Issued where Books.BooksId = Issued.BookId.

SQL> select book_id,book_name,author_name,qty_issued from books,issued where books.book_id=issued.b_id;

BOOK_ID BOOK_NAME AUTHOR_NAME QTY_ISSUED


---------- --------------- --------------- --------------------- --------------------
T0001 My First C++ Brain & Brooke 4
C0001 The Klone and I Lata Kappor 5
F0001 The Tears William Hopkins 2
T0002 C++ Brainworks A.W.Rossaine 5
F0002 Thunderbolts Ana Roberts 8

10. List the book_name, Author_name, Price. In ascending order of Book_name and then on descending
order of price.

20 | P a g e
SQL> select book_name,author_name,price from books order by book_name asc , price desc;

BOOK_NAME AUTHOR_NAME PRICE


--------------- --------------- ------------------------------
C++ Brainworks A.W.Rossaine 350
My First C++ Brain & Brooke 350
The Klone and I Lata Kappor 355
The Tears William Hopkins 650
Thunderbolts Ana Roberts 750

21 | P a g e
Activity 4: (Date Functions)
Database : Lab
Create Following table and insert tuples with suitable constraints

Table : Equipment_Details
No ItemName Costperitem Quantity Dateofpurchase Warranty Operational
1 Computer 30000 9 21/5/07 2 7
2 Printer 5000 3 21/5/06 4 2
3 Scanner 8000 1 29/8/08 3 1
4 Camera 7000 2 13/6/05 1 2
5 UPS 15000 5 21/5/08 1 4
6 Hub 8000 1 31/10/08 2 1
7 Plotter 25000 2 11/1/09 2 2

(Use date functions and aggregate functions)

1. To select the ItemName purchase after 31/10/07


2. Extend the warrenty of each item by 6 months
3. Display Itemname , Dateof purchase and number of months between purchase date and present date
4. To list the ItemName in ascending order of the date of purchase where quantity is more than 3.
5. To count the number, average of costperitem of items purchased before 1/1/08
6. To display the minimum warranty , maximum warrenty period
7. To Display the day of the date , month , year of purchase in characters
8. To round of the warranty period to month and year format.
9. To display the next Sunday from the date ’07-JUN-96’
10. To list the ItemName, which are within the warranty period till present date

Create table:-
SQL> create table equip_details(no number,itemname varchar(10),costperitem number,quantity
number,d_o_purchase date,warranty number,operational number,primary key(no));
Table created.

Insert table:-
SQL> insert into equip_details values(&no,'&itemname',&costperitem,&quantity,
'&d_o_purchase',&warranty,&operational);

Enter value for no: 1


Enter value for itemname: computer
Enter value for costperitem: 30000
Enter value for quantity: 9
Enter value for d_o_purchase: 21-may-07
Enter value for wqrranty: 2
Enter value for operational: 7
1 row created.

SQL> /

22 | P a g e
Enter value for no: 2
Enter value for itemname: printer
Enter value for costperitem: 5000
Enter value for quantity: 3
Enter value for d_o_purchase: 21-may-06
Enter value for wqrranty: 4
Enter value for operational: 2

1 row created.

SQL> /
Enter value for no: 3
Enter value for itemname: scanner
Enter value for costperitem: 8000
Enter value for quantity: 1
Enter value for d_o_purchase: 29-aug-08
Enter value for wqrranty: 3
Enter value for operational: 1

1 row created.

SQL> /
Enter value for no: 4
Enter value for itemname: camera
Enter value for costperitem: 7000
Enter value for quantity: 2
Enter value for d_o_purchase: 13-jun-05
Enter value for wqrranty: 1
Enter value for operational: 2

1 row created.

SQL> /
Enter value for no: 5
Enter value for itemname: ups
Enter value for costperitem: 15000
Enter value for quantity: 5
Enter value for d_o_purchase: 21-may-08
Enter value for wqrranty: 1
Enter value for operational: 4

1 row created.

SQL> /
Enter value for no: 6
Enter value for itemname: hub
Enter value for costperitem: 8000
Enter value for quantity: 1
Enter value for d_o_purchase: 31-oct-08
Enter value for wqrranty: 2
23 | P a g e
Enter value for operational: 1

1 row created.

SQL> /
Enter value for no: 7
Enter value for itemname: plotter
Enter value for costperitem: 25000
Enter value for quantity: 2
Enter value for d_o_purchase: 11-jan-09
Enter value for wqrranty: 2
Enter value for operational: 2

1 row created.

SQL> select * from equip_details;

NO ITEMNAME COSTPERITEM D_O_PURCH WARRAN OPERATIONAL QUANTIT


--------- --------------- ----------- --------- --------- ----------- ------------------------------------------------
1 Computer 30000 21-MAY-07 2 7 9
2 Printer 5000 21-MAY-06 4 2 3
3 Scanner 8000 29-AUG-08 3 1 1
4 Camera 7000 13-JUN-05 1 2 2
5 UPS 15000 21-MAY-08 1 4 5
6 Hub 8000 31-OCT-08 2 1 1
7 Plotter 25000 11-JAN-09 2 2 2

7 rows selected.

Queries :-

1. To select the ItemName purchase after 31/10/07.

SQL> select itemname from equip_details where d_o_purchase > '31-oct-07';

ITEMNAME
----------
scanner
ups
hub
plotter

2. Extend the warrenty of each item by 6 months

SQL> update equip_details set warranty=warranty+0.5;

7 rows updated.

SQL> select * from equip_details;

24 | P a g e
NO ITEMNAME COSTPERITEM QUANTITY D_O_PURCH WARRANTY OPERATION
--------- ---------- ----------- --------- --------- --------- -----------------------------------------------------
1 computer 30000 9 21-MAY-07 2.5 7
2 printer 5000 3 21-MAY-06 4.5 2
3 scanner 8000 1 29-AUG-08 3.5 1
4 camera 7000 2 13-JUN-05 1.5 2
5 ups 15000 5 21-MAY-08 1.5 4
6 hub 8000 1 31-OCT-08 2.5 1
7 plotter 25000 2 11-JAN-09 2.5 2

7 rows selected.
3. Display Itemname , Dateofpurchase and number of months between purchase date and present date.

SQL> select itemname, D_O_Purchase, round(months_between(sysdate,D_O_Purchase)) as no_of_months from


equip_details;

ITEMNAME D_O_PURCH NO_OF_MONTHS


---------- --------- ------------
computer 21-MAY-07 93
printer 21-MAY-06 105
scanner 29-AUG-08 78
camera 13-JUN-05 117
ups 21-MAY-08 81
hub 31-OCT-08 76
plotter 11-JAN-09 74

7 rows selected.

4. To list the ItemName in ascending order of the date of purchase where quantity is more than 3.

SQL> select itemname from equip_details where quantity > 3 order by D_O_Purchase;

ITEMNAME
----------
computer
ups

5. To count the number, average of costperitem of items purchased before 1/1/08

SQL> select count(*) as record_count, avg(costperitem) as Average from equip_details where


D_O_Purchase<'01-jan-08';

RECORD_COUNT AVERAGE
------------------------- ----------------
3 14000

6. To display the minimum warranty , maximum warrenty period

SQL> select min(warranty), max(warranty) from equip_details;

25 | P a g e
MIN(WARRANTY) MAX(WARRANTY)
-------------------------- --------------------------
1.5 4.5

7. To Display the day of the date , month , year of purchase in characters

SQL> SELECT TO_CHAR(D_o_purchase, 'fmDaY DD fmMonth YYYY') as Date_in_Characters from equip_details;

DATE_IN_CHARACTERS
---------------------------
Monday 21 May 2007
Sunday 21 May 2006
Friday 29 August 2008
Monday 13 June 2005
Wednesday 21 May 2008
Friday 31 October 2008
Sunday 11 January 2009

7 rows selected.

8. To round of the date_of_purchase period to month and year format.

SQL> select D_O_PURCHASE, round(D_O_PURCHASE,'month') month ,round(D_O_PURCHASE,'year') year from


equip_details;

D_O_PURCH MONTH YEAR


--------- --------- ---------
21-MAY-07 01-JUN-07 01-JAN-07
21-MAY-06 01-JUN-06 01-JAN-06
29-AUG-08 01-SEP-08 01-JAN-09
13-JUN-05 01-JUN-05 01-JAN-05
21-MAY-08 01-JUN-08 01-JAN-08
31-OCT-08 01-NOV-08 01-JAN-09
11-JAN-09 01-JAN-09 01-JAN-09

7 rows selected.
9. To display the next Sunday from the date ’07-JUN-96’

SQL> SELECT next_day('07-jun-96','saturday') as Next_Sunday_Date from dual;

NEXT_SUNDAY_DATE
---------------------------------
08-JUN-96

10. To list the ItemName, which are within the warranty period till present date.

SQL> select itemname from equip_details where (add_months(d_o_purchase,(warranty*12))) > sysdate;


no rows selected

26 | P a g e
Activity : 5 (set operators)
Database : subject
Create Following table and insert tuples with suitable constraints
Table - Physics
Regno Name Year Combination
1441SB01 Abhishek First PCM
1441SB02 Charan Second PMCs
1441SB03 Sonika Third PME
1441SB04 Divya First PMCs

Table – Computer Science


Regno Name Year Combination
1441SB02 Charan Second PMCs
1441SB05 Naveen Second BCA
1441SB06 Pavan First BCA
1441SB04 Divya First PMCs

1. Select all students from physics and Computer Science


2. Select student common in physics and Computer Science
3. Display all student details those are studying in second year
4. Display student those who are studying both physics and computer science in second year
5. Display the students studying only physics
6. Display the students studying only Computer Science
7. select all student having PMCs combination
8. select all student having BCA combination
9. select all student studying in Third year
10. Rename table Computer Science to CS

CREATE TABLE:-

TABLE 1:-

SQL> create table physics(regno varchar(7),name varchar(10),year varchar(10),combination varchar(6),primary


key(regno));

Table created.

TABLE 2:-

SQL> create table com_sci(regno varchar(7),name varchar(10),year varchar(10),combination varchar(6),primary


key(regno) );

Table created.

27 | P a g e
INSERT VALUES TO TABLES:-

TABLE 1:-

SQL> insert into physics values('&regno','&name','&year','&combination');


Enter value for regno: 1441SB01
Enter value for name: Abhishek
Enter value for year: First
Enter value for combination: PCM

1 row created.

SQL> /
Enter value for regno: 1441SB02
Enter value for name: Charan
Enter value for year: Second
Enter value for combination: PMCs

1 row created.

SQL> /
Enter value for regno: 1441SB03
Enter value for name: Sonika
Enter value for year: Third
Enter value for combination: PME

1 row created.

SQL> /
Enter value for regno: 1441SB04
Enter value for name: Divya
Enter value for year: First
Enter value for combination: PMCs

1 row created.

SQL> select * from physics;

REGNO NAME YEAR COMBIN


------- ---------- ---------- ------
1441SB01 Abhishek First PCM
1441SB02 Charan Second PMCs
1441SB03 Sonika Third PME
28 | P a g e
1441SB04 Divya First PMCs

TABLE 2:-

SQL> insert into com_sci values('&regno','&name','&year','&combination');


Enter value for regno: 1441SB02
Enter value for name: Charan
Enter value for year: Second
Enter value for combination: PMCs

1 row created.

SQL> /
Enter value for regno: 1441SB05
Enter value for name: Naveen
Enter value for year: Second
Enter value for combination: BCA

1 row created.

SQL> /
Enter value for regno: 1441SB06
Enter value for name: Pavan
Enter value for year: First
Enter value for combination: BCA

1 row created.

SQL> /
Enter value for regno: 1441SB04
Enter value for name: Divya
Enter value for year: First
Enter value for combination: PMCs

1 row created.

SQL> SELECT * FROM COM_SCI;

REGNO NAME YEAR COMBIN


------- ---------- ---------- ------
1441SB02 Charan Second PMCs
1441SB05 Naveen Second BCA
1441SB06 Pavan First BCA
1441SB04 Divya First PMCs
29 | P a g e
QUERIES:-

1. Select all students from physics and Computer Science

SQL> select name from physics union select name from com_sci;

NAME
----------
Abhishek
Pavan
Divya
Sonika
Charan
Naveen

6 rows selected.

2. Select student common in physics and Computer Science

SQL> select name from physics intersect select name from com_sci;

NAME
----------
Divya
Charan

3. Display all student details those are studying in second year

SQL> select * from physics where year='Second' union select * from com_sci where year='Second';

REGNO NAME YEAR COMBIN


------- ---------- ---------- ------
1441SB02 Charan Second PMCs
1441SB05 Naveen Second BCA

4. Display student those who are studying both physics and computer science in second year

SQL> select name from physics where year='Second' intersect select name from com_sci where year='Second';

no rows selected

5. Display the students studying only physics

30 | P a g e
SQL> select name from physics minus select name from com_sci;

NAME
----------
Abhishek
Sonika

6. Display the students studying only Computer Science

SQL> select name from com_sci minus select name from physics;

NAME
----------
Pavan
Naveen

7. select all student having PMCs combination

SQL> select name from physics where combination='PMCs' union select name from com_sci where
combination='PMCs';

NAME
----------
Divya
Charan

8. select all student having BCA combination

SQL> select name from physics where combination='BCA' union select name from com_sci where
combination='BCA';

NAME
----------
Pavan
Naveen

9. select all student studying in Third year

SQL> select name from physics where year='Third' union select name from com_sci where year='Third';

NAME
----------

31 | P a g e
Sonika

10. Rename table Computer Science to CS

SQL> alter table com_sci rename to computer_science;

Table altered.

SQL> select * from computer_science;

REGNO NAME YEAR COMBIN


------- ---------- ---------- ------
1441SB02 Charan Second PMCs
1441SB05 Naveen Second BCA
1441SB06 Pavan First BCA
1441SB04 Divya First PMCs

32 | P a g e
Activity 6 : (views)
Database: Railway Reservation System
Create Following table and insert tuples with suitable constraints

Table: Train Details


Train_no Train_name Start_place DESTINATIONn
RJD96 Rajdhani Express Bangalore Mumbai
KRW01 Karwar Express Chennai Mangalore
KKE55 Karnataka Express Bangalore Chennai
SHV07 Shivaji Express Hubli Bangalore
STB11 Shatabdi Express Bangalore Mysore

Table : Availability
Train_no Class Start_Place DESTINATION No_of_seats
n
RJD96 Sleeper Class Banglore Mumbai 15
KRW01 First Class Chennai Mangalore 22
KKE55 First Class AC Bangalore Chennai 15
SHV07 Second Class Hubli Bangalore 8
STB11 Sleeper Class Bangalore Mysore 18

1. Create view sleeper to display train no, start place, DESTINATIONn and perform the following
a. insert new record
b. update DESTINATIONn=’Manglore’ where train no=’RJD96’
c. delete a record which have train no=’KKE55’
2. Create view details to display train no, train name, class
3. Create view total_seats to display train number, start place, use SUM function to no of seats , group by start
place
4. Rename view sleeper to class
5. Delete view details

CREATE TABLE:-

TABLE 1:-

SQL> create table train_details(train_no varchar(10),train_name varchar(15),start_place


varchar(10),DESTINATIONn varchar(10),primary key(train_no));

Table created.

TABLE 2:-

SQL> create table availability(t_no varchar(10),class varchar(15),start_place varchar(15),DESTINATIONn


varchar(10),no_of_seats number,foreign key(t_no) references train_details(train_no));
33 | P a g e
Table created.

INSERT VALUES INTO:-

TABLE 1:-

SQL> insert into train_details values('&train_no','&train_name','&start_place','&DESTINATIONn');


Enter value for train_no: RJD96
Enter value for train_name: Rajdhani Express
Enter value for start_place: Bangalore
Enter value for DESTINATIONn: Mumbai

1 row created.

SQL> /
Enter value for train_no: KRW01
Enter value for train_name: Karwar Express
Enter value for start_place: Chennai
Enter value for DESTINATIONn: Mangalore

1 row created.

SQL> /
Enter value for train_no: KKE55
Enter value for train_name: Karnataka Express
Enter value for start_place: Bangalore
Enter value for DESTINATIONn: Chennai

1 row created.

SQL> /
Enter value for train_no: SHV07
Enter value for train_name: Shivaji Express
Enter value for start_place: Hubli
Enter value for DESTINATIONn: Bangalore

1 row created.

SQL> /
Enter value for train_no: STB11
Enter value for train_name: Shatabdi Express
Enter value for start_place: Bangalore
Enter value for DESTINATIONn: Mysore
34 | P a g e
1 row created.

SQL> select * from train_details;

TRAIN_NO TRAIN_NAME START_PLAC DESTINATION


---------- ------------------- - ---------- ----------
RJD96 Rajdhani Express Bangalore Mumbai
KRW01 Karwar Express Chennai Mangalore
KKE55 Karnataka Express Bangalore Chennai
SHV07 Shivaji Express Hubli Bangalore
STB11 Shatabdi Express Bangalore Mysore

TABLE 2:-

SQL> insert into availability values('&t_no','&class','&strat_place',


'&DESTINATIONn','&no_of_seats');
Enter value for t_no: RJD96
Enter value for class: Sleeper Class
Enter value for strat_place: Banglore
Enter value for DESTINATIONn: Mumbai
Enter value for no_of_seats: 15

1 row created.

SQL> /
Enter value for t_no: KRW01
Enter value for class: First Class
Enter value for strat_place: Chennai
Enter value for DESTINATIONn: Mangalore
Enter value for no_of_seats: 22

1 row created.

SQL> /
Enter value for t_no: KKE55
Enter value for class: First Class AC
Enter value for strat_place: Bangalore
Enter value for DESTINATIONn: Chennai
Enter value for no_of_seats: 15
1 row created.

SQL> /
Enter value for t_no: SHV07
Enter value for class: Second Class
35 | P a g e
Enter value for strat_place: Hubli
Enter value for DESTINATIONn: Bangalore
Enter value for no_of_seats: 8

1 row created.

SQL> /
Enter value for t_no: STB11
Enter value for class: Sleeper Class
Enter value for strat_place: Bangalore
Enter value for DESTINATIONn: Mysore
Enter value for no_of_seats: 18

1 row created.

SQL> select * from availability;

T_NO CLASS START_PLACE DESTINATION NO_OF_SEATS


---------- --------------- --------------- ---------- -----------
RJD96 Sleeper Class Bangalore Mumbai 15
KRW01 First Class Chennai Mangalore 22
KKE55 First Class AC Bangalore Chennai 15
SHV07 Second Class Hubli Bangalore 8
STB11 Sleeper Class Bangalore Mysore 18

QUERIES:-

1. Create view sleeper to display train no, start place, DESTINATIONn


and perform the following

SQL> create view sleeper as select t_no,start_place,DESTINATIONn from availability;


View created.

SQL> select * from sleeper;

T_NO START_PLACE DESTINATION


---------- --------------- ----------
KRW01 Chennai Mangalore
KKE55 Bangalore Chennai
SHV07 Hubli Bangalore
STB11 Bangalore Mysore
RJD96 Banglore Mumbai

A. insert new record


36 | P a g e
SQL> insert into sleeper values('KKE55','Mysore','Bangalore');

1 row created.

SQL> select * from sleeper;

T_NO START_PLACE DESTINATION


---------- --------------- ----------
KRW01 Chennai Mangalore
KKE55 Bangalore Chennai
SHV07 Hubli Bangalore
STB11 Bangalore Mysore
KKE55 Mysore Bangalore
RJD96 Banglore Mumbai

6 rows selected.

B. update DESTINATIONn=’Manglore’ where train no=’RJD96’

SQL> update sleeper set DESTINATIONn='Manglore' where t_no='RJD96';

1 row updated.

SQL> select * from sleeper;

T_NO START_PLACE DESTINATION


---------- --------------- ----------
KRW01 Chennai Mangalore
KKE55 Bangalore Chennai
SHV07 Hubli Bangalore
STB11 Bangalore Mysore
KKE55 Mysore Bangalore
RJD96 Banglore Manglore
6 rows selected.

C. delete a record which have train no=’KKE55’

SQL> delete sleeper where t_no='KKE55';

2 rows deleted.

2. Create view details to display train no, train name, class

37 | P a g e
SQL> create view details as select train_no,train_name,class from train_details,availability where
train_details.train_no=availability.t_no;

View created.

SQL> select * from details;

TRAIN_NO TRAIN_NAME CLASS


---------- -------------------- ---------------
KRW01 Karwar Express First Class
SHV07 Shivaji Express Second Class
STB11 Shatabdi Express Sleeper Class
RJD96 Rajdhani Express Sleeper Class

3. Create view total_seats to display start place, use sum function to no of seats , group by start place

SQL> create view total_seats as select distinct start_place,sum(no_of_seats) as sum from availability group by
start_place;

View created.

SQL> select * from total_seats;

START_PLACE sum
--------------- ---------
Bangalore 18
Chennai 22
Hubli 8

4. Rename view sleeper to class

SQL> rename sleeper to class;

Table renamed.

SQL> select * from class;

T_NO START_PLACE DESTINATION


---------- --------------- ----------
KRW01 Chennai Mangalore
SHV07 Hubli Bangalore
STB11 Bangalore Mysore
RJD96 Banglore Manglore

38 | P a g e
5. Delete view details

SQL> delete details where train_no='RJD96';

1 row deleted.

SQL> select * from details;

TRAIN_NO TRAIN_NAME CLASS


---------- -------------------- ---------------
KRW01 Karwar Express First Class
SHV07 Shivaji Express Second Class
STB11 Shatabdi Express Sleeper Class

39 | P a g e
Activity 7 : (group by, having clause)
Database: Bank system
Create Following table and insert tuples with suitable constraints

Table: Account Table: Branch

Account_no Cust_Name Branch_ID


Branch_ID Branch_Name Branch_City
AE00128560 Abhishek SB002 SB001 Malleshwaram Bangalore
AE1185698 Charan SB001
SB002 MG Road Bangalroe
AE1203996 Kumar SB004 SB003 MG Road Mysore
AE1225889 Meher SB002 SB004 Jayanagar Mysore
AE8532166 Naveen SB003
AE8552266 Pavan SB003
AE1003996 Yogeesh SB004
AE1100996 Prakash SB002

Table: Depositor Table: Loan

Account_no Branch_Id Balance


Account_no Branch_Id Balance
AE0012856 SB002 12000 AE1185698 SB001 102000

AE1203996 SB004 58900 AE8552266 SB003 40000

AE8532166 SB003 40000 AE1003996 SB004 15000


AE1225889 SB002 150000 AE1100996 SB002 100000

1. Display Total Number of accounts present in each branch


2. Display Total Loan amount in each branch
3. Display Total deposited amount in each branch by descending order
4. Display max , min loan amount present in each city.
5. Display average amount deposited in each branch , each city
6. Display maximum of loan amount in each branch where balance is more than 25000
7. Display Total Number of accounts present in each city
8. Display all customer details in ascending order of branchid
9. Update Balance to 26000 where account_no=AE1003996
10. Display Customer Names with their branch Name

Note:-first create table branch and also while inserting values to tables, first insert into branch table.

Create table:-

Table 1:-

SQL> create table Branch(Branch_ID varchar(15), Branch_Name varchar(15), Branch_City varchar(15), primary
key(Branch_ID));
40 | P a g e
Table created.

Table 2:-

SQL> create table Account(Account_No varchar(15), Cust_Name varchar(15), Branch_ID varchar(15), primary
key(Account_No), foreign key(branch_id) references branch(branch_id));

Table created.

Table 3:-

SQL> create table Depositor(Account_No varchar(15), Branch_ID varchar(15), Balance number, foreign
key(Account_No) references Account(Account_No), foreign key(branch_id) references branch(branch_id));

Table created.

Table 4:-

SQL> create table Loan(Account_No varchar(15), Branch_ID varchar(15), Balance number, foreign
key(Account_No) references Account(Account_No), ), foreign key(branch_id) references branch(branch_id));

Table created.

Insert values:-

Table 1:-

SQL> insert into Branch values('&B_ID', '&B_Name','&B_City');


Enter value for branch_id: SB001
Enter value for branch_name: Malleshwaram
Enter value for branch_city: Bangalore

1 row created.

SQL> /
Enter value for branch_id: SB002
Enter value for branch_name: MG Road
Enter value for branch_city: Bangalroe

1 row created.

SQL> /
Enter value for branch_id: SB003
41 | P a g e
Enter value for branch_name: MG Road
Enter value for branch_city: Mysore

1 row created.

SQL> /
Enter value for branch_id: SB004
Enter value for branch_name: Jayanagar
Enter value for branch_city: Mysore

1 row created.

SQL> select * from Branch;

B_ID B_NAME B_CITY


--------------- --------------- ---------------
SB001 Malleshwaram Bangalore
SB002 MG Road Bangalroe
SB003 MG Road Mysore
SB004 Jayanagar Mysore

Table 2:-

SQL> insert into account values(‘&account_no’,’&cust_name’,’&branch_id’);


Enter value for account_no: AE0012856
Enter value for cust_name: Abhishek
Enter value for branch_id: SB002

1 row created.

SQL> /
Enter value for account_no: AE1185698
Enter value for cust_name: Charan
Enter value for branch_id: SB001

1 row created.
SQL> /
Enter value for account_no: AE1203996
Enter value for cust_name: Kumar
Enter value for branch_id: SB004

1 row created.

SQL> /
42 | P a g e
Enter value for account_no: AE1225889
Enter value for cust_name: Meher
Enter value for branch_id: SB002

1 row created.

SQL> /
Enter value for account_no: AE8532166
Enter value for cust_name: Naveen
Enter value for branch_id: SB003

1 row created.

SQL> /
Enter value for account_no: AE8552266
Enter value for cust_name: Pavan
Enter value for branch_id: SB003

1 row created.

SQL> /
Enter value for account_no: AE1003996
Enter value for cust_name: Yogeesh
Enter value for branch_id: SB004

1 row created.

SQL> /
Enter value for account_no: AE1100996
Enter value for cust_name: Prakash
Enter value for branch_id: SB002

1 row created.

SQL> select * from Account;

ACCOUNT_NO CUST_NAME BRANCH_ID


--------------- --------------- ---------------
AE0012856 Abhishek SB002
AE1185698 Charan SB001
AE1203996 Kumar SB004
AE1225889 Meher SB002
AE8532166 Naveen SB003
AE8552266 Pavan SB003
43 | P a g e
AE1003996 Yogeesh SB004
AE1100996 Prakash SB002

8 rows selected.

Table 3:-

SQL> insert into Depositor values('&Account_No','&Bran_ID',&Balance);


Enter value for account_no: AE0012856
Enter value for branch_id: SB002
Enter value for balance: 12000

1 row created.

SQL> /
Enter value for account_no: AE1203996
Enter value for branch_id: SB004
Enter value for balance: 58900

1 row created.

SQL> /
Enter value for account_no: AE8532166
Enter value for branch_id: SB003
Enter value for balance: 40000

1 row created.

SQL> /
Enter value for account_no: AE1225889
Enter value for branch_id: SB002
Enter value for balance: 150000

1 row created.

Table 4:-

SQL> insert into Loan values('&A_No','&BID',&Bal);


Enter value for account_no: AE1185698
Enter value for branch_id: SB001
Enter value for balance: 102000

1 row created.

44 | P a g e
SQL> /
Enter value for a_no: AE8552266
Enter value for branch_id: SB003
Enter value for balance: 40000

1 row created.

SQL> /
Enter value for account_no: AE1003996
Enter value for branch_id: SB004
Enter value for balance: 15000

1 row created.

SQL> /
Enter value for account_no: AE1100996
Enter value for branch_id: SB002
Enter value for balance: 100000

1 row created.

QUERIES:-

1. Display Total Number of accounts present in each branch

SQL> Select account.branch_id,COUNT(account.branch_id) as no_of_acc from account,branch where


account.branch_id=branch.branch_id group by account.branch_id;

BRANCH_ID NO_OF_ACC
--------------- ----------------
SB001 1
SB002 3
SB003 2
SB004 2

2. Display Total Loan amount in each branch

SQL> select branch_id,sum(balance) as t_loan_amt from loan group by branch_id;

BID T_LOAN_AMT
----- --------
SB001 102000
SB002 100000
SB003 40000
45 | P a g e
SB004 15000

3. Display Total deposited amount in each branch by descending order

SQL> Select branch_id,sum(balance) from depositor group by branch_id order by sum(balance) desc;

BRAN_ID SUM(BALANCE)
----------- ------------
SB002 162000
SB004 58900
SB003 40000

4. Display max , min loan amount present in each city.

SQL> Select branch_city,max(balance),min(balance) from branch,loan where branch.branch_id=loan.branch_id


group by branch_city;

Branch_CITY MAX(BALANCE) MIN(BALANCE)


--------------- --------- ---------
Banglore 102000 100000
Mysore 40000 15000

5. Display average amount deposited in each branch,each city

SQL> Select depositor.branch_id,branch_city,avg(balance) from depositor,branch where


depositor.branch_id=branch.branch_id group by depositor.branch_id,branch_city;

BRAN_ID B_CITY AVG(BALANCE)


------------ - --------- ------------
SB002 Banglore 81000
SB003 Mysore 40000
SB004 Mysore 58900

6. Display maximum of loan amount in each branch where balance is more than 25000

SQL> Select branch_id,max(balance) from loan group by branch_id having max(balance)>25000;

Branch_ID MAX(BALANCE)
------------- --------
SB001 102000
SB002 100000
SB003 40000

46 | P a g e
7. Display Total Number of accounts present in each city

SQL> Select branch_city,COUNT(branch_city) as no_of_acc from account,branch where


account.branch_id=branch.branch_id group by branch_city;

Branch_CITY NO_OF_ACC
--------------- ---------
Banglore 4
Mysore 4

8. Display all customer details in ascending order of branchid

SQL> select * from account order by branch_id;

ACCOUNT_NO CUST_NAME BRANCH_ID


--------------- --------------- ---------------
AE1185698 Charan SB001
AE0012856 Abhishek SB002
AE1100996 Prakash SB002
AE1225889 Meher SB002
AE8532166 Naveen SB003
AE8552266 Pavan SB003
AE1203996 Kumar SB004
AE1003996 Yogeesh SB004

8 rows selected.

9. Update Balance to 26000 where accno=AE1003996

SQL> Update LOAN set balance=26000 where account_no='AE1003996';

1 row updated.

SQL> SELECT * FROM LOAN;

Account_NO Branch_ID BAL


--------------- --------------- ---------
AE1185698 SB001 102000
AE8552266 SB003 40000
AE1003996 SB004 26000
AE1100996 SB002 100000

10. Display Customer Names with their branch Name

47 | P a g e
SQL> Select distinct cust_name,branch_name from account,branch where
account.branch_id=branch.branch_id;

CUST_NAME B_NAME
--------------- ---------------
Charan Malleshwaram
Pavan MG Road
Kumar Jayanagar
Abhishek MG Road
Meher MG Road
Yogeesh Jayanagar
Naveen MG Road
Prakash MG Road

8 rows selected.

48 | P a g e

You might also like