Shubham DBMS Lab
Shubham DBMS Lab
SALESMAN MASTER
alter table salesman_master modify salesmanno varchar(6) primary key;
alter table salesman_master modify salesmanname varchar(20) not null;
alter table salesman_master modify address1 varchar(30) not null;
alter table salesman_master modify salamt numeric(8,2) not null;
alter table salesman_master modify tgttoget numeric(6,2) not null;
alter table salesman_master modify ytdsales numeric(6,2) not null;
SALES ORDER
create table sales_order (orderno varchar(6) primary key, clientno varchar(6),
orderdate date not null, delyaddr varchar(25), salesmanno varchar(6), delytype char
default "f", billyn char, delydate date, orderstatus varchar(10), foreign key (clientno)
references client_master(clientno), foreign key (salesmanno) references
salesman_master(salesmanno));
insert into sales_order (orderno, clientno, orderdate, salesmanno, delytype, billyn,
delydate, orderstatus) values ("o19001", "c00001", str_to_date("12-june-04", "%d-
%m-%y"), "s00001", 'f', 'n', str_to_date("20-july-02", "%d-%m-%y"), "in process");
insert into sales_order (orderno, clientno, orderdate, salesmanno, delytype, billyn,
delydate, orderstatus) values ("o19002", "c00002", str_to_date("25-june-04", "%d-
%m-%y"), "s00002", 'p', 'n', str_to_date("27-june-02", "%d-%m-%y"), "cancelled");
insert into sales_order (orderno, clientno, orderdate, salesmanno, delytype, billyn,
delydate, orderstatus) values ("o46865", "c00003", str_to_date("18-feb-04", "%d-
%m-%y"), "s00003", 'f', 'y', str_to_date("20-feb-02", "%d-%m-%y"), "fulfilled");
insert into sales_order (orderno, clientno, orderdate, salesmanno, delytype, billyn,
delydate, orderstatus) values ("o19003", "c00001", str_to_date("03-apr-04", "%d-
%m-%y"), "s00001", 'f', 'y', str_to_date("07-apr-02", "%d-%m-%y"), "fulfilled");
insert into sales_order (orderno, clientno, orderdate, salesmanno, delytype, billyn,
delydate, orderstatus) values ("o46866", "c00004", str_to_date("20-may-04", "%d-
%m-%y"), "s00002", 'p', 'n', str_to_date("22-may-02", "%d-%m-%y"), "cancelled");
insert into sales_order (orderno, clientno, orderdate, salesmanno, delytype, billyn,
delydate, orderstatus) values ("o19008", "c00005", str_to_date("24-may-04", "%d-
%m-%y"), "s00004", 'f', 'n', str_to_date("26-july-02", "%d-%m-%y"), "in process");
SALES ORDER DETAILS
create table sales_order_details (orderno varchar(6), productno varchar(6),
qtyordered numeric(8), qtydisp numeric(8), productrate numeric(10,2), foreign key
(orderno) references sales_order(orderno), foreign key (productno) references
product_master(productno));
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o19001", "p00001", 4, 4, 525);
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o19001", "p00002", 2, 1, 8400);
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o19001", "p00003", 2, 1, 5250);
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o19002", "p00001", 10, 0, 525);
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o46865", "p00004", 3, 3, 3150);
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o46865", "p00005", 3, 1, 5250);
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o46865", "p00001", 10, 10, 525);
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o46865", "p00006", 4, 4, 1050);
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o19003", "p00007", 2, 2, 1050);
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o19003", "p00008", 1, 1, 12000);
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o46866", "p00009", 1, 0, 8400);
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o46866", "p00005", 1, 0, 1050);
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o19008", "p00001", 4, 4, 525);
insert into sales_order_details (orderno, productno, qtyordered, qtydisp,
productrate) values ("o19008", "p00007", 5, 3, 1050);
Lab-4
3. AND
SELECT * FROM sales_order_details WHERE orderno='O19001' AND productno='P00001';
4. OR
SELECT * FROM sales_order_details WHERE orderno='O19001' OR productno='P00001';
5. LIKE
SELECT * FROM sales_order_details WHERE productno LIKE 'P____1';
6. IN and NOT IN
SELECT * FROM sales_order_details WHERE productno IN('P00008','P00009');
SELECT * FROM sales_order_details WHERE productno NOT
IN('P00001','P00002','P00003','P00004','P00005');
7. BETWEEN
SELECT * FROM sales_order_details WHERE qtyordered BETWEEN 9 AND 10;
8. AVG()
SELECT AVG(productrate) "Average Rate" FROM sales_order_details;
9. MIN()
SELECT MIN(productrate) "Minimum Rate" FROM sales_order_details;
10. MAX()
SELECT MAX(productrate) "Maximum Rate" FROM sales_order_details;
11. COUNT()
SELECT COUNT(*) "No. Of Records" FROM sales_order_details;
12. SUM()
SELECT SUM(productrate) "Total Cost" FROM sales_order_details;
13. ABS()
SELECT ABS(-20) "Absolute";
14. POWER(m,n)
SELECT POWER(3,2);
15. ROUND(n[,m])
SELECT ROUND(15.19,1);
23. FLOOR(n): - Returns the largest integer value that is equal to or less than n.
SELECT FLOOR(24.8);
24. CEIL(n): - Returns the smallest integer value that is greater than or equal to n.
SELECT CEIL(24.8);
Lab-5
2. INITCAP: - Returns a string with the first letter of each word in upper case.
select initcap(name) "title case" from client_master;
5. ASCII: - Returns the NUMBER code that represents the specified character. If
more than one character is entered, the function will return the value for the first
character and ignore all of the characters after the first.
select ascii('a') ,ascii('A') from client_master;
11. LTRIM: - Removes characters from the left of char with initial characters
removed upto the first character in set.
select ltrim('Ivan Bayross','I') 'ltrim' from client_master;
12. TRIM: - Removes all specified characters either from the beginning or the
ending of a string.
select trim(' sql tutorial ') as trimmedstring;
select trim(leading 'x' from 'xxxhanselxxx') "remove prefixes";
select trim(both 'x' from 'xxxhanselxxx') "remove prefixes & suffixes";
14. RPAD: - Returns char1, right-padded to length n with the sequence of characters specified in
char2. If char2 is not specified, Oracle uses blanks by default.
select rpad('shb',6,'*') as new;
15. VSIZE: - Returns the number of bytes in the internal representation of an expression.
select vsize(name) "size" from client_master;
select length(name) "length" from client_master;
18. TO_CHAR (date conversion): - Convers a value of a DATE datatype to CHAR value.
TO_CHAR() accepts a data, as well as the format (fmt) in which the data has to appear. fmt
must be a date format. If fmt is omitted, the date is converted to a character value using the
default date format, i.e. “DD-MON-YY”.
select to_char(delydate, 'month dd, yyyy') "new date format" from sales_order;
21. LAST_DAY: - Returns the last date of the month specified with the function.
select sysdate(), last_day(sysdate()) "last day";
23. ROUND: - Returns a date rounded to a specific unit of measure. If the second parameter is
omitted, the ROUND function will round the date to the nearest day.
Exercise-1: Perform the following computations on table data:
a. List the names of all clients having ‘a’ as the second letter in their names.
select name from client_master where substr(name,2,1)='a';
b. List the clients who stay in a city whose First letter is ‘M’.
select * from client_master where city like 'M%';
e. List all information from the sales_order table for order placed in the month of June.
select * from sales_order where extract(month from sales_order.orderdate)=6;
g. List products whose selling price is greater than 500 and less than or equal to 750.
select * from product_master where sellprice>500 and sellprice<=750;
h. List the names, city and state of clients who are not in the state of ‘Maharashtra’.
select name, city, state from client_master where not state='Maharashtra';
k. Determine the maximum and minimum product prices. Rename the output as
max_price and min_price respectively.
SELECT MAX(sellprice) "max_price", MIN(sellprice) "min_price" FROM product_master;
l. Count the number of products having price less than or equal to 500.
SELECT COUNT(*) FROM product_master WHERE sellprice<=500;
m. List all the products whose qtyonhand is less than recorder level.
SELECT * FROM product_master WHERE qtyonhand<reorderlvl;
Exercise-2: Exercise on Date Manipulation:
a. List the order number and day on which clients placed their order.
SELECT orderno, DAY(orderdate) FROM sales_order;
SELECT orderno, DAY(orderdate) "Day" FROM sales_order;
b. List the month (in alphabets) and date when the orders must be delivered.
SELECT MONTHNAME(delydate) "Month", delydate "Date" FROM sales_order;
a. Find out the products, which have been sold to ‘Ivan Bayross’.
select p.productno, p.description from client_master c inner join sales_order s
on c.clientno=s.clientno inner join sales_order_details so on
so.orderno=s.orderno inner join product_master p on
p.productno=so.productno where c.name='Ivan Bayross';
b. Find out the products and their quantities that will have to be delivered
in the current month.
select p.description ,p.productno, so.qtyordered from sales_order s inner join
sales_order_details so on s.orderno=so.orderno inner join product_master p
on p.productno=so.productno where extract(month from
so.delydate)=extract(month from sysdate());
e. List the products and orders from customers who have ordered less
than 5 units of ‘Pull Overs’.
select description, so.orderno from (((client_master cm inner join sales_order
so on cm.clientno=so.clientno) inner join sales_order_details sod on
so.orderno=sod.orderno) inner join product_master pm on
sod.productno=pm.productno) where cm.clientno in (select cm.clientno from
(((client_master cm inner join sales_order so on so.clientno=cm.clientno) inner
join sales_order_details sod on sod.orderno=so.orderno) inner join
product_master pm on pm.productno=sod.productno) where
pm.description='pull overs' and qtyordered<5);
f. Find the products and their quantities for the orders placed by ‘Ivan
Bayross’ and ‘Mamta Muzumdar’.
select p.description ,so.qtyordered from client_master c inner join sales_order
s on c.clientno=s.clientno inner join sales_order_details so on
s.orderno=so.orderno
inner join product_master p on p.productno =so.productno where c.name in
('Ivan Bayross', 'Mamta Muzumdar');
g. Find the products and their quantities for the orders placed by ClientNO
‘C00001’ and ‘C00002’.
select p.description ,so.qtyordered from client_master c inner join sales_order
s on c.clientno=s.clientno inner join sales_order_details so on
s.orderno=so.orderno inner join product_master p on p.productno
=so.productno where c.clientno in ('C00001', 'C00002');
Lab-7 GROUP BY, HAVING
a. Print the description and total qty sold for each product.
SELECT description, SUM(qtyordered) FROM product_master pm,
sales_order_details sod WHERE sod.productno=pm.productno GROUP BY
description;
c. Calculate the average qty sold for each client that has a maximum order
value of 15000.00.
SELECT name, AVG(sod.qtydisp) "Avg. Qty. Sold" FROM sales_order_details
sod, sales_order so, client_master cm WHERE cm.clientno=so.clientno AND
so.orderno=sod.orderno GROUP BY name HAVING
MAX(sod.qtyordered*sod.productrate)>15000;
d. Find out the total of all the billed orders for the month of June.
SELECT so.orderno, orderdate, SUM(qtyordered*productrate) Bill,
SUM(qtydisp*productrate) Total FROM sales_order so, sales_order_details
sod WHERE so.orderno=sod.orderno AND billyn='Y' AND
MONTH(orderdate)=6 GROUP BY so.orderno, orderdate;
Lab-7 Subqueries
b. List the customer name ,address1 ,address2 ,city and pincode for the client who has
placed order ‘O19001’.
select name,address1,address2,city,pincode
from client_master where clientno in (select clientno from sales_order where
orderno='o19001');
c.List the client names that have placed orders before the month of May’02.
select name from client_master where clientno in (select clientno from
sales_order where orderdate<'2004-05-02');
d.List if the product ‘Lycra Top’ has been ordered by any client and print
the Client_no. , Name to whom it was sold .
select name,clientno from client_master where clientno in (select clientno
from sales_order where orderno in (select orderno from sales_order_details
where productno in (select productno from product_master where
description='Lycra Tops')));
e.List the names of clients who have placed orders worth Rs.1000 or more.
select name from client_master where clientno in (select cientno from
sales_order where orderno in (select orderno from sales_order_details
where(qtyordered*productrate)>=10000));
Lab-8 VIEW