Anda di halaman 1dari 10

DBMS LAB

RISHBAH JAIN
2K12/CO/103
COE-B
PROBLEM 1
Consider the employee database of Figure, where the primary keys are underlined.
Give an expression in SQL for eah of the following !ueries.
employee (employee-name, street, city)
works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)
". Find the names of all employees who work for First #ank Corporation.
$. Find the names and ities of residene of all employees who work for First
#ank Corporation.
%. Find the names, street addresses, and ities of residene of all employees
who work for First #ank Corporation and earn more than &"','''.
(. Find all employees in the database who live in the same ities as the ompanies
for whih they work.
). Find all employees in the database who live in the same ities and on the
same streets as do their managers.
*. Find all employees in the database who do not work for First #ank Corporation.
+. Find all employees in the database who earn more than eah employee of
Small #ank Corporation.
,. -ssume that the ompanies may be loated in several ities. Find all ompanies
loated in every ity in whih Small #ank Corporation is loated.
.. Find all employees who earn more than the average salary of all employees
of their ompany.
"'. Find the ompany that has the most employees.
"". Find the ompany that has the smallest payroll.
"$. Find those ompanies whose employees earn a higher salary, on average,
than the average salary at First #ank Corporation.
SOLUTION:
1.
select employee-name
from works
where company-name ='First Bank Corporation'
2.
select e.employee-name, city
from employee e, works w
where w.company-name = 'First Bank Corporation' and
w.employee-name = e.employee-name
3.
select *
from employee
where employee-name in
select employee-name
from works
where company-name = 'First Bank Corporation' and
salary=1!!!!"
#.
select e.employee-name
from employee e, works w, company c
where e.employee-name = w.employee-name and e.city = c.city
and
w.company -name = c.company -name
$.
select %.employee-name
from employee %, employee &, mana'es (
where %.employee-name = (.employee-name and
(.mana'er-name = &.employee-name and
%.street = &.street and %.city = &.city
).
select employee-name
from works
where company-name*='First Bank Corporation'
+.
select employee-name
from works
where salary , all
select salary
from works
where company-name = '-mall Bank Corporation'"
..
select /.company-name
from company /
where select &.city
from company &
where &.company-name = /.company-name"
contains
select -.city
from company -
where -.company-name = '-mall Bank Corporation'"
0.
select employee-name
from works /
where salary, select a1' salary"
from works -
where /.company-name = -.company-name"
1!.
select company-name
from works
'ro2p 3y company-name
ha1in' co2nt distinct employee-name" ,= all
select co2nt distinct employee-name"
from works
'ro2p 3y company-name"
11.
select company-name
from works
'ro2p 3y company-name
ha1in' s2m salary" 4= all select s2m salary"
from works
'ro2p 3y company-name"
12.
select company-name
from works
'ro2p 3y company-name
ha1in' a1' salary" ,select a1' salary"
from works
where company-name = .First Bank Corporation."
PROBLEM 2
Consider the insurane database where the primary keys
are underlined. Construt the following SQL !ueries for this relational
database.
person (driver id, name, address)
car (license, model, year)
accident (report number, date, location)
owns (driver id, license)
participated (driver id, car, report number, damage amount)
". Find the total number of people who owned ars that were involved in aidents
in ".,..
$. Find the number of aidents in whih the ars belonging to ./ohn Smith.
were involved.
%. -dd a new aident to the database0 assume any values for re!uired attributes.
(. 1elete the 2a3da belonging to ./ohn Smith..
). 4pdate the damage amount for the ar with liense number .--##$'''. in
the aident with report number .-5$".+. to &%'''.
SOLUTION:
1.
select co2nt distinct name"
from accident, participated, person
where accident.report n2m3er = participated.report n2m3er
and participated.dri1er id = person.dri1er id
and date 3etween date 510.0-!!-!!5 and date 510.0-12-31
2.
select co2nt distinct *"
from accident
where e6ists
select *
from participated, person
where participated.dri1er-id = person.dri1er-id
and person.name = .7ohn -mith.
and accident.report-n2m3er = participated.report-n2m3er"
3.
insert into accident
1al2es #!!+, .2!!1-!0-!1., .Berkeley."
insert into participated
select o.dri1er-id, c.license, #!!+, 3!!!
from person p, owns o, car c
where p.name = .7ones. and p.dri1er-id = o.dri1er-id and
o.license = c.license and c.model = ./oyota.
#.
delete car
where model = 5(a8da5 and license in
select license
from person p, owns o
where p.name = 57ohn -mith5 and p.dri1er id = o.dri1er id"
$.
2pdate participated
set dama'e-amo2nt = 3!!!
where report-n2m3er = '9&210+' and dri1er-id in
select dri1er-id
from owns
where license = '99BB2!!!'"
PROBLEM 3:
Consider the following relations6
Student(snum: integer, sname: string, major: string, level: string, age: integer)
Class(name: string, meets at: string, room: string, d: integer)
Enrolled(snum: integer, cname: string)
aculty(d: integer, !name: string, deptid: integer)
"#e meaning o! t#ese relations is straig#t!orward$ !or e%ample, Enrolled #as one record
per student-class pair suc# t#at t#e student is enrolled in t#e class&
7rite the following !ueries in SQL
". Find the names of all /uniors 8level 9 /5: who are enrolled in a lass taught by
;. <eah.
$. Find the age of the oldest student who is either a =istory ma>or or enrolled in a
ourse taught by ;. <eah.
%. Find the names of all lasses that either meet in room 5"$, or have ?ve or more
students enrolled.
(. Find the names of all students who are enrolled in two lasses that meet at the
same time.
). Find the names of faulty members who teah in every room in whih some lass
is taught.
*. Find the names of faulty members for whom the ombined enrollment of the
ourses that they teah is less than ?ve.
+. For eah level, print the level and the average age of students for that level.
SOLUTION:
1.
-:;:C/ <=-/=>C/ -.-name
F&?( -t2dent -, Class C, :nrolled :, Fac2lty F
@A:&: -.sn2m = :.sn2m 9>< :.cname = C.name 9>< C.Bd =
F.id 9><
F.fname = C=./each5 9>< -.le1el = C7&5
2.
-:;:C/ (9D-.a'e"
F&?( -t2dent -
@A:&: -.maEor = CAistory5"
?& -.sn2m => -:;:C/ :.sn2m
F&?( Class C, :nrolled :, Fac2lty F
@A:&: :.cname = C.name 9>< C.Bd = F.Bd
9>< F.fname = C=./each5 "
3.
-:;:C/ C.name
F&?( Class C
@A:&: C.room = C&12.5
?& C.name => -:;:C/ :.cname
F&?( :nrolled :
F&?G% BH :.cname
A9I=>F C?G>/ *" ,= $"
#.
-:;:C/ <=-/=>C/ -.sname
F&?( -t2dent -
@A:&: -.sn2m => -:;:C/ :1.sn2m
F&?( :nrolled :1, :nrolled :2, Class C1, Class C2
@A:&: :1.sn2m = :2.sn2m 9>< :1.cname 4, :2.cname
9>< :1.cname = C1.name
9>< :2.cname = C2.name 9>< C1.meets at = C2.meets at"
$.
-:;:C/ <=-/=>C/ F.fname
F&?( Fac2lty F
@A:&: >?/ :D=-/- -:;:C/ *
F&?( Class C "
:DC:%/
-:;:C/C1.room
F&?( Class C1
@A:&: C1.Bd = F.Bd ""
).
-:;:C/ <=-/=>C/ F.fname
F&?( Fac2lty F
@A:&: $ , -:;:C/ C?G>/ :.sn2m"
F&?( Class C, :nrolled :
@A:&: C.name = :.cname
9>< C.Bd = F.Bd"
+.
-:;:C/ -.le1el, 9IF-.a'e"
F&?( -t2dent -
F&?G% BH -.le1el
PROBLEM 4
Consider t#e !ollowing sc#ema:
Suppliers(sid: integer, sname: string, address: string)
'arts(pid: integer, pname: string, color: string)
Catalog(sid: integer, pid: integer, cost: real)
". Find the pnames of parts for whih there is some supplier.
$. Find the snames of suppliers who supply every part
%. Find hte snames of suppliers who supply every red part
(. Find the sids of suppliers who supply only red parts
). Find the sids of suppliers who supply a red part and a green part.
*. Find the sids of suppliers who harege more for some part then the average ost of that part
8averaged over all suppliers who supply that part:
SOLUTION:
1.
select %.pname
fromparts %, catalo' c
where p.pid = c.cid
2.
select s.sname
from s2ppliers s
where not e6ists select * from parts p" e6cept
select c.pid from catalo' c where c.sid = s.sid""
3.
-elect -.sname
From s2ppliers s
where not e6ists
select * from parts p where p.color = 'red'" J all red
parts
e6cept
select c.pid from catalo' c, parts p where c.sid = s.sid
and c.pid = p.pid and p.color = 'red'""
#.
select distinct c.sid
fromcatalo' c, parts p
where not e6ists select * from parts p where p.pid = c.pid and
p.color 4, 'red'"
$.
select distinct c.sid
from catalo' c, parts p
where c.pid = p.pid and p.color = 'red'
=>/:&-:C/
select distinct c.sid
from catalo' c, parts p
where c.pid = p.pid and p.color = ''reen'
).
select distinct c.sid
from catalo' c
where c.cost , select a1'c1.cost" from catalo' c1 where
c1.pid = c.pid"

Anda mungkin juga menyukai