6
D Sub Sub Reg. Univ TABASE SYSTEMS  Assignment-3  SQL itted To: Prof. Shoaib Farooq itt ed By: Far han Sal eem No: L1F07BSCS2007 rsity of Central Punjab

Assignment 3(SQL)

Embed Size (px)

Citation preview

8/14/2019 Assignment 3(SQL)

http://slidepdf.com/reader/full/assignment-3sql 1/6

D

Sub

Sub

Reg.

Univ

TABASE SYSTEMS 

 Assignment-3

 SQL

itted To: Prof. Shoaib Farooq

itted By: Farhan Saleem

No : L1F07BSCS2007

rsity of Central Punjab

8/14/2019 Assignment 3(SQL)

http://slidepdf.com/reader/full/assignment-3sql 2/6

Question # 1)

1. Select title from product where price>20;2. Select count(catagory) from product where catagory=’COLD

DRINK’;

3. Select title from product where catagory='BISCUITS' and

price>=8;

4. select title,price*qty as TotalPrice from product;

5. select title from product where price=(select min(price) from

product);6. select sum(price) from product;

7. select sum(price) from product group by (catagory);

8. select avg(price) from product group by (catagory),(psize);

9. Select sum(qty) from product group by (catagory) HAVING

sum(qty)>1000;

10. SELECT PSIZE,SUM(QTY) FROM PRODUCT GROUP

BY PSIZE;

Question # 2)

1. (Maker because cars have more than one maker, Color 

because cars have more than one color,HrPower because

cars have many horsePower speeds,Price because cars

have different prices according to their brands or 

enginePower wise etc.)

2. (Title because different Books have different Titles, Course

because different Books are required for course,Publisher 

8/14/2019 Assignment 3(SQL)

http://slidepdf.com/reader/full/assignment-3sql 3/6

because Books are published by many Publishers,Price

because Books have different prices)

3. (JOB is best column to choose it for group by clause beause

it differentiate the employees job wise which is moreprominent as compared to others).

Question # 3)

1. CONSTRAINTS

PRIMARY KEYS

alter table Person add constraint person_name_pk

primary key(name);

alter table Dealer add constraint dealer_dealerid_pk

primary key(dealerid);

alter table Car add constraint Car_carid_pk primary

key(carid);

alter table Location add constraint Location_locid_pk

primary key(locid);

FOREIGN KEYS

alter table Person add constraint person_carid_fk foreignkey(carid) references car(carid);

alter table Person add constraint person_dealerid_fk

foreign key(dealerid) references Dealer(dealerid);

8/14/2019 Assignment 3(SQL)

http://slidepdf.com/reader/full/assignment-3sql 4/6

alter table Dealer add constraint Dealer_locid_fk foreign

key(locid) references Location(locid);

alter table Car add constraint Car_locid_fk foreignkey(locid) references Location(locid);

2. VIEW ALL CONSTRAINTS

SELECT constraint_name, column_nameFROM user_cons_columns WHERE table_name ='person';

SELECT constraint_name, column_nameFROM user_cons_columns WHERE table_name ='dealer';

SELECT constraint_name, column_nameFROM user_cons_columns WHERE table_name = 'car';

SELECT constraint_name, column_nameFROM user_cons_columns WHERE table_name ='location';

3. (5) Dummy EntriesPerson(1) insert into person values('usman',1,2,9600000,'17-

JAN-2006');(2) insert into person values('ali',5,1,4550000,'14-AUG-

1947');(3) insert into person values('arsal',3,3,970000,'28-SEP-

2007');(4) insert into person values('arshad',4,1,1500000,'09-

FEB-2009');

8/14/2019 Assignment 3(SQL)

http://slidepdf.com/reader/full/assignment-3sql 5/6

(5) insert into person values('umer',2,4,2080000,'29-JAN-2010');

Dealer (1) insert into dealer values(1,'hameed',1,50000);

(2) insert into dealer values(2,'gul',2,60000);(3) insert into dealer values(3,'ahmad',3,70000);(4) insert into dealer values(4,'sana',4,80000);(5) insert into dealer values(5,'farha',5,80000);Car (1) insert into car values(1,'bmw','x700',1,'9000000');(2) insert into car values(2,'diablo','sv2',4,'2000000');(3) insert into car values(3,'suzuki','cultus',5,'900000');

(4) insert into car values(4,'mercedes','benz',3,'1450000');

(5) insert into car values(5,'honda','accord',2,'4500000');Location(1) insert into location values(1,'karachi clifton');(2) insert into location values(2,'Lahore cantt');(3) insert into location values(3,'Lahore Defence');(4) insert into location values(4,'Islamabad');(5) insert into location values(5,'multan');

4. select company,locationname from car,location wherecar.locid=location.locid and company='bmw' andlocationname='karachi clifton';

5. select dealername from dealer,car,person wheredealer.dealerid=person.dealerid andcar.carid=person.carid and company=(select companyfrom car where company='diablo' and model='sv2');

6. select name,locationname from person,location,car whereperson.carid=car.carid and car.locid=location.locid andlocationname='Lahore cantt';

7. select dealername,buyingdate from dealer,person whereperson.dealerid=dealer.dealerid and buyingdate='17-JAN-2006';

8/14/2019 Assignment 3(SQL)

http://slidepdf.com/reader/full/assignment-3sql 6/6

8. select company,model from person,car whereperson.carid=car.carid and buyingdate='17-JAN-2006';

9. select name,dealername,buyingdate,locationname fromperson,dealer,location where

person.dealerid=dealer.dealerid anddealer.locid=location.locid and dealername='hameed' andbuyingdate='14-AUG-1947' and locationname='karachiclifton';

10. select count(company) from car,person whereperson.carid=car.carid and company='suzuki' andname='arshad';

11. select name,company,model,priceandtax as

totalprice,dealername,locationname fromperson,dealer,car,location where person.carid=car.caridand car.locid=location.locid andperson.dealerid=dealer.dealerid and name='arshad' andcompany=(select company from car wherecompany='mercedes' and model='benz') and priceandtax=1500000 and dealername='hameed' andlocationname='Lahore Defence';