You are on page 1of 13

UNIVERSITY OF MYSORE

BSc.(Computer Science)
II
nd
year Semester 3
DBMS LAB SOLUTIONS


SQL


By:
S.N.Raghavan M.Sc. (CS)
Lecture in Computer Science
Department of Computer Science
N.D.R.K FIRST GRADE COLLEGE
Email:-raghavaniyengar92@gmail.com
HASSAN-573201






SEMESTER 3
DBMS LAB (MC282)
PRACTICAL: 20 (EXAM) + 10 (IA)
Practical Hours: 14 Weeks x 3Hours/Week=42 Hours/ Semester.
Exercise-1
Create the following tables:
1) SALESMEN
Table Structure:
Field Type Constraint
SID VARCHAR2 (5) Primary Key
SNAME VARCHAR2 (20) Not Null
CITY VARCHAR2 (15) Not Null
SALARY NUMBER (5, 2)
PRODUCT VARCHAR2 (20)
TGTTOGET NUMBER (5,2)
COMM NUMBER (5,2)
2) CUSTOMERS
Table Structure:
Field Type Constraint
CID VARCHAR2 (5) Primary Key
CNAME VARCHAR2 (20) Not Null
CITY VARCHAR2 (15) Not Null
STATE VARCHAR2 (15)
PINCODE NUMBER (8)
PRODUCT VARCHAR2 (20)
CLASS CHAR (1) Default value 'A'
3) ORDERS
Table Structure:
Field Type Constraint
OID VARCHAR2 (5) Primary Key
CID VARCHAR2 (5) Foreign Key (CUSTOMERS)
SID VARCHAR2 (5) Foreign Key (SALESMEN)
PRODUCT VARCHAR2 (20)
QTY NUMBER (5)
ODATE DATE Not Null
O_AMT NUMBER (8, 2) Minimum zero


Exercise 2
Insert the following records: (Enter required data in empty columns)





Exercise 3
Solve the following queries:
Display all the information about each salesman with appropriate headings.
List all the salesmen that live outside the Gujarat.
Displays class 'A' customers with their id and name.
Add a new field as country in salesmen and customers table.
Change the name of the city whose name is Sunil Gandhi from customers table.
List the information of all the customers who are located in Baroda.
List the name of all the salesmen whose salary is greater than 2000.
Change the order amount for the order id O501.
Display order id and order date from orders table whose sales id is S102.
Exercise 4(Constraints)
Add primary key and Foreign Key to the existing tables using alter table
command.
Create cust table which contains cno having pk, cname and occupation where data
Values inserted for cno must start with the capital letter C and cname should be in
upper case.
Insert the correct values as well as display the error message for incorrect values.
Find out the name of all the salesmen having a as the second letter in their
names.
List all the information of customers whose state contains null value.
List all the information of customers in descending order according to their name.
Exercise 5(Joins)
Display customer no, name, city and order amount.
Display salesman details with their order details.
Display customer info of salesman S102 and S105.
List the salesmen details along with customers names associated with them.
Exercise 5 (Group functions)
Display order info with salesman name which has given on date before 10
of any month.
Display minimum order amount of each sales man.
Display total order amount for each salesman.
Find the customer city of customer which order amount is minimum.


Exercise 6 (aggregate, math and string functions)
Count the total number of orders.
Count the number of salesmen whose commission is greater than 500.
Find average order amount of each salesman.
Find out the orders whose order amount is more than 12000 and also find the new
order amount as original order amount * 5.
Determine the minimum and maximum salary of the salesman and rename the
title as min_sal and max_sal respectively.
Show the use of right and left justify string function.
Show use of floor and ceiling function.
Exercise 7(Sub queries)
Display customer name which customer no is highest.
Display customer name whose salesman staying in Ahmadabad.
Display order info for which order taken earliest.
Exercise 8
1. Insurance database.
1.SQL> create table person(driver_id varchar(10),name varchar(10),address
varchar(10),primary key(driver_id));

SQL> create table car(regno varchar(10),model varchar(10),year int,primary key(regno));

SQL> create table accident(report_number int,accd_date date,location
varchar(10),primary key(report_number));

SQL> create table owns(driver_id varchar(10),regno varchar(10),primary
key(driver_id,regno),foreign key(driver_id) references person(driver_id),foreign
key(regno) references car(regno));

SQL> create table participated(driver_id varchar(10),regno varchar(10),report_number
int,damage_amount int,primary key(driver_id,regno,report_number),foreign
key(driver_id) references person(driver_id),foreign key(regno) references
car(regno),foreign key(report_number) references accident(report_number));

2. SQL> insert into person values('&driver_id','&name','&address');

SQL> insert into car values('&regno','&model',&year);

SQL> insert into accident values(&report_number,'&accd_date','&location');



