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

# REF 1.

0

Lebanese International University
School of Engineering
Department of Computer and Communication Engineering
CENG440: Introduction to Database Systems

Section ID: Enter your Section ID here

Assignment I: Introduction and Relational Algebra

Chapter Coverage: Chapter 6

Submitted by: Enter your name here
IdentiIication Number: Enter your Student ID here

Instructor: Enter Instructor Name here

Date: Click here to enter a date.

REF 1.0
!roblem 1: Consider the Iollowing database schema

Product (maker, model, type)
PC (model, speed, ram, hd, price)
Laptop (model, speed, ram, hd, screen, price)
Printer (model, color, type, price)

a) Find the mode l number, speed, and hard-disk size Ior all PC`s whose price is under
\$1000.
SELECT model,speed,hd
FRJM PC
WHERE price < 1000 ;

b) Find the manuIacturers oI printers
SELECT maker
FRJM Product
WHERE TYPE = 'printer' ;

c) Find the model number, memory size, and screen size Ior laptops costing more than
\$1500
SELECT model, ram,screen
FRJM Laptop
WHERE price 1500 ;

d) Find all the tuples in the printer relation Ior color printers. Remember that color is a
Boolean-valued attribute.
SELECT
FRJM Printer
WHERE color ;

REF 1.0
!roblem 2: consider the Iollowing database schema
Classes (class, type, country, numGuns, bore, displacement)
Ships (name, class, launched)
Battles (name, date)
Outcomes (ship, battle, result) //Battle outcomes

a) List all the ships mentioned in the database. (Remember that all these ships may not
appear in the Ships relation)
SELECT name shipName
FRJM Ships

UNIJN

SELECT ship shipName
FRJM Jutcomes ;

b) Find those countries that have both battleships and battlecruisers

SELECT C1.country
FRJM Classes C1,Classes C2
WHERE C1.country = C2.country
AND C1.type = 'bb'
AND C2.type = 'bc' ;

c) Find those ships that were damaged in one battle, but later Iought in anather.

SELECT J1.ship
FRJM Jutcomes J1, Battles B1
WHERE J1.battle = B1.name AND J1.result = 'damaged'
AND EXISTS
(SELECT B2.date
FRJM Jutcomes J2,Battles B2
WHERE J2.battle=B2.name
AND J1.ship = J2.ship AND B1.date < B2.date
) ;

d) Find those battles with at least three ships oI the same country.
SELECT J.battle
FRJM Jutcomes J, Ships S , Classes C
WHERE J.ship = S.name AND S.class = C.class
GRJUP BY C.country, J.battle
HAVING CJUNT(J.ship) = 3;

REF 1.0
!roblem 3: Consider the Iollowing database schema

Product (maker, model, type)
PC (model, speed, ram, hd, price)
Laptop (model, speed, ram, hd, screen, price)
Printer (model, color, type, price)

Use at least one subquery in your answer. Write each query in two signiIicantly diIIerent ways
(Using diIIerent set oI operators EXISTS, IN, ALL and ANY)

a) Find the makers oI PC`s with a speed oI at least 3.0

SELECT DISTINCT maker
FRJM Product
WHERE model IN
(SELECT model
FRJM PC
WHERE speed = 3.0
);

SELECT DISTINCT R.maker
FRJM Product R
WHERE EXISTS
(SELECT P.model
FRJM PC P
WHERE P.speed = 3.0
AND P.model =R.model
);

b) Find the printers with the highest price.
SELECT P1.model
FRJM Printer P1
WHERE P1.price = ALL
(SELECT P2.price
FRJM Printer P2
) ;

SELECT P1.model
FRJM Printer P1
WHERE P1.price IN
(SELECT MAX(P2.price)
FRJM Printer P2
) ;

REF 1.0
c) Find the laptops whose speed is slower than that oI any PC.
SELECT L.model
FRJM Laptop L
WHERE L.speed < ANY
(SELECT P.speed
FRJM PC P
) ;

SELECT L.model
FRJM Laptop L
WHERE EXISTS
(SELECT P.speed
FRJM PC P
WHERE P.speed = L.speed
) ;

d) Find the model number oI the item (PC, laptop or printer) with the highest price

SELECT model
FRJM
(SELECT model,price
FRJM PC

UNIJN

SELECT model,price
FRJM Laptop

UNIJN

SELECT model,price
FRJM Printer
) M1
WHERE M1.price = ALL
(SELECT price
FRJM PC

UNIJN

SELECT price
FRJM Laptop

UNIJN

SELECT price
FRJM Printer
) ;

REF 1.0
SELECT model
FRJM
(SELECT model, price
FRJM PC

UNIJN

SELECT model, price
FRJM Laptop

UNIJN

SELECT model, price
FRJM Printer
) M1
WHERE M1.price IN
(SELECT MAX(price)
FRJM
(SELECT price
FRJM PC

UNIJN

SELECT price
FRJM Laptop

UNIJN

SELECT price
FRJM Printer
) M2
) ;

