Вы находитесь на странице: 1из 56

TY BSc IT –Sem V 2009

S.I.E.S COLLEGE OF COMMERCE AND


ECONOMICS
SION (EAST), MUMBAI 400 022.

LABORATORY JOURNAL
B.Sc. (I.T.)
Semester V
2009 – 2010

Name :

Roll No. :

Batch :

Subject :

SQL 2-Journal Page 1


TY BSc IT –Sem V 2009

SIES COLLEGE OF
COMMERCE AND ECONOMICS
SION(EAST), MUMBAI – 400 022.

B.Sc.(I.T.)
Semester V

Certificate
Certificate

This is certify that Mr. / Miss_______________________________

of B.Sc. (IT) Batch _______________Roll No.______________________

has completed a course of necessary experiments in the subject of


_________________________________________________under my

Supervision during the academic year 2009-2010.

Co-ordinator Lecturer-in-charge

Date : Date :

SQL 2-Journal Page 2


TY BSc IT –Sem V 2009

Q1.Write the query for the following questions:

Emp(ename,street,city)

Work(ename,cname,sal)

Company(cname,city)

Manager(ename,mname)

SQL>create table emp27(ename char(10) primary key,street varchar(10),city char(10));

SQL>insert into emp27 values('&ename','&street','&city');

SQL> select * from emp27;

ENAME STREET CITY

---------- ---------- ----------

tazz cst mumbai

diths kokan furus

heena tv marg sion

tom kalina rd santacruz

dick hazzel gotham

harry straw mumbai

6 rows selected.

SQL>create table work27(ename char(10) primary key,cname varchar(10),sal number(6));

SQL>insert into work27 values('&ename','&cname','&sal');

SQL> select * from work27;

ENAME CNAME SAL

---------- ---------- ----------

tazz Emirates 60000

diths sbi 45000

heena sbi 40000

SQL 2-Journal Page 3


TY BSc IT –Sem V 2009

tom Emirates 59000

dick Chiaro 34000

SQL>create table co27(cname char(10) primary key,city char(10));

SQL>insert into co27 values('&cname','&city');

SQL> select * from co27;

CNAME CITY

---------- ----------

sbi furus

sbi mumbai

Chiaro sion

Emirates gotham

Chiaro mumbai

sbi santacruz

SQL>create table man27(ename char(10),mname char(10));

SQL>insert into man27 values('&ename','&mname');

SQL> select * from man27;

ENAME MNAME

---------- ----------

tazz tom

diths tazz

tom heena

harry tom

1. Find the name of employees who work for SBI .

Ans:SQL> select ename from work27 where cname='sbi';

ENAME

----------

SQL 2-Journal Page 4


TY BSc IT –Sem V 2009

diths

heena

2. Find the name ,street,city of all employees who work at SBI and salary greater than
10000

Ans:SQL> select e.ename,street,city from emp27 e,work27 w

where e.ename=w.ename and sal>10000 and cname='sbi';

ENAME STREET CITY

---------- ---------- ----------

tazz cst mumbai

diths kokan furus

heena tv marg sion

tom kalina rd santacruz

dick hazzel gotham

harry straw mumbai

3.Find all employees in database who live in the same city as the company which they
work.

Ans: SQL>select emp27.ename from emp27,co27,work27

where emp27.ename=works27.ename and co27.cname=work27.cname and


co27.city=emp27.city

4 Find all employees in database who live in the same city and same street as their
managers.

Ans: SQL>select emp27.ename from man27,emp27 where

emp27.ename=man27.ename and city=(select city from emp27 where ename=mname);

5. Find all employees in database who don’t work for SBI.

Ans: SQL>select ename from work27 where cname<>'sbi';

SQL 2-Journal Page 5


TY BSc IT –Sem V 2009

ENAME

----------

tazz

tom

dick

6. Find all employees in database who earn more than each employee of SBI.

Ans: SQL>select ename from work27 where sal>all(select sal from work27 where
cname='sbi');

ENAME

----------

tazz

tom

7. Assume company located in several cities. Find all company located in every city in
which SBI is located.

Ans: SQL>select distinct cname from co27 where city in (select city from co27 where
cname='sbi');

CNAME

----------

sbi

Chiaro

8.Find all employees who earn more than average salary of all employees of their
company.

Ans: SQL>select w.ename,w.sal from work27 w

where w.sal> (select avg(b.sal) from work27 b where w.cname=b.cname group by


b.cname);

ENAME SAL

SQL 2-Journal Page 6


TY BSc IT –Sem V 2009

---------- ----------

harry 65000

tazz 60000

diths 45000

9. Find company that has most employees.

Ans: SQL>select cname from work27 group by cname

having count(cname)= (select max(count(cname)) from work27 group by cname);

CNAME

----------

Chiaro

sbi

Emirates

10. Find company that has smallest payroll.

Ans: SQL>select distinct cname from work27 where sal = (select min(sal) from work27);

CNAME

----------

Chiaro

11. Find those companies whose employee earn a higher, on average than average salary
at SBI.

Ans: SQL>select distinct cname from work27

where sal > (select avg(sal) from work27 where cname='sbi' group by cname);

CNAME

----------

sbi

Emirates

SQL 2-Journal Page 7


TY BSc IT –Sem V 2009

Q2. Write the query for the following questions:

Emp(empid,ename,age,sal)

Works(empid,did,pct_time)

Dept(did,budget,mgrid)

SQL> create table emp27q2 (empid number(2) primary key,ename char(10),age


number(2),sal number(6));

Table created.

SQL> insert into emp27q2 values('&empid','&ename','&age','&sal');

SQL> select * from emp27q2;

EMPID ENAME AGE SAL

---------- ---------- ---------- ----------

1 tashu 20 45000

2 priya 21 40000

3 shraddha 20 42000

4 prachi 22 100005

5 amreen 19 80000

SQL> create table work27q2 (empid number(2),did number(2),pct_time number(4));

