Submitted To: - Submitted By:-: Page 1 of 32
Submitted To: - Submitted By:-: Page 1 of 32
Page 1 of 32
TABLE CONTENTS
Sr.No Questions. Page
. no.
1. CREATE THE TABLES STRUCTURE 3
2. INSERT THE DATA IN TO RESPECTIVE TABLES. 5
3. SINGLE TABLE RETRIEVAL 8
4. FUNCTION AND CONCATENATION 15
5. HAVING AND GROUP BY 18
6. JOINS AND CORRELATIONS 20
7. NESTED QUERIES 22
8. QUERIES USING DATE 25
9. TABLE UPDATIONS 27
10. PL/SQL 28
12. TRIGGERS 32
Page 2 of 32
CREATE THE TABLES STRUCTURE
SQL> create table cust(cust_id varchar2(3) primary
key,lnamevarchar2(15),fname varchar2(15), area varchar2(2),phone_no number(8));
Table created.
SQL> create table movie (mv_no number(2) primary key,title varchar2(25), type
varchar2(10),star varchar2(25), price number(8,2));
Table created.
SQL>desc movie;
Table created.
Page 3 of 32
SQL> desc invoice;
Page 4 of 32
INSERT THE DATA IN TO RESPECTIVE TABLES
1 row created.
6 rows selected.
1 row created.
10 rows selected.
1 row created.
10 rows selected.
Page 7 of 32
SINGLE TABLE RETRIEVAL
LNAME
---------------
Bayross
Saitwal
Jaguste
Navindgi
Sreedbaran
Rukmani
6 rows selected.
6 rows selected.
Q-3 Retrieve the list of fname and the area of all customers.
FNAME AREA
----------------------------
Ivan sa
Vandana mu
Pramada da
Basu ba
Page 8 of 32
Ravi va
6 rows selected.
Q-4 List the various movie types available from the movie table.
TYPE
----------
Action
Thriller
Romance
Comedy
Thriller
Suspense
Horror
Comedy
Drama
Comedy
10 rows selected.
Q-5 Find the names of all customers having ‘a’ as the second letter in their fnames.
LNAME FNAME
------------------------------
Saitwal Vandana
Navindgi Basu
Sreedbaran Ravi
Q-6 Find the names of all customers that begin with ‘s’ or ‘j’.
SQL> select lname,fname from cust where fname like '%s' or fname like '%j';
no rows selected
Q-7 Find out the customers who stay in an area whose second letter is ‘a’.
SQL> select cust_id,fname || ' ' || lname "Full Name" from cust where area like '_a%';
Page 9 of 32
CUS_ID Full Name
--- -------------------------------
a01 Ivan Bayross
a03 Pramada Jaguste
a04 Basu Navindgi
a05 Ravi Sreedbaran
Q-8 Find the list of all customers who stay in area ‘da’ or area ‘mu’ or area ‘gh’.
SQL> select cust_id,fname || ' ' || lname "Full Name" from cust where area='da' or
area='mu' or area='gh';
Q-9 Print the list of employees whose phone numbers are greater than the value
5550000.
SQL> select cust_id,fname || ' ' || lname "Employee Name" from cust where
phone_no>5550000;
Q-10 Print the information from invoice table of customers who have been issued movies
in the month of September.
INV_NO
-----------
i04
i06
i010
Q-11 Display the Invoice table information for cust_id ‘a01’ and ‘a02’.
Page 10 of 32
SQL> select *from invoice where cust_id='a01' and cust_id='a02';
no rows selected
TITLE
-------------------------
Bloody Vengeance
Home Alone
Quick Change
Carry On Doctor
Q-13 Find the movies whose price is greater then 150 and less then or equal to 200.
SQL> select title from movie where price >150 and price<=200;
no rows selected
Q-14 Find the movies that cost more than 150 and also find the new cost as original
cost *15.
TITLE PRICE*15
------------------------------------------
Bloody Vengeance 7500000
The Firm 6750000
Pretty Woman 6000000
Home Alone 3750000
The Fugitive 9750000
Corna 6750000
Dracula 9750000
Quick Change 12750000
Gone With The Wind 9750000
Carry On Doctor 11250000
10 rows selected.
Page 11 of 32
Q-15 Rename the new column in the above query as new_price.
TITLE New_Price
--------------------------------------------
Bloody Vengeance 7500000
The Firm 6750000
Pretty Woman 6000000
Home Alone 3750000
The Fugitive 9750000
Corna 6750000
Dracula 9750000
Quick Change 12750000
Gone With The Wind 9750000
Carry On Doctor 11250000
10 rows selected.
TITLE
-------------------------
Bloody Vengeance
Carry On Doctor
Corna
Dracula
Gone With The Wind
Home Alone
Pretty Woman
Quick Change
The Firm
The Fugitive
10 rows selected.
Q-17 Print the names and types of all the movies except horror movie.
TITLE TYPE
Page 12 of 32
----------------------------------------
Bloody Vengeance Action
The Firm Thriller
Pretty Woman Romance
Home Alone Comedy
The Fugitive Thriller
Corna Suspense
Quick Change Comedy
Gone With The Wind Drama
Carry On Doctor Comedy
9 rows selected.
SQRT(PRICE)
-------------------
707.106781
670.820393
632.455532
500
806.225775
670.820393
806.225775
921.954446
806.225775
866.025404
10 rows selected.
Q-19 Divide the cost of movie ‘home alone’ by difference between its price and 100.
PRICE/(PRICE-100)
--------------------------
1.00040016
Q-20 List the names,areas and cust_id of customers without phone number.
SQL> select fname ||' '|| lname,area,cust_id from cust where phone_no is NULL;
Page 13 of 32
FNAME||''||LNAME AREA CUS_ID
------------------------------------------------------------
Ravi Sreedbaran va a05
Rukmani gh a06
SQL> select fname ||' '|| lname from cust where lname is NULL;
no rows selected
Q-22 List the mv_no,title,type of movies whose stars begin with letter ‘m’.
MV_NO INV_NO
-----------------------------
4 i01
8 i010
3 i02
1 i03
6 i04
Page 14 of 32
FUNCTION AND CONCATENATION
COUNT(CUST_ID)
--------------------------
SUM(PRICE)
-----------------
5600000
AVG(PRICE)
------------------
560000
Q-27 Determine the Maximum and Minimum movie Prices. Rename the Title as
max_price and min_price.
Max_Price Min_Price
-----------------------------
850000 250000
Q-28 Count the number of Movies having Price greater than or equal to 150.
COUNT(MV_NO)
------------------------
Page 15 of 32
10
Q-29 Print the information of Invoice table in the following format for all records.
(A) The Invoice No. of Customer Id.{cust_id} is {inv_no} and Movie No. is {mv_no}.
SQL> select 'The invoice No. of customer id.{'||cust_id||'} is {'||inv_no||'} and Movie
NO. is {'||mv_no||'}' from invoice;
'THEINVOICENO.OFCUSTOMERID.{'||CUST_ID||'}IS{'||INV_NO||'}ANDMOVIENO.IS{'||
MV_NO||'}'
---------------------------------------------------------------------------------------------------------------
The invoice No. of customer id.{a01} is {i01} and Movie NO. is{4}
The invoice No. of customer id.{a02} is {i02} and Movie NO. is{3}
The invoice No. of customer id.{a02} is {i03} and Movie NO. is{1}
The invoice No. of customer id.{a03} is {i04} and Movie NO. is{6}
The invoice No. of customer id.{a04} is {i05} and Movie NO. is{7}
The invoice No. of customer id.{a06} is {i06} and Movie NO. is{2}
The invoice No. of customer id.{a05} is {i07} and Movie NO. is{9}
The invoice No. of customer id.{a01} is {i08} and Movie NO. is{9}
The invoice No. of customer id.{a03} is {i09} and Movie NO. is{5}
The invoice No. of customer id.{a06} is {i010} and Movie NO. is{8}
10 rows selected.
'{'||CUST_ID||'}HASTAKENMOVIENO.{'||MV_NO||'}ON{{'||ISSUE_DATE||'}ANDWILL
RETURNO
---------------------------------------------------------------------------------------------------------------
Page 16 of 32
{a01} Has Taken Movie No. {4}on{{23-JUL-93} and will return on {25-JUL-93}
{a02} Has Taken Movie No. {3}on{{12-AUG-93} and will return on {15-AUG-93}
{a02} Has Taken Movie No. {1}on{{15-AUG-93} and will return on {18-AUG-93}
{a03} Has Taken Movie No. {6}on{{10-SEP-93} and will return on {13-SEP-93}
{a04} Has Taken Movie No. {7}on{{05-AUG-93} and will return on {08-AUG-93}
{a06} Has Taken Movie No. {2}on{{18-SEP-93} and will return on {21-SEP-93}
{a05} Has Taken Movie No. {9}on{{07-JUL-93} and will return on {10-JUL-93}
{a01} Has Taken Movie No. {9}on{{11-AUG-93} and will return on {14-AUG-93}
{a03} Has Taken Movie No. {5}on{{06-JUL-93} and will return on {09-JUL-93}
{a06} Has Taken Movie No. {8}on{{03-SEP-93} and will return on {06-SEP-93}
10 rows selected.
Page 17 of 32
HAVING AND GROUP BY
TYPE AVG(PRICE)
----------------------------------
Action 500000
Comedy 616666.667
Drama 650000
Horror 650000
Romance 400000
Suspense 450000
Thriller 550000
7 rows selected.
7 rows selected.
Q-32 Count separately the number of Movies in the ‘Comedy’ and ‘Thriller’ Types.
SQL> select type,COUNT (mv_no)"No. of Movies" from movie group by type having
type='Comedy' or type='Thriller';
Q-33 Calculate the Average price for each that has a maximum price of 150.00.
SQL> select AVG (price) from movie group by type having max (price)=150;
no rows selected
Q-34 Calculate the Average price of all Movies where type is ‘Comedy’ or ‘Thriller’
and price is greater than or equal to 150.00
SQL> select AVG (price) from movie group by type having type IN 'Comedy',
'Thriller') and AVG (price) >=150;
AVG(PRICE)
------------------
616666.667
550000
Page 19 of 32
JOINS AND CORRELATIONS:
Q35.Find out the movie no. which has been issued to ‘ivan’.
MV_NO
----------
4
9
Q36.Finds the names and movie no. of all the customers who have been issued.
TITLE TYPE
------------------------- ----------
bloody vengeance action
pretty woman romance
Q37.Select the title cust_id ,mv_no for all the movies that are issued.
10 rows selected.
Page 20 of 32
Q38.Find out the title and types of movies that have been issued to ‘vandana’.
SQL> select cust.fname || ' ' || cust.lname,movie.type from movie,cust,invoice
2 where cust.cust_id=invoice.cust_id and movie.mv_no=invoice.mv_no
3 and cust.fname='Vandana';
CUST.FNAME||''||CUST.LNAME TYPE
------------------------------- ----------
Vandana Saitwal action
Vandana Saitwal romance
Q39.Find the name of customers who have been issued movie of type ‘drama’.
SQL> select cust.fname || ' ' || cust.lname from cust,invoice,movie where
2 cust.cust_id=invoice.cust_id and movie.mv_no=invoice.mv_no
3 and movie.type='drama';
CUST.FNAME||''||CUST.LNAME
-------------------------------
Ravi Sreedbaran
Ivan Bayross
Q40.Display the title, lname,fname for customers having movie no. greater than or
Equal to three in the following format.
SQL> select 'The movie is taken by {' || cust.fname || '} ,{' || cust.lname || '} is
{' || movie.title || '}' from movie,cust,invoice where cust.cust_id=invoice.cust_i
d and invoice.mv_no=movie.mv_no and invoice.mv_no>=3;
'THEMOVIEISTAKENBY{'||CUST.FNAME||'},{'||CUST.LNAME||'}IS{'||
MOVIE.TITLE||'}'
------------------------------------------------------------------------
--------
The movie is taken by {Vandana} ,{Saitwal} is {pretty woman}
The movie is taken by {Ivan} ,{Bayross} is {home alone}
The movie is taken by {Pramada} ,{Jaguste} is {the fugitive}
The movie is taken by {Pramada} ,{Jaguste} is {corna}
The movie is taken by {Basu} ,{Navindgi} is {dracula}
The movie is taken by {} ,{Rukhmani} is {quic change}
The movie is taken by {Ivan} ,{Bayross} is {gone with the wind}
The movie is taken by {Ivan} ,{Bayross} is {gone with the wind}
8 rows selected.
Page 21 of 32
NESTED QUERIES:
Q41.Find out which customers have been issued movie no. 9.
SQL> select fname || ' ' || lname from cust,invoice where cust.cust_id=invoice.c
ust_id and mv_no=(select distinct mv_no from invoice where mv_no=9);
FNAME||''||LNAME
-------------------------------
Ravi Sreedbaran
Ivan Bayross
FNAME||''||LNAME AR
------------------------------- --
Rukhmani gh
Q43.Find the customer names and phone no. who have been issued movies before the month of august.
FNAME||''||CUST.LNAME PHONE_NO
------------------------------- ----------
Ivan Bayross 6125467
Ravi Sreedbaran
TITLE
-------------------------
bloody vengeance
pretty woman
home alone
gone with the wind
Page 22 of 32
Q45.List the movie no,movie names issued to all customers.
SQL> select movie.mv_no,movie.title from invoice,movie where invoice.mv_no=movie.mv_no;
MV_NO TITLE
---------- -------------------------
1 bloody vengeance
2 the firm
3 pretty woman
4 home alone
5 the fugitive
6 corna
7 dracula
8 quic change
9 gone with the wind
9 gone with the wind
10 rows selected.
Q46.Find the type and movie no of movie issued to cust_id ‘a01’ and ‘a02’.
TYPE MV_NO
---------- ----------
comedy 4
drama 9
action 1
romance 3
Q47.Find out if the movie stareing ‘tom cruise’is issued to any customer and print the custid to whom it is
issued.
CUS
---
a06
Page 23 of 32
Q48.Find the lname,fname who have been issued movies.
SQL> select cust.lname, cust.fname from cust where cust.cust_id in (select invoi
ce.cust_id from cust,invoice where invoice.cust_id=cust.cust_id);
LNAME FNAME
--------------- ---------------
Bayross Ivan
Saitwal Vandana
Jaguste Pramada
Navindgi Basu
Sreedbaran Ravi
Rukhmani
6 rows selected.
Page 24 of 32
QUERIES USING DATE:
Q49.Display the invoice no and day on which customer were issued movies.
INV_NO DAY
---- --
i01 23
i02 12
i03 15
i04 10
i05 05
i06 18
i07 07
i08 11
i09 06
i010 03
10 rows selected.
Q50.Display the month (in alphabets) in which customer are supposed to return the movies.
TO_
---
jul
aug
aug
sep
aug
sep
jul
aug
aug
sep
10 rows selected.
Page 25 of 32
Q51.Display the issue date in the format ‘dd-mm-yy’.
TO_CHAR(
--------
25-07-93
15-08-93
18-08-93
13-09-93
08-08-93
21-09-93
10-07-93
14-08-93
09-08-93
06-09-93
10 rows selected.
SYSDATE+1
---------
21-MAY-06
Q53.find the no. of days elapsed between the current date and the return date of the movie for all customers.
SQL> select sysdate-return_date "Difference" from invoice;
Difference
----------
4668.55839
4647.55839
4644.55839
4618.55839
4654.55839
4610.55839
4683.55839
4648.55839
4653.55839
4625.55839
10 rows selected.
Page 26 of 32
TABLE UPDATIONS:
Q54.Change the telephone number of Pramada to 466389.
1 row updated.
SQL> update movie set price=250 where title='gone with the wind';
1 row updated.
Q57.Delete the record with invoice no.’i08’ from the invoice table.
SQL> delete from invoice where inv_no='i08';
1 row deleted.
Q58.Delete all the records having return date before 10th july 93.
SQL> delete from invoice where return_date < '10-jul-93';
1 row deleted.
Q59.Change the area of cust_id ‘a05’ to vs.
SQL> update cust set area='vs' where cust_id='a05';
1 row updated.
Page 27 of 32
PL/SQL:
SQL> desc areas;
Name Null? Type
----------------------------------------- --------
-------------------------
RADIOUS NUMBER(5)
AREA NUMBER(14,2)
SQL> declare
2 pi constant number(4,2):=3.14;
3 radius number(5);
4 area number(14,2);
5 begin
6 radius:=3;
7 while radius<=7
8 loop
9 area:=pi*power(radius,2);
10 insert into areas values(radius,area);
11 radius:=radius+1;
12 end loop;
13 end;
14 /
Page 28 of 32
SQL> select * from areas;
RADIOUS AREA
---------- ----------
3 28.26
4 50.24
5 78.5
6 113.04
7 153.86
Table created.
SQL> declare
2 sqr number(10,2);
3 num number(5);
4 begin
5 num:=1;
6 while num<=10
7 loop
8 sqr:=sqrt(num);
9 insert into sqrno values(num,sqr);
10 num:=num+1;
11 end loop;
12 end;
13 /
NUM SQR
---------- ----------
1 1
2 1.41
3 1.73
4 2
5 2.24
6 2.45
7 2.65
8 2.83
9 3
10 3.16
Page 29 of 32
SQL> declare
2 fi number(3):=0;
3 sec number(3):=1;
4 th number(3);
5 term number(3);
6 num number(3);
7 begin
8 term:=&term;
9 dbms_output.put_line(fi);
10 dbms_output.put_line(sec);
11 num:=1;
12 while num<=term
13 loop
14 th:=fi+sec;
15 dbms_output.put_line(th);
16 fi:=sec;
17 sec:=th;
18 num:=num+1;
19 end loop;
20 end;
21 /
Enter value for term: 15
old 8: term:=&term;
new 8: term:=15;
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
SQL> declare
2 num number(5);
3 rem number(5);
4 renum number(5):=0;
Page 30 of 32
5 begin
6 num:=#
7 while num>0
8 loop
9 rem:=mod(num,10);
10 renum:=renum*10+rem;
11 num:=num/10;
12 end loop;
13 dbms_output.put_line(renum);
14 end;
15 /
Enter value for num: 1234
old 6: num:=#
new 6: num:=1234;
4321
Page 31 of 32
TRIGGERS:
1 row created.
USERNAME TODAYDATE O
---------------- ---------------- --
PGDCA6 04-MAY-06 I
Page 32 of 32