SQL> insert into owns values('&driver_id','&regno');

SQL> insert into participated
values('&driver_id','&regno',&report_number,&damage_amount);

3a. SQL> update participated set damage_amount=25000 where report_number=12 and
regno='5';

3b. SQL> insert into accident values(&report_number,'&accd_date','&location');
SQL> insert into participated
values('&driver_id','&regno',&report_number,&damage_amount);

4. SQL> select count(distinct o.driver_id) as People from owns o,participated p,accident
a where a.accd_date like
'%08' and o.regno=p.regno and p.report_number=a.report_number;

5. SQL> select count(*) as Totalcars from car c,participated p where c.regno=p.regno and
c.model='Alto';



Exercise-1
Create the following tables:
1) SALESMEN
Create table salesmen ( sid varchar2(5) primary key,
2 sname varchar2(20) not null,
3 city varchar2(15) not null,
4 salary number(5,2),
5 product varchar2(20),
6 tgttoget number(5,2),
7 comm number(5,2) );

2) CUSTOMERS
Create table customers ( cid varchar2(5) primary key,
2 cname varchar2(20) not null,
3 city varchar2(15) not null,
4 state varchar2(15),
5 pincode number(8),
6 product varchar2(20),
7 class char(1) default A);

3) ORDERS
Create table orders ( oid varchar2(5) primary key,
2 cid varchar2(5) references customers,
3 sid varchar2(5) references salesmen,
4 product varchar2(20),
5 qty number(5),
6 odate date not null,
7 o_amt number(8,2) );


Exercise 2
Table1 Salesmen
Insert into salesmen (sid, sname, city, comm) values(s101,ajay
patel,ahmedabad,1200);
Insert into salesmen (sid, sname, city, comm) values(s102,chintan
shah,baroda,1500);
Insert into salesmen (sid, sname, city, comm) values(s103,vinay
mehra,pune,1200);
Insert into salesmen (sid, sname, city, comm) values(s104,jay
pandey,surat,800);
Insert into salesmen (sid, sname, city, comm) values(s105,jimit
dave,mumbai,300);
Insert into salesmen (sid, sname, city, comm) values(s106,manam
gandhi,ahmedabad,1200);
Table2 customers
Insert into customers(cid, cname, city, class) values(c301,nirav
patel,nadiad,B);
Insert into customers(cid, cname, city, class) values(c302,kiran
dave,delhi,A);
Insert into customers(cid, cname, city, class) values(c303,sapan
shah,bangalore,B);
Insert into customers(cid, cname, city, class) values(c304,saurabh
mehta,baroda,C);
Insert into customers(cid, cname, city, class) values(c305,smriti
mishra,ahmedabad,B);
Insert into customers(cid, cname, city, class) values(c306,harshal
pandya,mumbai,A);
Insert into customers(cid, cname, city, class) values(c307,sunil
gandhi,baroda,B);
Insert into customers(cid, cname, city, class) values(c301,bimal
thakkar,surat,C);


Table3 orders
Insert into orders(oid,cid,sid,odate,o_amt) values(o501,c302,s102,02-jan-
09,700000);
Insert into orders(oid,cid,sid,odate,o_amt) values(o502,c301,s105,21-jan-
09,10000);
Insert into orders(oid,cid,sid,odate,o_amt) values(o503,c308,s103,10-feb-
09,250000);
Insert into orders(oid,cid,sid,odate,o_amt) values(o504,c306,s104,14-feb-
09,400000);
Insert into orders(oid,cid,sid,odate,o_amt) values(o501,c302,s102,02-jan-
09,700000);
Insert into orders(oid,cid,sid,odate,o_amt) values(o501,c302,s102,02-jan-
09,700000);
Insert into orders(oid,cid,sid,odate,o_amt) values(o501,c302,s102,02-jan-
09,700000);
Insert into orders(oid,cid,sid,odate,o_amt) values(o501,c302,s102,02-jan-
09,700000);
Insert into orders(oid,cid,sid,odate,o_amt) values(o501,c302,s102,02-jan-
09,700000);
Insert into orders(oid,cid,sid,odate,o_amt) values(o501,c302,s102,02-jan-
09,700000);
Insert into orders(oid,cid,sid,odate,o_amt) values(o501,c302,s102,02-jan-
09,700000);






Exercise 3
Solve the following queries:
2. List all the salesmen that live outside the Gujarat.
Select * from salesmen where city not in(ahmedabad,baroda,surat);
3. Displays class 'A' customers with their id and name.
Select cid,cname from customers where class=a;
4. Add a new field as country in salesmen and customers table.
Alter table salesmen add(country varchar2(15));
5. Change the name of the city whose name is Sunil Gandhi from customers table.
Update customers set city=mumbai where cname=sunilgandhi;
6. List the information of all the customers who are located in Baroda.
Select * from customers where city=baroda;
7. List the name of all the salesmen whose salary is greater than 2000.
First if you not inserted the salary amount then insert the value using below query
Update salesmen set salary=2000 where sid=s101;
Do for all salesmen with changing salary and sid
Select * from salesmen where salary>2000;
8.Change the order amount for the order id O501.
Update orders set o_amt=800000 where oid=o501;
9. Display order id and order date from orders table whose sales id is S102.
select oid,odate from orders where sid=s102;