Table created.

SQL> insert into work27q2 values('&empid','&did','&pct_time');

SQL> select * from work27q2;

EMPID DID PCT_TIME

---------- ---------- ----------

1 12 78

1 2 67

2 1 56

2 2 77

3 12 44
SQL 2-Journal Page 8
TY BSc IT –Sem V 2009

3 2 32

4 4 4

5 2 99

8 rows selected.

SQL> create table dept27 (did number(2),budget number(6),mid number(2));

Table created.

SQL> insert into dept27 values('&did','&budget','&mid');

SQL> select * from dept27;

DID BUDGET MID

---------- ---------- ----------

1 12000 1

2 100020 5

4 90000 4

12 120000 1

1. Print name and age of each employee who work in both hardware and software
department.

Ans: SQL>select distinct e.empid,ename,age from emp27q2 e,work27q2 w where


e.empid=w.empid and did in ('12','2');

EMPID ENAME AGE

---------- ---------- ---

1 tashu 20

2 priya 21

3 shraddha 20

5 amreen 19

6 rows selected.

SQL 2-Journal Page 9


TY BSc IT –Sem V 2009

2. Print name of employee whose budget exceeds of all departments that he or she
works in.

Ans: SQL>select ename from emp27q2

where sal > all (select budget from dept27 , work27q2 where dept27.did=work27q2.did

and work27q2.empid=emp27q2.empid);

ENAME

----------

prachi

3. Find managerid os manager who manage only department with budget>100000.

Ans: SQL>select mid from dept27 where budget>100000;

MID

----------

4. Find name of manager who manage department with largest budget.

Ans: SQL>select ename from emp27q2,dept27 where empid=mid and budget=

(select max(budget) from dept27);

ENAME

----------

tashu

5. Find manager id of managers who control largest amount.

Ans: SQL>select mid from dept27 where budget =

2 (select max(budget) from dept27);

MID

----------
SQL 2-Journal Page 10
TY BSc IT –Sem V 2009

SQL 2-Journal Page 11


TY BSc IT –Sem V 2009

Q3. Write the query for the following questions:

Book(id,title,author,publisher,category,year,price,distid)

Distributor(distid, name , city,discount)

SQL> select * from book1027;

ID TITLE AUTHOR PUBLISHER CATEGORY YEAR PRICE

---------- ---------- ---------- ---------- ---------- ---------- ----------

DISTID

----------

1 murder agatha tmh mystery 1981 555

2 mistake chetan tcpip it 2009 350

3 idiot amir khan movie 2009 400

4 famousfive enid tmh horror 2005 460

SQL> select * from dist1027;

DISTID DISNAME CITY DISCOUNT

---------- ---------- ---------- ----------

1 pulpy shimla 5

2 mango furus 2

3 orange nagpur 4

SQL 2-Journal Page 12


TY BSc IT –Sem V 2009

1. Display the details of all books whose price is more than the average price of all
books.

Ans: SQL>select * from book1027 where price>

(select avg(price) from book1027);

ID TITLE AUTHOR PUBLISHER CATEGORY YEAR PRICE

---------- ---------- ---------- ---------- ---------- ---------- ----------

DISTID

----------

1 murder agatha tmh mystery 1981 555

4 famousfive enid tmh horror 2005 460

Q2. Display the details of books written by ‘Agatha’ and supplied by ‘TMH’.

Ans: SQL>select * from book1027 where author='agatha' and publisher='tmh';

ID TITLE AUTHOR PUBLISHER CATEGORY YEAR PRICE

---------- ---------- ---------- ---------- ---------- ---------- ----------

DISTID

----------

1 murder agatha tmh mystery 1981 555

Q.4. Write the query for the following questions:


SQL 2-Journal Page 13
TY BSc IT –Sem V 2009

Emp(eid,ename,address,phone,salary,design,did,doj_date)

client(cid ,cname ,address ,phone )


project(pid ,pname ,contract_date ,cid ,did ,cost )
dept (did,dname)

SQL>create table temp10(eid number(1),ename char(10),address varchar(15),phone


number(10),salary number(5),design char(10),did number(4),doj date,dob date);

Table created.

SQL>insert into temp10


values('&eid','&ename','&address','&phone'.'&salary','&design','&did','&doj','&dob');

select * from temp10;

EID ENAME ADDRESS PHONE SALARY DESIGN


----- ---------- --------------- ---------- ---------- --------
DID DOJ DOB
----- --------- ---------
E01 subho sion 9820887228 45000 manager
2001 01-JAN-08 04-NOV-90

E02 madhu dadar 9820551971 34000 chairman


2002 06-MAR-08 09-SEP-92

E08 riddhi vt 9876543210 20000 manager


2003 04-JUN-09 01-JAN-89

SQL>create table tclient(cid number(2),cname char(10),address varchar(15),phone


number(10));

SQL>insert into tclient values('&cid','&cname','&address','&phone');

select * from tclient;

CID CNAME ADDRESS PHONE


------ ---------- --------------- ----------
6 venus ghat 9876897643
7 fairy nerul 9868435210
8 tubbies dadar 9876890976

SQL>create table tproject(pid number(3),pname char(10),contract_date date,cid


number(2),did number(4),cost number(5));

SQL 2-Journal Page 14


TY BSc IT –Sem V 2009

insert into tproject values('&pid','&pname','&contract_date','&cid','&did','&cost');

select * from tproject;

PID PNAME CONTRACT_ CID DID COST


----- ---------- --------- ---------- ---------- ----------
101 building 03-JUL-08 6 2001 40000
102 bridge 03-JUL-08 7 2001 20000
103 hall 17-AUG-08 8 2003 10000

SQL>create table tdept(did number(4),dname char(10));

insert into tdept values('&did','&dname');

select * from tdept;

DID DNAME
------ ----------
2001 it
2002 sales
2003 hr