e) Find the maker oI the color printer with the lowest price
SELECT R.maker
FRJM Product R,
Printer T
WHERE R.model =T.model
AND T.price <= ALL
(SELECT MIN(price)
FRJM Printer
);

SELECT R.maker
FRJM Product R,
Printer T1
WHERE R.model =T1.model
AND T1.price IN
(SELECT MIN(T2.price)
FRJM Printer T2
);

REF 1.0
I) Find the maker(s) oI the PC(s) with the Iastest processor among all those PC`s that have
the smallest amount oI RAM.
SELECT R1.maker
FRJM Product R1,
PC P1
WHERE R1.model=P1.model
AND P1.ram IN
(SELECT MIN(ram)
FRJM PC
)
AND P1.speed = ALL
(SELECT P1.speed
FRJM Product R1,
PC P1
WHERE R1.model=P1.model
AND P1.ram IN
(SELECT MIN(ram)
FRJM PC
)
);

SELECT R1.maker
FRJM Product R1,
PC P1
WHERE R1.model=P1.model
AND P1.ram =
(SELECT MIN(ram)
FRJM PC
)
AND P1.speed IN
(SELECT MAX(P1.speed)
FRJM Product R1,
PC P1
WHERE R1.model=P1.model
AND P1.ram IN
(SELECT MIN(ram)
FRJM PC
)
);

REF 1.0
!roblem 4: consider the Iollowing database schema
Classes (class, type, country, numGuns, bore, displacement)
Ships (name, class, launched)
Battles (name, date)
Outcomes (ship, battle, result) //Battle outcomes

Use at least one subquery in your answer. Write each query in two signiIicantly diIIerent ways
(Using diIIerent set oI operators EXISTS, IN, ALL and ANY)

a) Find the countries whose ships had the largest number oI guns.

SELECT C.country
FRJM Classes C
WHERE numGuns IN
(SELECT MAX(numGuns)
FRJM Classes
);

SELECT C.country
FRJM Classes C
WHERE numGuns = ALL
(SELECT numGuns
FRJM Classes
);

b) Find the classes oI ships, at least one oI which was sunk in a battle.
SELECT DISTINCT C.class
FRJM Classes C,
Ships S
WHERE C.class = S.class
AND EXISTS
(SELECT ship
FRJM Jutcomes J
WHERE J.result='sunk'
AND J.ship = S.name
) ;

SELECT DISTINCT C.class
FRJM Classes C,
Ships S
WHERE C.class = S.class
AND S.name IN
(SELECT ship
FRJM Jutcomes J
WHERE J.result='sunk'
) ;

REF 1.0

c) Find the names oI the ships with a 16-inch bore.
SELECT S.name
FRJM Ships S
WHERE S.class IN
(SELECT class
FRJM Classes C
WHERE bore=16
) ;

SELECT S.name
FRJM Ships S
WHERE EXISTS
(SELECT class
FRJM Classes C
WHERE bore =16
AND C.class = S.class
);

d) Find the battles in which ships oI the Kongo class participated.
SELECT J.battle
FRJM Jutcomes J
WHERE J.ship IN
(SELECT name
FRJM Ships S
WHERE S.Class ='Kongo'
);

SELECT J.battle
FRJM Jutcomes J
WHERE EXISTS
(SELECT name
FRJM Ships S
WHERE S.Class ='Kongo'
AND S.name = J.ship
);

REF 1.0
e) Find the names oI the ships whose number oI guns was the largest Ior those oI the same
bore.

SELECT S.name
FRJM Ships S, Classes C
WHERE S.Class = C.Class
AND numGuns = ALL
(SELECT numGuns
FRJM Ships S2,Classes C2
WHERE S2.Class = C2.Class AND C2.bore = C.bore
) ;
SELECT S.name
FRJM Ships S, Classes C
WHERE S.Class = C.Class
AND numGuns IN
(SELECT MAX(numGuns)
FRJM Ships S2, Classes C2
WHERE S2.Class = C2.Class AND C2.bore = C.bore
) ;

REF 1.0
!roblem 5: Consider the Iollowing database schema

Product (maker, model, type)
PC (model, speed, ram, hd, price)
Laptop (model, speed, ram, hd, screen, price)
Printer (model, color, type, price)

a) Find the average speed oI PC`s

SELECT AVG(speed) AS Avg_Speed
FRJM PC ;

b) Find the average speed oI laptops costing over \$1000

SELECT AVG(speed) AS Avg_Speed
FRJM Laptop
WHERE price 1000 ;

