Sample Manual
Sample Manual
Sample Manual
Question:
Create the following schemas
student(regno, sname, cgpa)
course(ccode, cname, credits)
faculty(empid, fname, designation, salary)
SQL Query :
create table student(regno varchar(9), sname varchar(20), cgpa number(2));
create table course(ccode varchar(9), cname varchar(20), credits
number(3));
create table faculty(empid varchar(9), fname varchar(20), designation
varchar(20), salary number(7));
Output:
Table created.
Table created.
Table created.
Question: Change the data type of sname in student relation and describe
the structure
Question: Change the size of any attribute and describe the structure
Question: Add phone number attribute to student relation and describe the
structure
SQL> alter table student add phone integer(10);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0
Question: Delete the email id attribute from student and describe the
structure
SQL> alter table student drop column email;
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0
Question: Copy the contents of student table and name the table as student
new.
SQL> create table student_new as select * from student;
Query OK, 5 rows affected (0.14 sec)
Records: 5 Duplicates: 0 Warnings: 0
SQL> select * from student_new;
+-----------+-------+------+------------+
| regno | sname | cgpa | phone |
+-----------+-------+------+------------+
| 16BCE1001 | harsh | 9 | 8979867898 |
| 16BCE1002 | alex | 9.3 | 8979867899 |
| 16BCE1003 | abhi | 8.9 | 8979867800 |
| 16BCE1004 | virat | 7.8 | 8979867810 |
| 16BCE1005 | anmol | 8.3 | 8979867811 |
+-----------+-------+------+------------+
5 rows in set (0.00 sec)
Question: Delete the contents of student new table, describe the structure
and display the contents of the table
SQL> truncate table student_new;
Query OK, 0 rows affected (0.08 sec)
Question: List the name and designation of the faculty who receives salary
between 1 lakh and 2 lakhs
SQL> select fname,designation from faculty where salary>100000 and
salary<200000;
Empty set (0.00 sec)
Question: Sort the faculty details in ascending order based on the salary
they receive.
SQL> select * from faculty order by salary asc;
+------+-------+-------------+--------+
| fid | fname | designation | salary |
+------+-------+-------------+--------+
| 1003 | mona | asst. prof. | 10000 |
| 1004 | tony | asst. prof. | 10000 |
| 1001 | mohan | prof. | 30000 |
| 1005 | john | prof. | 100000 |
| 1002 | rohan | prof. | 300000 |
+------+-------+-------------+--------+
5 rows in set (0.00 sec)
Question: Sort the designation of the faculty by descending order and name
of the faculty in ascending order.
SQL> select * from faculty order by designation desc, fname asc;
+------+-------+-------------+--------+
| fid | fname | designation | salary |
+------+-------+-------------+--------+
| 1005 | john | prof. | 100000 |
| 1001 | mohan | prof. | 30000 |
| 1002 | rohan | prof. | 300000 |
| 1003 | mona | asst. prof. | 10000 |
| 1004 | tony | asst. prof. | 10000 |
+------+-------+-------------+--------+
5 rows in set (0.00 sec)
Question: List the course names which has more than 3 credits
SQL> select * from course where credits>3;
+-------+-------+---------+
| ccode | cname | credits |
+-------+-------+---------+
| c1011 | DBMS | 4 |
| c2034 | IWP | 4 |
| c3004 | ML | 4 |
| c4014 | AI | 4 |
| c1002 | DLD | 4 |
+-------+-------+---------+
5 rows in set (0.00 sec)
Question: List the name of the student whose name starts with ‘r’ and ends
with ‘i’
SQL> select sname from student where sname like 'r%i';
Empty set (0.00 sec)
2. Authors Table:
a. authorId – primary key
b. email
3. Book_Authors:
a. isbn – foreign key references books table
b. authorId – foreign key references authors table
SQL Query:
create table books(isbn varchar(10), title varchar(20), price
float(5) not null, qty int(3) not null, constraint myuni1
unique(title), constraint pk primary key(isbn));
create table author(authorID varchar(10), name varchar(30),
email varchar(20), constraint pk primary key(authorID));
create table ba(isbn varchar(10), authorID varchar(10),
constraint fk1 foreign key(isbn) references books(isbn),
constraint fk2 foreign key(authorID) references
author(authorID));
desc books;
desc author;
desc ba;
Output:
Query OK, 0 rows affected (0.69 sec)
Query OK, 0 rows affected (0.41 sec)
Query OK, 0 rows affected (0.17 sec)
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | NO | PRI | NULL | |
| title | varchar(20) | YES | UNI | NULL | |
| price | float | NO | | NULL | |
| qty | int(3) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.45 sec)
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| authorID | varchar(10) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | YES | MUL | NULL | |
| authorID | varchar(10) | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
1. Constraints
Query:
alter table books modify title varchar2(20) not null;
desc books;
Output:
Query OK, 0 rows affected (0.24 sec)
Records: 0 Duplicates: 0 Warnings: 0
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | NO | PRI | NULL | |
| title | varchar(20) | NO | UNI | NULL | |
| price | float | NO | | NULL | |
| qty | int(3) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
Query:
alter table author add constraint myuni1 unique(email);
desc author;
Output:
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| authorID | varchar(10) | NO | PRI | NULL | |
| email | varchar(20) | YES | UNI | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
Query:
alter table books modify price float(5) null;
alter table books add constraint chk1 check(price>0.0);
desc books;
Output:
Query OK, 0 rows affected (0.28 sec)
Records: 0 Duplicates: 0 Warnings: 0
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | NO | PRI | NULL | |
| title | varchar(20) | NO | UNI | NULL | |
| price | float | YES | | NULL | |
| qty | int(3) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
Query:
alter table books modify qty int(3) null;
desc books;
Output:
Query OK, 0 rows affected (0.23 sec)
Records: 0 Duplicates: 0 Warnings: 0
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | NO | PRI | NULL | |
| title | varchar(20) | NO | UNI | NULL | |
| price | float | YES | | NULL | |
| qty | int(3) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
Query:
alter table books alter qty int set default 0;
desc books;
Output:
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | NO | PRI | NULL | |
| title | varchar(20) | NO | UNI | NULL | |
| price | float | YES | | NULL | |
| qty | int(3) | YES | | 0 | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
Query:
alter table ba drop foreign key fk1;
desc ba;
alter table ba drop foreign key fk1;
Output:
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | YES | MUL | NULL | |
| authorID | varchar(10) | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
Query:
update author set email='kelvinjones@xyz.com' where authorID=4;
select * from author;
Output:
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
+----------+--------------+---------------------+
| authorID | name | email |
+----------+--------------+---------------------+
| 1 | Tan Ah Teck | teck@nowhere.com |
| 2 | Mohamed Ali | ali@somewhere.com |
| 3 | Kumar | kumar@abc.com |
| 4 | Kelvin Jones | kelvinjones@xyz.com |
+----------+--------------+---------------------+
4 rows in set (0.00 sec)
Output:
Query OK, 4 rows affected (0.08 sec)
Rows matched: 4 Changed: 4 Warnings: 0
+------+------------------+--------+------+
| isbn | title | price | qty |
+------+------------------+--------+------+
| 1001 | Java for Dummies | 12.221 | 11 |
| 1002 | Only Java | 24.442 | 22 |
| 1003 | Java ABC | 36.663 | 33 |
| 1004 | Java 123 | 48.884 | 44 |
+------+------------------+--------+------+
4 rows in set (0.00 sec)
Output:
+----------+--------------+---------------------+
| authorID | name | email |
+----------+--------------+---------------------+
| 4 | Kelvin Jones | kelvinjones@xyz.com |
| 3 | Kumar | kumar@abc.com |
| 2 | Mohamed Ali | ali@somewhere.com |
| 1 | Tan Ah Teck | teck@nowhere.com |
+----------+--------------+---------------------+
4 rows in set (0.00 sec)
Ex. No 4 – Joins
The cartesian product and the implicit inner join are the same.
3. List the courses with more than 2 credits which was taught
by the faculty “mohan” [Equi Join and Non Equi Join]
4. List the students with >9 CGPA and has taken CSE3002 course
taught by “Prof. Sai” [Cross Join and Left Outer Join]
REGNO CCODE
--------- ----------
16BCE1001 CSE1004
16BCE1001 CSE3002
16BCE1001 HUM1001
16BCE1001 CHY1002
16BCE1002 CSE3002
16BCE1003 CSE2004
16BCE1004 CSE1004
16BCE1005 CSE3002
8 rows selected.
1. List the students who have took CSE2004 taught by Prof. alok
SQL> select regno from course_student natural join
course_faculty natural join faculty where ccode='CSE2004' and
fname='alok';
REGNO
---------
16BCE1003
COUNT(REGNO)
------------
3
3. Count the students who have got >8.5 CGPA whose name starts
with a
SQL> select * from student where cgpa>8.5 and sname like 'a%';
FNAME
--------------------
ashok
alok
6. List the courses with more than 3 credits which was took by
the student harsh
CNAME
--------------------
networks
COUNT(CCODE)
------------
2
8. Count the courses which was not taken by the student geeta
5-COUNT(CCODE)
--------------
1
8 rows selected.
OLD TABLES:
SQL> select * from student;
REGNO CCODE
---------- ----------
16BCE1001 HUM1001
16BCE1003 HUM1001
16BCE1003 CSE2004
16BCE1001 CSE1004
16BCE1001 CSE3002
16BCE1001 CSE1002
16BCE1002 CSE3002
16BCE1004 CSE1004
16BCE1005 CSE3002
16BCE1003 CSE2004
10 rows selected.
CCODE EMPID
---------- ----------
CSE2004 f1003
CSE3002 f1004
CSE1004 f1002
CSE1002 f1001
HUM1001 f1005
CCODE
----------
CSE1002
CSE1004
CSE2004
CSE3002
HUM1001
no rows selected
no rows selected
no rows selected
5. List the faculty who are not handling any courses.
no rows selected
With Select
6. List the students’ regno who took courses with credits more
than 3
REGNO
----------
16BCE1003
16BCE1003
16BCE1004
16BCE1001
16BCE1005
16BCE1002
16BCE1001
7 rows selected.
no rows selected
With Update
8. Increase the cgpa by 0.5 for the students who have taken
CSE3002
3 rows updated.
4 rows updated.
With Delete
10. Delete all courses which was not taken by any of the
students
SQL> delete from course where ccode in (select ccode from course
minus select ccode from course_student);
0 rows deleted.
REGNO
----------
16BCE1001
SQL> declare
2 a number:=10;
3 b number:=20;
4 c number:=30;
5 begin
6 if(a>b and a>c) then
7 dbms_output.put_line('a is greatest');
8 elsif(b>a and b>c) then
9 dbms_output.put_line('b is greatest');
10 else
11 dbms_output.put_line('c is greatest');
12 end if;
13 end;
14 /
c is greatest
a=153
b=1^3+5^3+3^3 = 1+125+27=153
a=153 is equal to b=153, so 153 is an Armstrong number.
SQL> declare
2 n integer:=153;
3 s integer:=0;
4 r integer;
5 m integer;
6 begin
7 m:=n;
8 while m>0
9 loop
10 r:=mod(m,10); --mod is used to find the
remainder
11 s:=s+(r*r*r);
12 m:=trunc(m/10); --trunc is used to reduce
decimal points to avoid round operation
13 end loop;
14 if n=s
15 then
16 dbms_output.put_line('Armstrong Number');
17 else
18 dbms_output.put_line('Not an Armstrong Number');
19 end if;
20 end;
21 /
Armstrong Number
SQL> declare
2 a number:=10;
3 b number:=20;
4 c number;
5 ch varchar(20);
6 begin
7 ch:='add';
8 case ch
9 when 'add' then
10 c:=a+b;
11 dbms_output.put_line('Addition is '||c);
12 when 'subtract' then
13 c:=a-b;
14 dbms_output.put_line('Subtraction is '||
c);
15 when 'multiply' then
16 c:=a*b;
17 dbms_output.put_line('Multiplication is
'||c);
18 when 'divide' then
19 c:=a/b;
20 dbms_output.put_line('Division is '||c);
21 end case;
22 end;
23 /
Addition is 30
REGNO CCODE
---------- ----------
16BCE1001 HUM1001
16BCE1003 HUM1001
16BCE1003 CSE2004
16BCE1001 CSE1004
16BCE1001 CSE3002
16BCE1001 CSE1002
16BCE1002 CSE3002
16BCE1004 CSE1004
16BCE1005 CSE3002
16BCE1003 CSE2004
10 rows selected.
Procedure created.
Function created.
SQL> begin
2 dbms_output.put_line('Maximum cgpa is '||cgpa());
3 end;
4 /
Maximum cgpa is 9.3
SQL> begin
2 execute immediate 'alter table course_student add marks
number';
3 end;
4 /
Procedure created.
4. Get the marks of the the regno 102 and display his grade
Procedure created.
1 Create table for the above schema with primary and foreign key
constraints.
SQL> create table hotel(hotelNo number(10), hotelName
varchar2(20), city varchar2(20), constraint pk1 primary
key(hotelNo));
Table created.
Table created.
Table created.
Table created.
Table altered.
Table altered.
1 row created.
SQL> /
Enter value for hotelno: 2
Enter value for hotelname: meridian
Enter value for city: california
old 1: insert into hotel values(&hotelNo, '&hotelName',
'&city')
new 1: insert into hotel values(2, 'meridian', 'california')
1 row created.
1 row created.
SQL> /
Enter value for roomno: 102
Enter value for hotelno: 1
Enter value for type: executive deluxe
Enter value for price: 8000
old 1: insert into room values(&roomNo, &hotelNo, '&type',
&price)
new 1: insert into room values(102, 1, 'executive deluxe',
8000)
1 row created.
SQL> /
Enter value for roomno: 103
Enter value for hotelno: 1
Enter value for type: deluxe
Enter value for price: 5000
old 1: insert into room values(&roomNo, &hotelNo, '&type',
&price)
new 1: insert into room values(103, 1, 'deluxe', 5000)
1 row created.
SQL> /
Enter value for roomno: 201
Enter value for hotelno: 2
Enter value for type: deluxe
Enter value for price: 6000
old 1: insert into room values(&roomNo, &hotelNo, '&type',
&price)
new 1: insert into room values(201, 2, 'deluxe', 6000)
1 row created.
SQL> /
Enter value for roomno: 202
Enter value for hotelno: 2
Enter value for type: executive deluxe
Enter value for price: 10000
old 1: insert into room values(&roomNo, &hotelNo, '&type',
&price)
new 1: insert into room values(202, 2, 'executive deluxe',
10000)
1 row created.
SQL> /
Enter value for roomno: 203
Enter value for hotelno: 2
Enter value for type: executive deluxe
Enter value for price: 10000
old 1: insert into room values(&roomNo, &hotelNo, '&type',
&price)
new 1: insert into room values(203, 2, 'executive deluxe',
10000)
1 row created.
1 row created.
SQL> /
Enter value for hotelno: 2
Enter value for guestno: 2
Enter value for datefrom: 13-AUG-18
Enter value for dateto: 15-AUG-18
Enter value for roomno: 203
old 1: insert into booking
values(&hotelno,&guestno,'&datefrom','&dateto',&roomno)
new 1: insert into booking values(2,2,'13-AUG-18','15-AUG-
18',203)
1 row created.
SQL> /
Enter value for hotelno: 2
Enter value for guestno: 3
Enter value for datefrom: 13-SEP-18
Enter value for dateto: 20-SEP-18
Enter value for roomno: 201
old 1: insert into booking
values(&hotelno,&guestno,'&datefrom','&dateto',&roomno)
new 1: insert into booking values(2,3,'13-SEP-18','20-SEP-
18',201)
1 row created.
SQL> /
Enter value for hotelno: 1
Enter value for guestno: 4
Enter value for datefrom: 14-OCT-18
Enter value for dateto: 16-OCT-18
Enter value for roomno: 103
old 1: insert into booking
values(&hotelno,&guestno,'&datefrom','&dateto',&roomno)
new 1: insert into booking values(1,4,'14-OCT-18','16-OCT-
18',103)
1 row created.
1 row created.
SQL> /
Enter value for guestno: 2
Enter value for guestname: Alex
Enter value for guestaddress: Sector 33
old 1: insert into guest
values(&guestno,'&guestname','&guestaddress')
new 1: insert into guest values(2,'Alex','Sector 33')
1 row created.
SQL> /
Enter value for guestno: 3
Enter value for guestname: Lisa
Enter value for guestaddress: LA
old 1: insert into guest
values(&guestno,'&guestname','&guestaddress')
new 1: insert into guest values(3,'Lisa','LA')
1 row created.
SQL> /
Enter value for guestno: 4
Enter value for guestname: Rick
Enter value for guestaddress: LA
old 1: insert into guest
values(&guestno,'&guestname','&guestaddress')
new 1: insert into guest values(4,'Rick','LA')
1 row created.
COUNT(HOTELNO)
--------------
1
1
COUNT(ROOMNO) HOTELNO
------------- ----------
3 1
3 2
ROOMNO HOTELNO
---------- ----------
102 1
202 2
10 List the details of all rooms at the Meridian Hotel,
including the name of the guest
staying in the room, if the room is occupied.
SQL> select
room.roomno,room.hotelno,room.type,room.price,guest.guestname
from room left join booking natural full outer join guest on
room.roomno=booking.roomno where room.hotelno in (select
distinct(hotelno) from hotel where hotelname='meridian');
10 rows selected.
SQL> DECLARE
2 total_rows number(2);
3 BEGIN
4 UPDATE student SET cgpa='9.1' WHERE regno='16BCE1001';
5 IF sql%notfound THEN
6 dbms_output.put_line('no data updated');
7 ELSIF sql%found THEN
8 total_rows:=sql%rowcount;
9 dbms_output.put_line(total_rows||' data updated');
10 END IF;
11 END;
12 /
1 data updated
SQL> DECLARE
2 c_cname course.cname%type;
3 c_credits course.credits%type;
4 CURSOR c_course is SELECT cname, credits FROM course;
5 BEGIN
6 OPEN c_course;
7 LOOP
8 FETCH c_course into c_cname, c_credits;
9 EXIT WHEN c_course%notfound;
10 dbms_output.put_line(c_credits||' '||c_cname);
11 END LOOP;
12 CLOSE c_course;
13 END;
14 /
3 dbms
3 chemistry
3 networks
4 iwp
3 humanities
3. List the name of the students who have more than 9 cgpa
SQL> DECLARE
2 s_sname student.sname%type;
3 CURSOR s_student is SELECT sname FROM student WHERE cgpa>9;
4 BEGIN
5 OPEN s_student;
6 LOOP
7 FETCH s_student into s_sname;
8 EXIT WHEN s_student%notfound;
9 dbms_output.put_line(s_sname);
10 END LOOP;
11 CLOSE s_student;
12 END;
13 /
harsh
anuj
abhishek
mudit