1. Display projects undertaken by department to which emp ‘E08’ belongs.

Ans: SQL>select pid,pname


from tproject,temp10
where eid='E08'and
temp10.did=tproject.did;

PID PNAME
---- ----------
103 hall

2. Display name of employee getting maximum salary.

Ans: SQL>select ename


from temp10
where salary=(select max(salary) from temp10);

ENAME
----------
Subho

3. Display name of employee getting minimum salary.

Ans: SQL>select ename

SQL 2-Journal Page 15


TY BSc IT –Sem V 2009

from temp10
where salary=(select min(salary) from temp10);

ENAME
----------
Riddhi

4. Count number of employee in each department.

Ans: SQL>select count(eid),did


from temp10
group by did;

COUNT(EID) DID
---------- ----------
1 2001
1 2003
1 2002

5. Delete entries of project developed in 3 years back.


Ans: SQL>delete from tproject
where to_char(contract_date,'yy')<to_char(sysdate,'yyyy')-3;

3 rows deleted.

6. Count number of managers in each department.

Ans: SQL>select count(eid)


from temp10
where design='manager'
group by did;

COUNT(EID)
----------
2

7. Delete all entries of project developed in first quadrant of current year.


Ans: SQL> delete contract_date from tproject
where to_char(contract_date,’yy’)=to_char(sysdate) and
to_char(contract_date,’mm’) in(‘01’,’02’,’03’);

Table deleted.

Q5. Write the query for the following questions:

Supplier(sno,sname,status,city)

SQL 2-Journal Page 16


TY BSc IT –Sem V 2009

Parts(pno,pname,color,weight)

Shipment(sno,pno,qty,s_date)

SQL> create table parts1027(pno number(4),pname char(10),color char(10),weight


number(3)

2 ,city char(10));

Table created.

SQL> insert into parts1027 values('&pno','&pname','&color','&weight','&city');

SQL> select * from parts1027;

PNO PNAME COLOR WEIGHT CITY

---------- ---------- ---------- ---------- ----------

1 engine red 1 mumbai

2 door blue 4 delhi

3 airbag red 3 pune

4 seat black 5 goa

5 wiper red 6 jammu

SQL> select * from sup1027;

SNO SNAME STATUS CITY

---------- ---------- ---------- --------

11 subh 1 mumbai

12 tasu 2 goa

13 vini 3 kerla

14 tess 4 jammu

SQL> select * from ship27;

SNO PNO QTY S_DATE

---------- ---------- ---------- ---------


SQL 2-Journal Page 17
TY BSc IT –Sem V 2009

11 1 40 01-JAN-09

15 1 20 03-FEB-09

13 3 50 05-MAR-09

14 4 30 31-AUG-09

15 5 60 31-AUG-09

1.Display the number of suppliers currently supplying red parts.

Ans:SQL> select count(distinct sno) from parts1027,ship27 where color='red' and


ship27.pno=parts1027.pno;

COUNT(DISTINCTSNO)

------------------

2.Display part no. of all parts supplied.

Ans:SQL> select distinct pno from ship27;

PNO

----------

3.Display all parts whose weight is between 1 and 3.

Ans:SQL> select * from parts1027 where weight between 1 and 3;

PNO PNAME COLOR WEIGHT CITY


SQL 2-Journal Page 18
TY BSc IT –Sem V 2009

---------- ---------- ---------- ---------- ----------

1 engine red 1 mumbai

3 airbag red 3 pune

4.Display name of suppliers who dont supply atleast one part.

Ans:SQL> select sname from sup1027 where sno not in (select sno from ship27);

SNAME

----------

tasu

5. Create a dupliocate table named sup_dup with all records for supplier table.

Ans:SQL> create table supplier_dup1027 as select * from sup1027;

Table created.

SQL> select * from supplier_dup1027;

SNO SNAME STATUS CITY

---------- ---------- ---------- --------

11 subh 1 mumbai

12 tasu 2 goa

13 vini 3 kerla

14 tess 4 jammu

6. Display number of suppliers for part no. p2.

Ans:SQL> select count(sno) from ship27 where pno='1';

COUNT(SNO)

----------

SQL 2-Journal Page 19


TY BSc IT –Sem V 2009

7. List cities where either a part locates or supplier resides.

Ans:SQL> select distinct city from parts1027 union select distinct city from sup1027;

CITY

----------

dadar

delhi

goa

goa

jammu

jammu

kerla

mumbai

mumbai

pune

pune

11 rows selected.

8. List all supplier names with string "d_a".

Ans:SQL> select sname from sup1027 where sname like '%d$_a%' escape '$';

SNAME

----------

duaa

9.display name of suppliers who supply parts on current date.

Ans:SQL> select sname from sup1027, ship1027

where sdate = current_date and ship1027.sno=sup1027.sno;

no rows selected

SQL 2-Journal Page 20


TY BSc IT –Sem V 2009

10. Display name of parts supplied in last quadrant of 2009.

Ans:SQL> select pname from ship1027,parts1027

where to_char(sdate,'yy') = '09'

and to_char(sdate,'mm') in ('10','11','12')

and ship1027.pno=parts1027.pno;

PNAME

----------

wiper

OR

Ans:SQL> select pname from ship1027,parts1027

2 where to_char(sdate,'yy') = '09'

3 and to_char(sdate,'q')=4

4 and ship1027.pno=parts1027.pno;

PNAME

----------

Wiper

Q6. Write the query for the following questions:

Supplier(sno,sname,status,city)

parts(pno,pname,color,weight,city)

spj(sno ,pno ,pr_id ,qty)

SQL 2-Journal Page 21


TY BSc IT –Sem V 2009

SQL> select * from sup1027;

SNO SNAME STATUS CITY

---------- ---------- ---------- --------

11 subh 1 mumbai

12 tasu 2 goa

13 vini 3 kerla

14 tess 4 jammu

15 kini 5 pune

16 duaa 6 dadar