c) Find the average price oI PC`s made by manuIacturer 'A

SELECT AVG(P.price) AS Avg_Price
FRJM Product R,
PC P
WHERE R.model=P.model
AND R.maker='A' ;

d) Find the average price oI PC`s and laptops made by manuIacturer 'D

SELECT AVG(M.price) AS Avg_Price
FRJM
(SELECT P.price
FRJM Product R,
PC P
WHERE R.model = P.model
AND R.maker = 'D'

UNIJN ALL

SELECT L.price
FRJM Product R,
Laptop L
WHERE R.model = L.model
AND R.maker = 'D'
) M ;

REF 1.0

e) Find, Ior each diIIerent speed, the average price oI a PC

SELECT SPEED,
AVG(price) AS AVG_PRICE
FRJM PC
GRJUP BY speed ;

I) Find Ior each manuIacturer, the average screen size oI its laptops

SELECT R.maker,
AVG(L.screen) AS Avg_Screen_Size
FRJM Product R,
Laptop L
WHERE R.model = L.model
GRJUP BY R.maker ;

g) Find the manuIacturers that make at least three diIIerent models oI PC

SELECT R.maker
FRJM Product R,
PC P
WHERE R.model = P.model
GRJUP BY R.maker
HAVING CJUNT(R.model) =3 ;

You can also write

SELECT maker
FRJM Product
WHERE type='pc'
GRJUP BY maker
HAVING CJUNT(model) =3 ;

h) Find Ior each manuIacturer who sells PC`s the maximum price oI a PC

SELECT R.maker,
MAX(P.price) AS Max_Price
FRJM Product R,
PC P
WHERE R.model = P.model
GRJUP BY R.maker ;

REF 1.0
i) Find Ior each speed oI PC above 2.0, the average price

SELECT speed,
AVG(price) AS Avg_Price
FRJM PC
WHERE speed 2.0
GRJUP BY speed ;

j) Find the average hard disk size oI a PC Ior all those manuIacturers that make printers.

SELECT AVG(P.hd) AS Avg_HD_Size
FRJM Product R,
PC P
WHERE R.model = P.model
AND R.maker IN
(SELECT maker
FRJM Product
WHERE type = 'printer'
) ;

!roblem 6: Consider the Iollowing database schema

Product (maker, model, type)
PC (model, speed, ram, hd, price)
Laptop (model, speed, ram, hd, screen, price)
Printer (model, color, type, price)

a) Using two INSERT statements, store in the database the Iact that PC model 1100 is made
by manuIacturer C, has speed 3.2, RAM 1024, hard disk 180 and sells Ior \$2499
INSERT
INTJ Product VALUES
(
'C' ,
'1100',
'pc'
) ;
INSERT
INTJ PC VALUES
(
'1100',
3.2 ,
1024,180,2499
) ;

REF 1.0

b) Insert the Iacts that Ior every PC there is a laptop with the same manuIacturer, speed,
RAM, and hard drive, a 17-inch screen, a model number 1100 greater and a price \$500
more.
INSERT
INTJ Product
SELECT make , model+1100, 'laptop'
FRJM Product
WHERE type = 'pc' ;
INSERT
INTJ Laptop
SELECT model+1100,
speed ,
ram ,
hd ,
17 ,
price+500
FRJM PC ;

c) Delete all PC`s with less than 100 gigabytes oI hard drive.
DELETE
FRJM PC
WHERE hd < 100 ;

d) Delete all laptops made by a manuIacturer that doesn`t make printers
DELETE
FRJM Laptop L
WHERE L.model IN
(SELECT R2.model
FRJM Product R2
WHERE R2.maker IN
(SELECT DISTINCT R.maker
FRJM Product R
WHERE R.maker NJT IN
(SELECT R2.maker
FRJM Product R2
WHERE R2.type = 'printer'
)
)
) ;
DELETE
FRJM PRJDUCT R3
WHERE R3.model IN
(SELECT R2.model
FRJM Product R2
WHERE R2.maker IN
(SELECT DISTINCT R.maker
FRJM Product R
WHERE R.maker NJT IN
REF 1.0
(SELECT R2.maker
FRJM Product R2
WHERE R2.type = 'printer'
)
)
)
AND R3.type = 'laptop';

e) ManuIacturer A buys manuIacturer B, change all products made by B so they are now
UPDATE Product
SET maker = 'A'
WHERE maker = 'B' ;

I) For each PC, double the amount oI RAM and add 60 gigabytes o the hard drive.
UPDATE PC
SET ram = ram2,
hd =hd +60 ;

g) For each laptop made by manuIacturer B, add one inch to the screen size and subtract
\$100 Irom the price.
UPDATE Laptop L
SET L.screen = L.screen+1,
L.price =L.price -100
WHERE L.model IN
(SELECT R.model
FRJM Product R
WHERE R.maker = 'B'
) ;