You are on page 1of 23

DBMS ASSIGNMENT 1

Name: Ritesh Verma


Roll Number: 31401219005
Stream :BCA
Year: 2nd
Semester: 4th
Subject : DBMS Practical (BCAN 491)
College: Techno India College of Technology
Submitted to : Aloke Bera Sir.

TABLE CREATION:
1st Table: STATE NAME

Code for creating table STATE NAME:


create table state_name (ccity varchar2(8),state varchar2(15), primary key(ccity));

Code for Inserting Vales to table STATE NAME:


insert into state_name values('mysore','karnataka');

insert into state_name values('kolkata’, ‘westbengal');

insert into state_name values('pune','maharastra');

insert into state_name values('chennai','tamilnadu');

insert into state_name values('indore','madhyapradesh');

Code for displaying table STATE NAME:


select * from state_name;
2nd Table: CUSST

Code for creating table CUSST:


create table custt(c_id varchar2(6) not null, cname varchar2(10), ccity varchar2(8), primary
key(c_id),foreign key(ccity)references state_name(ccity));

Code for Inserting Vales to table CUSTT:


insert into custt values('c1','pradip','mysore');

insert into custt values('c2','tushar','pune');

insert into custt values('c3','aastik','kolkata');

insert into custt values('c4','arpan','chennai');

insert into custt values('c5','anumita','indore');

Code for displaying table CUSTT:


select * from custt;
3rd Table: PRODD

Code for creating table PRODD:


create table prodd(p_id varchar2(6) not null, pname varchar2(6), pcost number(4,2), pprofit
number(3), primary key(p_id));

Code for Inserting Vales to table PRODD:


insert into prodd values('p3','pen',20.50,20);

insert into prodd values('p2','floppy',30,12);

insert into prodd values('p1','pencil',5.25,null);

Code for displaying table PRODD:


select * from prodd;
4th Table: SALE_DETAIL

Code for creating table SALE DETAIL:


create table sale_detail(c_id varchar2(6), p_id varchar2(6), sale number(3), saledt date, primary
key(c_id,p_id,saledt), foreign key(c_id)references custt(c_id), foreign key(p_id)references
prodd(p_id));

Code for Inserting Vales to table SALE DETAIL:


insert into sale_detail values('c1','p3',2,'14-jul-2008');

insert into sale_detail values('c3','p2',10,'15-jul-2008');

insert into sale_detail values('c2','p3',1,'14-jul-2008');

insert into sale_detail values('c1','p1',5,'14-jul-2008');

insert into sale_detail values('c1','p3',3,'20-aug-2008');

insert into sale_detail values('c4','p3',2,'14-nov-2008');

insert into sale_detail values('c5','p2',2,'18-sep-2008');

Code for displaying table SALE DETAIL:


select * from sale_detail;
QUERIES:
1. Write a query to display pname of all records , sort all records by pname.

(Ans) select pname from prodd order by pname;

2. Write a query to display cname and ccity of all records, sort by ccity descending order.

(Ans) select cname, ccity from custt order by ccity desc;


3. Write a query to display cname, and ccity who lives in mysore.

(Ans) select cname, ccity from custt where ccity ='mysore';

4. Write a query to display cname, pname, sale, saledt of all customers.

(Ans) select c.cname, p.pname, s.sale, s.saledt from custt c, prodd p, sale_detail s where
c.c_id=s.c_id and p.p_id=s.p_id;
5. Write a query to display cname who has purchased pen.

(Ans) select c.cname from custt c, prodd p, sale_detail s where c.c_id=s.c_id and p.p_id=s.p_id
and p.pname='pen';

6. Write a query to display saledt, and total sale on the data labelled as sale of all items sold
after 01-sept-08.

(Ans) select s.saledt, p.pcost*s.sale amount from sale_detail s , prodd p where s.p_id=p.p_id
and saledt > '01-sep-08';
7. Write a query to display satedt and total sale on the data labelled as sale of all items other
than floppy.

(Ans) select s.saledt, p.pname, p.pcost*s.sale amount from sale_detail s, prodd p where
s.p_id=p.p_id and p.pname !='floppy';

8. Write a query to find the product Id in sale_detail table.

(Ans) select p_id from sale_detail;


9. Write a query to find the product Id in sale_detail table to prevent duplicate rows.

(Ans) select distinct p_id from sale_detail;

10. Write a query to display cname and ccity of all the customers who lived in kolkata or
chennai.

(Ans) select cname , ccity from custt where ccity in ('kolkata','chennai');


11. Write a query to display the pname and pcost of all the customers where pcost lies between
5 and 25.