6 rows selected.

SQL> select * from parts1027;

PNO PNAME COLOR WEIGHT CITY

---------- ---------- ---------- ---------- ----------

1 engine red 1 mumbai

2 door blue 4 delhi

3 airbag red 3 pune

4 seat black 5 goa

5 wiper red 6 jammu

SQL> select * from pr1027;

PR_ID PR_NAME CITY

---------- ---------- ----------

99 bldg mumbai

SQL 2-Journal Page 22


TY BSc IT –Sem V 2009

98 railway goa

97 house jammu

96 office dehli

95 socket pune

SQL> create table spj2727(sno number(2),pno number(2),pr_id number(2),qty number(2));

Table created.

SQL> insert into spj2727 values('&sno','&pno','&pr_no','&qty');

SQL> select * from spj2727;

SNO PNO PR_ID QTY

---------- ---------- ---------- ----------

11 1 99 4

12 2 98 5

13 3 97 6

14 4 96 1

SQL> select * from spj2727;

SNO PNO PR_ID QTY

---------- ---------- ---------- ----------

11 1 99 4

12 2 98 5

13 3 97 6

14 4 96 1

15 3 99 6

16 4 96 4

6 rows selected.

SQL 2-Journal Page 23


TY BSc IT –Sem V 2009

1. Display parts numbers of the parts that are supplied either by ‘mumbai’ supplier or to a
‘Mumbai ‘ project.

Ans:SQL> select pno from spj2727

where sno in (select sno from sup1027 where city='mumbai')

and pr_id in (select pr_id from pr1027 where city='mumbai');

PNO

----------

2. Display total number of projects supplied by supplier s1.

Ans:SQL> select count(pno) from sup1027,spj2727

where sup1027.sno=spj2727.sno

and sno='11';

COUNT(PNO)

----------

3. Add a constraint valid_colors which will check for valid color:red,green and blue.

Ans:SQL>alter table parts1027 add constraint subh_color check(color in('red','blue','black'));

Table altered.

4. Display the supplier no, partsno pair such that indicated supplier does not supply
indicated part.

Ans:SQL> select a.sno, b.pno from sup1027 a,parts1027 b

(select c.sno from spj2727 c where c.sno=a.sno and c.pno=b.pno);

5. Display parts no. for parts supplied by more than one supplier.

Ans:SQL> select pno from spj2727 group by pno having count(*)>1;

no rows selected

SQL 2-Journal Page 24


TY BSc IT –Sem V 2009

6. Display supplier name for suppliers who supplies atleast one part.

Ans:SQL> select sname from sup1027, spj2727 where

2 sup1027.sno=spj2727.sno;

SNAME

----------

subh

tasu

vini

tess

7. Define a view consisting of all projects that are supplied by s1 and p1.

Ans:SQL> create view prjview as

2 select spj2727.pr_id,pr_name,city from pr1027,spj2727

3 where pr1027.pr_id=spj2727.pr_id

4 and sno='1' and pno='1';

Q7. Write the query for the following questions:

Student2840(snum,sname,major,level,age)

Class2840(cname,meets_at,room,fid)

Enrolled2840(snum,cname)

SQL 2-Journal Page 25


TY BSc IT –Sem V 2009

Faculty2840(fid,fname,deptid)

SQL> create table stud39(sno number primary key,sname char(10),major char(10),slevel


char(6),age number);

Table created.

SQL> insert into stud39 values('&sno','&sname','&major','&level','&age');

Enter value for sno: 01

Enter value for sname: steve

Enter value for major: IT

Enter value for level: Sr

Enter value for age: 22

old 1: insert into stud39 values('&sno','&sname','&major','&level','&age')

new 1: insert into stud39 values('01','steve','IT','Sr','22')

1 row created.

SQL> /

Enter value for sno: 02

Enter value for sname: james

Enter value for major: BMM

Enter value for level: Sr

Enter value for age: 21

old 1: insert into stud39 values('&sno','&sname','&major','&level','&age')

new 1: insert into stud39 values('02','james','BMM','Sr','21')

1 row created.

SQL> /

Enter value for sno: 03

Enter value for sname: arena


SQL 2-Journal Page 26
TY BSc IT –Sem V 2009

Enter value for major: IT

Enter value for level: Jr

Enter value for age: 19

old 1: insert into stud39 values('&sno','&sname','&major','&level','&age')

new 1: insert into stud39 values('03','arena','IT','Jr','19')

1 row created.

SQL> /

Enter value for sno: 04

Enter value for sname: annie

Enter value for major: BMM

Enter value for level: Jr

Enter value for age: 20

old 1: insert into stud39 values('&sno','&sname','&major','&level','&age')

new 1: insert into stud39 values('04','annie','BMM','Jr','20')

1 row created.

SQL> create table faculty890(fid number primary key,fname char(10),deptid varchar(3));

Table created.

SQL> insert into faculty890 values('&fid','&fname','&deptid');

Enter value for fid: 11

Enter value for fname: Adam

Enter value for deptid: 008

old 1: insert into faculty890 values('&fid','&fname','&deptid')

new 1: insert into faculty890 values('11','Adam','008')

SQL 2-Journal Page 27


TY BSc IT –Sem V 2009

1 row created.

SQL> /

Enter value for fid: 22

Enter value for fname: Sam

Enter value for deptid: 009

old 1: insert into faculty890 values('&fid','&fname','&deptid')

new 1: insert into faculty890 values('22','Sam','009')

1 row created.

SQL> /

Enter value for fid: 33

Enter value for fname: Edward

Enter value for deptid: 010

old 1: insert into faculty890 values('&fid','&fname','&deptid')

new 1: insert into faculty890 values('33','Edward','010')

1 row created.

SQL> /

Enter value for fid: 44

Enter value for fname: Noani

Enter value for deptid: 011

old 1: insert into faculty890 values('&fid','&fname','&deptid')