Exercise 4(Constraints)

1.Add primary key and Foreign Key to the existing tables using alter tablecommand.
We cant add primary key for which table having primary key

Alter table orders drop primary key;
Alter table orders add primary key(oid);
Alter table orders add(comm references salesmen);
2. Create cust table which contains cno having pk, cname and occupation where data

Create table cust (cno varchar2(15) primary key, cname varchar2(15),occupation
varchar2(15));

3. Values inserted for cno must start with the capital letter C and cname should be in
upper case.
insert into cust (cno,cname,occupation) values(C601,raju,manager);

5. Find out the name of all the salesmen having a as the second letter in their
names.

Select sname from salesmen where sname like_a%;

6. List all the information of customers whose state contains null value.

Select * from customers where state is null;

7. List all the information of customers in descending order according to their name.

Select * from customers order by cname desc;

Exercise 5(Joins)

1.Display customer no, name, city and order amount.

Select customers.cid,customers.cname,customers.city,orders.o_amt from customers
inner join orders on customers.cid=orders.cid;

2. Display salesman details with their order details.

Select salesmen.sid,salesmen.sname,salesmen.city,orders.oid,orders.cid,orders.sid,orders.o_amt
from salesmen innerjoin orders on salesmen.sid=orders.sid;





3. Display customer info of salesman S102 and S105.

Select customers.cid,customer.cname,customers.city,orders.sid,orders.oid from
customers inner join orders.cid=customers.cid where orders.sid in(s102,s105);

4. List the salesmen details along with customers names associated with them.

Select salesmen.sid,salesmen.sname from salesmen inner join orders on
salesmen.sid=orders.sid union all select customers.cid,customers.cname from customers inner
join orders on customers.cid=orders.cid;

Exercise 5 (Group functions)

1. Display order info with salesman name which has given on date before 10 of any month.

Select salesmen.sid,salesmen.sname,orders.oid,orders.o_amt,orders.odate from salesmen
inner join orders on salesmen.sid=orders.sid where orders.odate like 01-%-09 or orders.odate like
02-%-09 or orders.odate like 03-%-09 or orders.odate like 04-%-09 or orders.odate like 05-%-
09 or orders.odate like 06-%-09 or orders.odate like 07-%-09 or orders.odate like 08-%-09 or
orders.odate like 09-%-09 or orders.odate like 10-%-09;

2. Display minimum order amount of each salesman.

Select sid, min(o_amt) from orders group by sid;

3. Display total order amount for each salesman.

Select sid, sum(o_amt) from orders group by sid;

4. Find the customer city of customer which order amount is minimum.

Select customers.cid,customers.cname,customers.city from customers inner join orders on
customers.cid=orders.cid where orders.o_amt in (select min(o_amt) from orders);


Exercise 6 (aggregate, math and string functions)

1. Count the total number of orders.

Select count(*) from orders;

2. Count the number of salesmen whose commission is greater than 500.

Select count(*) from salesmen where comm.>500;





3. Find average order amount of each salesman.

Select sid,avg(o_amt) from orders group by sid;

4. Find out the orders whose order amount is more than 12000 and also find the new
order amount as original order amount * 5.

Select oid,o_amt,Round(o_amt*5) from orders where o_amt>1200;

5. Determine the minimum and maximum salary of the salesman and rename the
title as min_sal and max_sal respectively.

select min(salary)min_sal from salesmen;
select max(salary)max_salfrom salesmen;

6. Show the use of right and left justify string function.

Select lpad(patel,10,*) lpad from dual;
Select rpad(patel,10,*)rpad from dual;

7. Show use of floor and ceiling function.

Select floor(24.9) from dual;
Select ceil(24.9) from dual;

Exercise 7(Sub queries)

1. Display customer name which customer no is highest.

Select customers.cid,customers.cname,customers.city from customers inner join orders on
customers.cid=orders.cid where orders.o_amt in (select max(o_amt) from orders);

2. Display customer name whose salesman staying in Ahmadabad.

Select customers.cname from customers inner join orders on customers.cid=orders.cid where
orders.sid in ( select sid from salesmen where city=ahmedabad);

3. Display order info for which order taken earliest.

Select oid,cid,sid,odate from orders where odate in (select min(odate) from orders);

Exercise 8
This exercise can be done with students its self by using above queries.

You might also like