(Ans) select pname, pcost from prodd where pcost between '5' and '25';

12. Write a query to display distinct cid and pid is p3 or sale date is 18-sept-08

(Ans) select distinct c_id , p_id ,saledt from sale_detail where p_id ='p3' or saledt = ('18-sep-08');
13. Write a query to display cname, pid, and saledt of all those customers whose cid is in c1 or
c2 or c4 or c5.

(Ans) select c.cname, s.p_id ,s.saledt from custt c, sale_detail s where c.c_id =s.c_id and c.c_id in
('c1','c2','c4','c5');

14. Write a query to display all cname which includes two ‘’a’’ in the name.

(Ans) select cname from custt where cname like 'a%';


15. Write a query to display the total count of customers.

(Ans) select count (c_id) from custt;

16. Write a query to display average value of product cost rounded to 2 nd decimal places.

(Ans) select round (avg(pcost),2) from prodd;


17. Write a query to display pname with total sale detail in descending order.

(Ans) select p.pname , sum(s.sale) from prodd p, sale_detail s where p.p_id=s.p_id group by
p.pname order by p.pname desc;

18. Write a query to display pname, saledt and total amount collected for the product.

(Ans) select p.pname, s.saledt, sum(s.sale*p.pcost) from prodd p, sale_detail s where


p.p_id=s.p_id group by p.pname, s.saledt;
19. Write a query to display the pname, sale date and total amount collected on date wise sale
of pen and pencil.

(Ans) select p.pname, s.saledt, sum(s.sale*p.pcost) from prodd p, sale_detail s where


p.p_id=s.p_id group by p.pname, s.saledt having p.pname in('pen','pencil') order by s.saledt;

20. Write a query to display the pname, sale date and total amount collected on date wise sale
of pen and pencil. Arrange them in ascending order of total collected amount.

(Ans) select p.pname, s.saledt, sum(s.sale*p.pcost) from prodd p, sale_detail s where


p.p_id=s.p_id group by p.pname, s.saledt having p.pname in('pen','pencil') order by
sum(s.sale*p.pcost);
21. Write a query to display the pname, sale date and total amount collected on date wise sale
of all product. Arrange them in ascending order of total collected amount.

(Ans) select p.pname, s.saledt, sum(s.sale*p.pcost) from prodd p, sale_detail s where


p.p_id=s.p_id group by p.pname, s.saledt order by sum(s.sale*p.pcost);

22. Write a query to display sale date and total sale date which was sold after “14-jil-08”.

(Ans) select saledt, sum(sale) from sale_detail group by saledt having saledt >'14-jul-08' order
by saledt;
23. Write a query to display the customer name who belongs to those places whose name is
having character I or p.

(Ans) select cname from custt where ccity like 'i%' union select cname from custt where ccity
like 'p%';

24. Write a query to display the customer name who belongs to a city whose name contains
character “c” and whose name contains character “a”.

(Ans) select cname from custt where cname like 'a%' intersect select cname from custt where
ccity like 'c%';
25. Write a query to display the customer name who does not belong to pune.

(Ans) select cname,ccity from custt where ccity!='pune';

26. Updation of row where cid=c3 becomes cname “Tushar”.

(Ans) update custt set cname='Tushar' where c_id='c3';

Select * from custt;


27. Updation of row where cid=c3 becomes cname “aastik”.

(Ans) update custt set cname='aastik' where c_id='c3';

Select * from custt;

28. Updation of row where cid=c2 becomes cname “aastik”.

(Ans) update custt set cname='aastik' where c_id='c2';


29. Updation of row where cid=c2 and ccity=pune becomes cname “tushar”.

(Ans) update custt set cname='tushar' where c_id='c2' and ccity='pune';

30. Write a query to display customer name in upper case, lower case and title case from custt
table where customer name=’pradip’.

(Ans) select cname, lower(cname), upper(cname), initcap(cname) from custt where


cname='pradip';
31. Write a query to display the first 3 character of customer name.

(Ans) select substr(cname,1,3) from custt;

32. Write a query to display the position of character m in the cname of the customer whose
name is “anumita”.

(Ans)

select instr(cname,'m') from custt where cname='anumita';


33. Write a query to display the length of all customer name.

(Ans) select cname, length(cname) from custt;

34. Write a query to display the total count of customers.

(Ans) select count(*) from custt;


35. Write a query to display the minimum cost and maximum cost from products.

(Ans) select min(pcost), max(pcost) from prodd;

36. Write a query to display average value of product and round it to 2 nd decimal places.

(Ans) select round (avg(pcost),2) from prodd;

You might also like