new 1: insert into faculty890 values('44','Noani','011')

1 row created.

SQL> create table class890(cname char(10) primary key,room varchar(4),fid number,foreign


key(fid) references faculty890(fid));

Table created.
SQL 2-Journal Page 28
TY BSc IT –Sem V 2009

SQL> insert into class890 values('&cname','&room','&fid');

Enter value for cname: IT

Enter value for room: R1

Enter value for fid: 11

old 1: insert into class890 values('&cname','&room','&fid')

new 1: insert into class890 values('IT','R1','11')

1 row created.

SQL> /

Enter value for cname: BMM

Enter value for room: R2

Enter value for fid: 22

old 1: insert into class890 values('&cname','&room','&fid')

new 1: insert into class890 values('BMM','R2','22')

1 row created.

SQL> /

Enter value for cname: BMS

Enter value for room: R3

Enter value for fid: 33

old 1: insert into class890 values('&cname','&room','&fid')

new 1: insert into class890 values('BMS','R3','33')

1 row created.

SQL> /

Enter value for cname: CS

Enter value for room: R1

Enter value for fid: 44

SQL 2-Journal Page 29


TY BSc IT –Sem V 2009

old 1: insert into class890 values('&cname','&room','&fid')

new 1: insert into class890 values('CS','R1','44')

1 row created.

SQL> create table enrolled39(snum number,cname char(10),constraint pk primary


key(snum,cname));

Table created.

SQL> insert into enrolled39 values('&snum','&cname');

Enter value for snum: 0100

Enter value for cname: BMM

old 1: insert into enrolled39 values('&snum','&cname')

new 1: insert into enrolled39 values('0100','BMM')

1 row created.

SQL> /

Enter value for snum: 0110

Enter value for cname: IT

old 1: insert into enrolled39 values('&snum','&cname')

new 1: insert into enrolled39 values('0110','IT')

1 row created.

SQL> /

Enter value for snum: 0120

Enter value for cname: BMS

old 1: insert into enrolled39 values('&snum','&cname')

new 1: insert into enrolled39 values('0120','BMS')

1 row created.

SQL> /

Enter value for snum: 0130


SQL 2-Journal Page 30
TY BSc IT –Sem V 2009

Enter value for cname: CS

old 1: insert into enrolled39 values('&snum','&cname')

new 1: insert into enrolled39 values('0130','CS')

1 row created.

1. Find the names of all juniors who are enrolled in a class taught by Mr.X.

Ans:SQL> select sname from stud39 s,class890 c,enrolled39 e,faculty890 f

2 where s.sno=e.snum and e.cname=c.cname and c.fid=f.fid and slevel='Jr' and


fname='Edward' and e.snum=s.sno;

SNAME

----------

arena

2.Find the names of all classes that either meet in room r1 or have 5 or more students
enrolled

Ans:SQL> select cname

2 from class890 s where room='R1'

3 union

4 select cname from enrolled39 group by cname having count(snum)>=5;

CNAME

----------

CS

IT

3. Print the level and average age of students for that level, for each level.

Ans:SQL> select slevel,avg(age)

from stud39

SQL 2-Journal Page 31


TY BSc IT –Sem V 2009

group by slevel;

SLEVEL AVG(AGE)

------ ----------

Jr 19.5

Sr 21.6666667

4. Print the level and average age of students for that level, for all levels except junior.

Ans:SQL> select slevel,avg(age)

from stud39

where slevel<>'Jr'

group by slevel;

SLEVEL AVG(AGE)

------ ----------

Sr 21.6666667

5.Find the names of students who are enrolled in the maximum number of classes.

Ans:SQL> select sname from student2840

where snum = (select snum from enrolled2840 group by snum having count(snum)=(select
max(count(snum)) from enrolled2840 group by snum));

SNAME

---------

noorah

6. Find the names of students who are not enrolled in any class.

Ans:SQL> select sname

from stud39

where stud39.sno not in(select e.snum

from enrolled39 e);


SQL 2-Journal Page 32
TY BSc IT –Sem V 2009

SNAME

----------

Albert

Q8. Write the query for the following questions:

Supplier(sid,sname,address)

Parts(pid,pname,color)

category(sid,pid,cost)

SQL> create table tparts(pid number(2) primary key,pname char(10),color char(10));

Table created.

SQL> insert into tparts values('&pid','&pname','&color');

select * from tparts;


SQL 2-Journal Page 33
TY BSc IT –Sem V 2009

PID PNAME COLOR

----- ---------- --------

10 nuts red

11 cluch blue

12 handle red

13 motor black

SQL> create table tcat(sid number references tsup(sid),pid number references


tparts(pid),cost number
,constraint p_1 primary key(sid,pid));

Table created.

SQL> insert into tcat values('&sid','&pid','&cost');

SQL> select * from tcat;

SID PID COST


---------- ---------- ----------
1 10 40000
2 11 38000
3 12 20000
4 13 45000

1. Find parts name for which there is some suppliers.


Ans: SQL> select pname
from tparts
where pid in(select pid from tcat);

PNAME
----------
nuts
cluch
handle
motor

2. Find the names of suppliers who supply every ‘red’ parts.

Ans: SQL> select sname


SQL 2-Journal Page 34
TY BSc IT –Sem V 2009

from tsup s,tcat c,tparts p


where s.sid=c.sid and
p.pid=c.pid and color='red';

SNAME
----------
adman
tess

3. Find sid of suppliers who charge more for same part than average cost of that part.
Ans:

SQL> select a.sid,a.pid from tcat a where cost>(select avg(cost) from tcat b group by
b.pid having b.pid=a.pid);

4. Display sid,pid pair where supplier do not supply that part.


Ans: SQL> select pid,sid
from tparts p,tsup s
where not exists(select c.sid from tcat c where c.sid=s.sid
and c.pid=p.pid);

PID SID
---------- ----------
10 2
10 3
10 4
11 1
11 3
11 4
12 1
12 2
12 4
13 1
13 2

