0% found this document useful (0 votes)
66 views

The Database Scheme Consists of Four Tables

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

The Database Scheme Consists of Four Tables

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL EXCERSISE

The database scheme consists of four tables:


Product(maker, model, type)
PC(code, model, speed, ram, hd, cd, price)
Laptop(code, model, speed, ram, hd, screen, price)
Printer(code, model, color, type, price)
The Product table contains data on the maker, model number, and type of product ('PC', 'Laptop',
or 'Printer'). It is assumed that model numbers in the Product table are unique for all makers and
product types. Each personal computer in the PC table is unambiguously identified by a unique
code, and is additionally characterized by its model (foreign key referring to the Product table),
processor speed (in MHz) – speed field, RAM capacity (in Mb) - ram, hard disk drive capacity (in
Gb) – hd, CD-ROM speed (e.g, '4x') - cd, and its price. The Laptop table is similar to the PC table,
except that instead of the CD-ROM speed, it contains the screen size (in inches) – screen. For each
printer model in the Printer table, its output type (‘y’ for color and ‘n’ for monochrome) – color
field, printing technology ('Laser', 'Jet', or 'Matrix') – type, and price are specified.

Q1)
Find the model number, speed and hard drive capacity for all the PCs with prices below $500.
Result set: model, speed, hd.

--Solution:

select model, speed, hd from PC (nolock) where pc.price < 500

Q2) List all printer makers. Result set: maker.

--Solution:

select distinct maker from product p1 where type = 'printer'

Q3) Find the model number, RAM and screen size of the laptops with prices over $1000.

--Solution:

Select model, ram, screen from laptop where price > 1000

Q4) Find all records from the Printer table containing data about color printers.

--Solution:

Select * from printer where color = 'y'

Q5) Find the model number, speed and hard drive capacity of PCs cheaper than $600 having a 12x
or a 24x CD drive.

--Solution:

Select model, speed, hd from pc where price < 600 and cd in ('12x', '24x')
SQL EXCERSISE
Q6) For each maker producing laptops with a hard drive capacity of 10 Gb or higher, find the
speed of such laptops. Result set: maker, speed.

--Solution:

select distinct p.maker, l.speed from product p inner join laptop l on p.model = l.model where l.hd >=
10

Q7) Get the models and prices for all commercially available products (of any type) produced by
maker B.

--Solution:

select product.model, laptop.price from product inner join laptop on product.model = laptop.model
where product.maker = 'B'

union

select product.model, printer.price from product inner join printer on product.model =


printer.model where product.maker = 'B'

union

select product.model, pc.price from product inner join pc on product.model = pc.model where
product.maker = 'B'

Q8) Find the makers producing PCs but not laptops.

--Solution:

select distinct maker from product where type = 'PC' and maker not in (select maker from product
where type = 'laptop')

Q9) select distinct maker from product where type = 'PC' and maker not in (select maker from
product where type = 'laptop')

--Solution:

select distinct maker from product inner join pc on product.model = pc.model and pc.speed >= 450

Q10) Find the printer models having the highest price. Result set: model, price.

--Solution:

select distinct model, price from printer where price = (select max(price) from printer)

Q11) Find out the average speed of PCs.

--Solution:

Select avg(speed) from pc


SQL EXCERSISE
Q12) Find out the average speed of the laptops priced over $1000.

--Solution:

Select avg(speed) from laptop where price > 1000

Q13) Find out the average speed of the PCs produced by maker A.

--Solution:

Select avg(speed) from pc inner join product on pc.model = product.model where product.maker
= 'A'

Q14) For the ships in the Ships table that have at least 10 guns, get the class, name, and country.

--Solution:

select maker, type

from product

where maker in ( select maker from (select maker, type from product group by maker, type) x

group by x.maker

having count(type) = 1

group by maker, type

having count(model) > 1

Q15) Get hard drive capacities that are identical for two or more PCs.
Result set: hd.

--Solution:

Select hd from pc

group by hd

having count(hd) >= 2


SQL EXCERSISE
Q16) Get pairs of PC models with identical speeds and the same RAM capacity. Each resulting pair
should be displayed only once, i.e. (i, j) but not (j, i).
Result set: model with the bigger number, model with the smaller number, speed, and RAM.

--Solution:

select distinct p1.model, p2.model, p1.speed, p1.ram

from pc p1

inner join pc p2

on p1.speed = p2.speed

and p1.ram = p2.ram

and (p1.model > p2.model)

order by p1.speed, p1.ram

Q17) Get the laptop models that have a speed smaller than the speed of any PC.
Result set: type, model, speed.

--Solution:

Select distinct 'Laptop' as Type, model, speed from laptop where speed < (select min(speed) from
pc)

Q18) Find the makers of the cheapest color printers.


Result set: maker, price.

--Solution:

select distinct product.maker, printer.price

from product

inner join printer

on product.model = printer.model

where printer.price = (select min(price) from printer where color = 'y')

and printer.color = 'y'

Q19) For each maker having models in the Laptop table, find out the average screen size of the
laptops he produces.
Result set: maker, average screen size.

--Solution:

SELECT product.maker, avg(LAPTOP.screen) as [Average Screen Size]

FROM LAPTOP

INNER JOIN PRODUCT on laptop.model = product.model

group by product.maker

Q20) Find the makers producing at least three distinct models of PCs.
Result set: maker, number of PC models.
SQL EXCERSISE
--Solution:

select maker, count(distinct model) as Count_Model

from product where type = 'PC' group by maker

having count(distinct product.model) >= 3

Q21) Find out the maximum PC price for each maker having models in the PC table. Result set:
maker, maximum price.

--Solution:

select product.maker, max(pc.price) from product

inner join pc

on product.model = pc.model

group by product.maker

Q22) For each value of PC speed that exceeds 600 MHz, find out the average price of PCs with
identical speeds.
Result set: speed, average price.

--Solution:

select speed, avg(price) from pc

where speed > 600

group by speed

You might also like