PID SID
---------- ----------
13 3

12 rows selected.

SQL 2-Journal Page 35


TY BSc IT –Sem V 2009

Q9.

Write the query for the following questions:

book1027 (bookid,title,author,publisher,year, price ,did )

dist1027 (did,name,city)

SQL> create table book1027 (bookid number(2),title char(10),author char(10),

publisher char(10),year number(4), price number(5),did number(2));

Table created.

SQL> insert into book1027


values('&bookid','&title','&author','&publisher','&year','&price','&did');

SQL> select * from book1027;

BOOKID TITLE AUTHOR PUBLISHER YEAR PRICE DID


SQL 2-Journal Page 36
TY BSc IT –Sem V 2009

---------- ---------- ---------- ---------- ---------- ---------- ----------

1 murder Grof tmh 1981 555 1

2 mistake chetan tatahill 2009 350 2

3 idiot amir khan 2009 400 3

4 famousfive enid tmh 2005 460 1

SQL> create table dist1027 (did number(2),name char(10),city char(10));

Table created.

SQL> insert into dist1027 values('&did','&name','&city');

SQL> select * from dist1027;

DID NAME CITY

---------- ---------- ----------

1 TMG shimla

2 mango furus

3 orange nagpur

1.Display the details of all books whose price more than average price of all books.

SQL> select * from book1027

where price> (select avg(price)

from book1027);

ID TITLE AUTHOR PUBLISHER YEAR PRICE DID

---------- ---------- ---------- ---------- ---------- ---------- ----------

1 murder Grof tmh 1981 555 1

4 famousfive enid tmh 2005 460 1

2. display the details of book written by “Grof” and supplied by “TMG”

SQL 2-Journal Page 37


TY BSc IT –Sem V 2009

SQL> select * from book1027,dist1027

where author='Grof'

and name='TMG' and

book1027.did=dist1027.did;

ID TITLE AUTHOR PUBLISHER YEAR PRICE DID DID NAME CITY

---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------

1 murder Grof tmh 1981 555 1 1 TMG shimla

3. Create a view to show title, author, publisher & distributor’s name

SQL> create view vbook as

select title,author,publisher,name

from book1027,dist1027

where book1027.did=dist1027.did;

4.display the name of books having publishers last name as ‘hill’.

SQL> select title

from book1027

where publisher like '%hill';

ID TITLE AUTHOR PUBLISHER YEAR PRICE DID

---------- ---------- ---------- ---------- ---------- ---------- ----------

2 mistake chetan tatahill 2009 350 2

5.display the book which is having second highest price

SQL> select title

from book1027 where price =

(select max(price)

SQL 2-Journal Page 38


TY BSc IT –Sem V 2009

from book1027

where price < (select max(price)

from book1027));

TITLE

----------

Famousfive

Q10. Write the query for the following questions:

Customer(cid,cname,fname,area,phone)

Movie(mvno,mname,title,star,price)

Invoice(invno,mvno,cid,isuued_date,return_date)

SQL> create table customer(cid integer,cname char(10),fname char(10),area


char(15),phone integer);

Table created.

SQL> insert into customer values('&cid','&cname','&fname','&area','&phone');

Enter value for cid: 1

Enter value for cname: tweety

Enter value for fname: noorah

Enter value for area: kurla


SQL 2-Journal Page 39
TY BSc IT –Sem V 2009

Enter value for phone: 98765

old 1: insert into customer values('&cid','&cname','&fname','&area','&phone')

new 1: insert into customer values('1','tweety','noorah','kurla','98765')

1 row created.

SQL> /

Enter value for cid: 3

Enter value for cname: bunny

Enter value for fname: duaa

Enter value for area: BKC

Enter value for phone: 54321

old 1: insert into customer values('&cid','&cname','&fname','&area','&phone')

new 1: insert into customer values('3','bunny','duaa','BKC','54321')

1 row created.

SQL> /

Enter value for cid: 5

Enter value for cname: jerry

Enter value for fname: tess

Enter value for area: chembur

Enter value for phone: 554433

old 1: insert into customer values('&cid','&cname','&fname','&area','&phone')

new 1: insert into customer values('5','jerry','tess','chembur','554433')

1 row created.

SQL> /

Enter value for cid: 7

Enter value for cname: tazz

SQL 2-Journal Page 40


TY BSc IT –Sem V 2009

Enter value for fname: tasu

Enter value for area: kapadia

Enter value for phone: 776699

old 1: insert into customer values('&cid','&cname','&fname','&area','&phone')

new 1: insert into customer values('7','tazz','tasu','kapadia','776699')

1 row created.

SQL> /

Enter value for cid: 9

Enter value for cname: gujju

Enter value for fname: grishma

Enter value for area: ghatkopar

Enter value for phone: 993322

old 1: insert into customer values('&cid','&cname','&fname','&area','&phone')

new 1: insert into customer values('9','gujju','grishma','ghatkopar','993322')

1 row created.

SQL> select * from customer;

CID CNAME FNAME AREA PHONE

---------- ---------- ---------- --------------- ----------

1 tweety noorah kurla 98765

3 bunny duaa BKC 54321

5 jerry tess chembur 554433

7 tazz tasu kapadia 776699

9 gujju grishma ghatkopar 993322

SQL> create table movie(mvno integer primary key,mname char(20),title char(20),star


char(20),price integer);
SQL 2-Journal Page 41
TY BSc IT –Sem V 2009

Table created.

SQL> insert into movie values('&mvno','&mname','&title','&star','&price');

Enter value for mvno: 33

Enter value for mname: cinemax

Enter value for title: kites

Enter value for star: hrithik

Enter value for price: 300

old 1: insert into movie values('&mvno','&mname','&title','&star','&price')

new 1: insert into movie values('33','cinemax','kites','hrithik','300')

1 row created.

SQL> /

Enter value for mvno: 30

Enter value for mname: adlabs

Enter value for title: twilight

Enter value for star: edward

Enter value for price: 250

old 1: insert into movie values('&mvno','&mname','&title','&star','&price')

new 1: insert into movie values('30','adlabs','twilight','edward','250')

1 row created.

SQL> /

Enter value for mvno: 31

Enter value for mname: fame

Enter value for title: harrypotter

SQL 2-Journal Page 42


TY BSc IT –Sem V 2009

Enter value for star: daniel

Enter value for price: 350

old 1: insert into movie values('&mvno','&mname','&title','&star','&price')

new 1: insert into movie values('31','fame','harrypotter','daniel','350')

1 row created.

SQL> /

Enter value for mvno: 32

Enter value for mname: starcity

Enter value for title: wakeupsid

Enter value for star: ranbir

Enter value for price: 200

old 1: insert into movie values('&mvno','&mname','&title','&star','&price')

new 1: insert into movie values('32','starcity','wakeupsid','ranbir','200')

1 row created.

SQL>/

Enter value for mvno: 34

Enter value for mname: fame

Enter value for title: kites

Enter value for star: hrithik

Enter value for price: 300

old 1: insert into movie values('&mvno','&mname','&title','&star','&price')

new 1: insert into movie values('34','fame','kites','hrithik','300')

1 row created.

SQL 2-Journal Page 43


TY BSc IT –Sem V 2009

SQL> select * from movie;

MVNO MNAME TITLE STAR

-------- -------------------- -------------------- --------------------

PRICE

--------

33 cinemax kites hrithik

300

30 adlabs twilight edward

250

31 fame harrypotter daniel

350

MVNO MNAME TITLE STAR

-------- -------------------- -------------------- --------------------

PRICE

--------

32 starcity wakeupsid ranbir

200

34 fame kites hrithik

300

SQL> create table invoice1(invno integer,mvno integer,cid integer,issued_date


date,return_date date);

Table created.
SQL 2-Journal Page 44
TY BSc IT –Sem V 2009

SQL> insert into invoice1 values('&invno','&mvno','&cid','&issued_date','&return_date');

Enter value for invno: 60

Enter value for mvno: 30

Enter value for cid: 5

Enter value for issued_date: 14-feb-09

Enter value for return_date: 14-mar-09

old 1: insert into invoice1 values('&invno','&mvno','&cid','&issued_date','&return_date')

new 1: insert into invoice1 values('60','30','5','14-feb-09','14-mar-09')

1 row created.

SQL> /

Enter value for invno: 63

Enter value for mvno: 33

Enter value for cid: 1

Enter value for issued_date: 9-mar-09

Enter value for return_date: 9-apr-09

old 1: insert into invoice1 values('&invno','&mvno','&cid','&issued_date','&return_date')

new 1: insert into invoice1 values('63','33','1','9-mar-09','9-apr-09')

1 row created.

SQL> /

Enter value for invno: 64

Enter value for mvno: 31

Enter value for cid: 7

Enter value for issued_date: 4-aug-09

Enter value for return_date: 4-sep-09

SQL 2-Journal Page 45


TY BSc IT –Sem V 2009

old 1: insert into invoice1 values('&invno','&mvno','&cid','&issued_date','&return_date')

new 1: insert into invoice1 values('64','31','7','4-aug-09','4-sep-09')

1 row created.

SQL> /

Enter value for invno: 65

Enter value for mvno: 32

Enter value for cid: 5

Enter value for issued_date: 21-jan-09

Enter value for return_date: 21-feb-09

old 1: insert into invoice1 values('&invno','&mvno','&cid','&issued_date','&return_date')

new 1: insert into invoice1 values('65','32','5','21-jan-09','21-feb-09')

1 row created.

SQL> select * from invoice1;

INVNO MVNO CID ISSUED_DA RETURN_DA

---------- ---------- ---------- --------- ---------

60 30 5 14-FEB-09 14-MAR-09

63 33 1 09-MAR-09 09-APR-09

64 31 7 04-AUG-09 04-SEP-09

65 32 5 21-JAN-09 21-FEB-09

1. Find the name of movie having ‘a’ in the second position in their name.

select mname

from movie

where mname like'_a%';

SQL 2-Journal Page 46


TY BSc IT –Sem V 2009

MNAME

--------------------

Fame

2. Change the issue date of customer ‘01’ to 12-03-07.

update invoice1 set issued_date='12-mar-07'

where cid='01';

1 row updated.

select * from invoice1;

INVNO MVNO CID ISSUED_DA RETURN_DA

----- ---------- ---------- --------- ---------

60 30 5 14-FEB-09 14-MAR-09

63 33 1 12-MAR-07 09-APR-09

64 31 7 04-AUG-09 04-SEP-09

65 32 5 21-JAN-09 21-FEB-09

3. Find the number of movie for each type.

select title,count(mvno)

from movie

group by title;

TITLE COUNT(MVNO)

SQL 2-Journal Page 47


TY BSc IT –Sem V 2009

-------------------- -----------

wakeupsid 1

harrypotter 1

kites 2

twilight 1

4. Add one constraint which will allow area to be ghatkopar or BKC.

alter table customer add constraint aabb check (area in ('ghatkopar','BKC');

Table altered.

5. Display the customer names who are having the invoice of movies price 200.

select cname from customer,movie,invoice1

where price=200 and

customer.cid=invoice1.cid and

invoice1.mvno=movie.mvno;

CNAME

----------

jerry

SQL 2-Journal Page 48


TY BSc IT –Sem V 2009

Q11. Write the query for the following questions:

empp(eno,ename,dtjoin,city,deptid)

Dept(deptid,dname,manager)

salary(eno,basic,HRA,deduction ,tax ,pay)

SQL> create table dept890(deptid number primary key,dname char(10),mgr char(10));

Table created.

SQL> insert into dept890 values('&deptid','&dname','&mgr');

SQL> select * from dept890;

DEPTID DNAME MGR

---------- ---------- ----------

21 IT Adam

22 HR Smith

23 Sales John

24 Product Annie

SQL> create table empp98(eno number primary key,ename char(10),dtjoin date,city


char(15),deptid number);

Table created.

SQL> insert into empp98 values('&eno','&ename','&dtjoin','&city',’&deptid’);

SQL> select * from empp98;

SQL 2-Journal Page 49


TY BSc IT –Sem V 2009

ENO ENAME DTJOIN CITY DEPTID

---------- ---------- --------- --------------- ----------

1 Adam 21-FEB-09 mumbai 21

2 Smith 14-MAR-09 delhi 22

3 John 14-MAR-09 chennai 21

4 Annie 09-APR-09 mumbai 24

5 Julia 18-AUG-09 delhi 21

SQL> create table sal98(eno number,basic number,HRA number,deduction number,tax


number,pay number);

Table created.

SQL> insert into sal98 values('&eno','&basic','&HRA','&deduction','&tax','&pay);

SQL> select * from sal98;

ENO BASIC HRA DEDUCTION TAX PAY

---------- ---------- ---------- ---------- ---------- ----------

1 2500 100 10 15 2575

2 2400 100 10 15 2475

3 2700 100 10 15 2775

4 2600 100 10 15 2675

5 200 100 10 15 275

1.Display the name of the employee who get second highest basic.

SQL> select ename from empp98 e where e.eno=(select eno from sal98 where
basic>all(select basic fro

m sal98 where basic<(select max(basic) from sal98)));

ENAME

----------

SQL 2-Journal Page 50


TY BSc IT –Sem V 2009

John

2.Add a constraint to all only ‘mumbai’,’delhi’,’chennai’ as cities.

SQL> alter table empp98 add constraint ccity check(city in('mumbai','delhi','chennai'));

Table altered.

3.Display eno,ename,pay of employees who are working IT department.

SQL> select e.eno,ename,pay from empp98 e,sal98 s,dept890 d where dname='IT' and
e.eno=s.eno and e.deptid=d.deptid;

ENO ENAME PAY

---------- ---------- ----------

1 Adam 2575

3 John 2775

5 Julia 275

4.Display ename of all employees joined on same date.

SQL> select a.ename,a.dtjoin from empp98 a,empp98 b where a.dtjoin=b.dtjoin and


a.eno<>b.eno;

ENAME DTJOIN

---------- ---------

John 14-MAR-09

Smith 14-MAR-09

SQL 2-Journal Page 51


TY BSc IT –Sem V 2009

12. Write the query for the following questions:

Person(driverid,name,address)

Car(licence,model,year)

accident(reportno ,acci_ date,location )

towns(driverid ,licence )

SQL>create table tperson(driverid number(4),name char(10),address varchar(15));

Table created.

SQL>insert into tperson values('&driverid','&name','&address');

SQL> select * from tperson;

DRIVERID NAME ADDRESS

---------- ---------- ---------

1001 karisma sion

1002 kareena dadar

1003 rani vt

SQL>create table tcar(licence number(2),model char(10),year number(4));

Table created.

SQL>insert into tcar values('&licence','&model','&year');

SQL> select * from tcar;

LICENCE MODEL YEAR

---------- ---------- ----------

11 mazde 1993

SQL 2-Journal Page 52


TY BSc IT –Sem V 2009

22 bmw 2007

33 audi 2009

SQL>create table taccident(reportno number(3),acci_date date,location char(10));

Table created.

SQL>insert into taccident values('&reportno','&acci_date','&location');

SQL> select * from taccident;

REPORTNO ACCI_DATE LOCATION

---------- --------- ----------

100 02-JAN-09 mulund

102 04-OCT-08 sion

103 19-DEC-08 dadar

SQL>create table towns(driverid number(4),licence number(2));

Table created.

SQL>insert into towns values('&driverid','&licence');

SQL> select * from towns;

DRIVERID LICENCE

---------- ----------

1001 11

1002 22

1003 33

SQL>create table tparticipated(driverid number(4),reportno number(3),dam_amt


number(5));

Table created.

SQL>insert into tparticipated values('&driverid','&reportno','&dam_amt');

SQL 2-Journal Page 53


TY BSc IT –Sem V 2009

SQL> select * from tparticipated;

DRIVERID REPORTNO DAM_AMT

---------- ---------- ----------

1001 100 50000

1002 101 20000

1003 103 35000

SQL> select * from tparticipated;

DRIVERID REPORTNO DAM_AMT

---------- ---------- ----------

1001 100 50000

1002 101 20000

1003 103 35000

1. Find total number of people who owned cars that were involved in accident in
1993.

Ans. SQL>select count(driverid)

from taccident,tparticipated

where tparticipated.reportno=taccident.reportno and

to_char(acci_date,'yyyy')=1993;

COUNT(DRIVERID)

---------------

2. Delete the model belonging to ‘karisma’.

Ans. SQL>delete from tcar

SQL 2-Journal Page 54


TY BSc IT –Sem V 2009

where model='mazde' and

licence= (select licence from towns where driverid=(sel ect driverid

from tperson where name='karisma'));

1 row deleted.

3. Update the damage amount for the car with licence number ‘AA101’ in the
accident with report number ‘AR199’ to 3000.

Ans. SQL>update tparticipated set dam_amt=35000

where reportno=100 and driverid=(select driverid from towns where licence=11);

1 row updated.

4. Display the names of the drivers who owns a car with model ‘mazde’ and year
1993.

Ans. SQL>select name

from tperson,towns,tcar

where towns.driverid=tperson.driverid and

tcar.licence=town s.licence and model='mazde' and

year=1993;

NAME

----------

karisma

SQL 2-Journal Page 55


TY BSc IT –Sem V 2009

SQL 2-Journal Page 56

Вам также может понравиться