0% found this document useful (0 votes)
445 views20 pages

Shubham DBMS Lab

The document describes the creation of tables to store data for clients, products, salesmen and sales orders. It creates the tables with the relevant columns and inserts sample data. It then describes operations like retrieving, updating, deleting records and altering the table structures by adding primary keys, foreign keys and modifying column properties. This lays the foundation for a basic sales database with linked tables.

Uploaded by

Himanshu Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
445 views20 pages

Shubham DBMS Lab

The document describes the creation of tables to store data for clients, products, salesmen and sales orders. It creates the tables with the relevant columns and inserts sample data. It then describes operations like retrieving, updating, deleting records and altering the table structures by adding primary keys, foreign keys and modifying column properties. This lays the foundation for a basic sales database with linked tables.

Uploaded by

Himanshu Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Lab-1 CREATE TABLE

create table client_master (clientno varchar(6), name varchar(20), address1


varchar(30), address2 varchar(30), city varchar(15), pincode numeric(8), state
varchar(15), baldue numeric(10,2));
insert into client_master (clientno, name, city, pincode, state, baldue) values
('c00001', 'ivan bayross', 'mumbai', 400054, 'maharashtra', 15000);
insert into client_master (clientno, name, city, pincode, state, baldue) values
('c00002', 'mamta muzumdar', 'madras', 780001, 'tamil nadu', 0);
insert into client_master (clientno, name, city, pincode, state, baldue) values
('c00003', 'chhaya bankar', 'mumbai', 400057, 'maharashtra', 5000);
insert into client_master (clientno, name, city, pincode, state, baldue) values
('c00004', 'ashwini joshi', 'bangalore', 560001, 'karnataka', 0);
insert into client_master (clientno, name, city, pincode, state, baldue) values
('c00005', 'hansel colaco', 'mumbai', 400060, 'maharashtra', 2000);
insert into client_master (clientno, name, city, pincode, state, baldue) values
('c00006', 'deepak sharma', 'mangalore', 560050, 'karnataka', 0);

create table product_master (productno varchar(6), description varchar(15),


profitpercent numeric(4,2), unitmeasure varchar(10), qtyonhand numeric(8),
reorderlvl numeric(8), sellprice numeric(8,2), costprice numeric(8,2));
insert into product_master (productno, description, profitpercent, unitmeasure,
qtyonhand, reorderlvl, sellprice, costprice) values ('p00001', 't-shirts', 5, 'piece', 200,
50, 350, 250);
insert into product_master (productno, description, profitpercent, unitmeasure,
qtyonhand, reorderlvl, sellprice, costprice) values ('p00002', 'shirts', 6, 'piece', 150,
50, 500, 350);
insert into product_master (productno, description, profitpercent, unitmeasure,
qtyonhand, reorderlvl, sellprice, costprice) values ('p00003', 'cotton jeans', 5, 'piece',
100, 20, 600, 450);
insert into product_master (productno, description, profitpercent, unitmeasure,
qtyonhand, reorderlvl, sellprice, costprice) values ('p00004', 'jeans', 5, 'piece', 100,
20, 750, 500);
insert into product_master (productno, description, profitpercent, unitmeasure,
qtyonhand, reorderlvl, sellprice, costprice) values ('p00005', 'trousers', 2, 'piece',
150, 50, 850, 550);
insert into product_master (productno, description, profitpercent, unitmeasure,
qtyonhand, reorderlvl, sellprice, costprice) values ('p00006', 'pull overs', 2.5, 'piece',
80, 30, 700, 450);
insert into product_master (productno, description, profitpercent, unitmeasure,
qtyonhand, reorderlvl, sellprice, costprice) values ('p00007', 'denim shirts', 4, 'piece',
100, 40, 350, 250);
insert into product_master (productno, description, profitpercent, unitmeasure,
qtyonhand, reorderlvl, sellprice, costprice) values ('p00008', 'lyera tops', 5, 'piece',
70, 30, 300, 175);
insert into product_master (productno, description, profitpercent, unitmeasure,
qtyonhand, reorderlvl, sellprice, costprice) values ('p00009', 'skirts', 2, 'piece', 75,
30, 450, 300);

create table salesman_master (salesmanno varchar(6), salesmanname varchar(20),


address1 varchar(30), address2 varchar(30), city varchar(20), pincode numeric(8),
state varchar(20), salamt numeric(8,2), tgttoget numeric(6,2), ytdsales numeric(6,2),
remarks varchar(60));
insert into salesman_master (salesmanno, salesmanname, address1, address2,
city, pincode, state, salamt, tgttoget, ytdsales, remarks) values ('s00001', 'aman',
'a/14', 'worli', 'mumbai', 400002, 'maharashtra', 3000, 100, 50, 'good');
insert into salesman_master (salesmanno, salesmanname, address1, address2,
city, pincode, state, salamt, tgttoget, ytdsales, remarks) values ('s00002', 'omkar',
'65', 'nariman', 'mumbai', 400001, 'maharashtra', 3000, 200, 100, 'good');
insert into salesman_master (salesmanno, salesmanname, address1, address2,
city, pincode, state, salamt, tgttoget, ytdsales, remarks) values ('s00003', 'raj', 'p-7',
'bandra', 'mumbai', 400032, 'maharashtra', 3000, 200, 100, 'good');
insert into salesman_master (salesmanno, salesmanname, address1, address2,
city, pincode, state, salamt, tgttoget, ytdsales, remarks) values ('s00004', 'ashish',
'a/5', 'juhu', 'mumbai', 400044, 'maharashtra', 3500, 200, 150, 'good');
Lab-2
Exercise-1: Retrieving records from a table.
a. Find out the names of all the clients.
SELECT name FROM client_master;
b. Retrieve the entire contents of the client_master table.
SELECT * FROM client_master;
c. Retrieve the list of names, city and the state of all the clients.
SELECT name, city, state FROM client_master;
d. List the various products available from the product_master table.
SELECT description FROM product_master;
e. List all the clients who are located in Mumbai.
SELECT * FROM client_master WHERE city='Mumbai';
f. Find the names of salesman who have a salary equal to Rs.3000.
SELECT salesmanname FROM salesman_master WHERE salamt=3000;
Exercise-2: Updating records in a table.
a. Change the city of clientno 'C00005' to 'Bangalore'.
UPDATE client_master set city='Bangalore' WHERE clientno='C00005';
b. Change the baldue of clientno 'C00001' to Rs.1000.
UPDATE client_master set baldue=1000 WHERE clientno='C00001';
c. Change the cost price of 'Trousers' to Rs.950.00.
UPDATE product_master set costprice=950.00 WHERE
description='Trousers';
d. Change the city of the salesman to 'Pune'.
UPDATE salesman_master set city='Pune';
Exercise-3: Deleting records in a table.
a. Delete all salesman from the salesman_master whose salaries are equal
to Rs.3500.
DELETE FROM salesman_master WHERE salamt=3500;
b. Delete all products from product_master where the quantity on hand is
equal to 100.
DELETE FROM product_master WHERE qtyonhand=100;
c. Delete from client_master where the column state holds the value 'Tamil
Nadu'.
DELETE FROM client_master WHERE state='Tamil Nadu';
Exercise-4: Altering the table structure.
a. Add a column called 'telephone' of data type 'NUMERIC' and size=10 to
the client_master.
ALTER TABLE client_master ADD telephone NUMERIC(10);
b. Change the size of sellprice column in product_master to 10,2.
ALTER TABLE product_master MODIFY sellprice NUMERIC(10,2);
Exercise-5: Deleting the table structure along with the data.
a. Destroy the table client_master along with its data.
DROP TABLE client_master;
Exercise-6: Renaming the table.
a. Change the name of the salesman_master table to sman_mast.
ALTER TABLE salesman_master RENAME to sman_mast;
Lab-3 ALTER TABLE, PRIMARY KEY, NOT NULL and FOREIGN KEY
REFERENCES.
CLIENT MASTER
alter table client_master modify clientno varchar(6) primary key;
PRODUCT MASTER
alter table product_master modify productno varchar(6) primary key;
alter table product_master modify description varchar(15) not null;
alter table product_master modify profitpercent numeric(4,2) not null;
alter table product_master modify unitmeasure varchar(10) not null;
alter table product_master modify qtyonhand numeric(8) not null;
alter table product_master modify reorderlvl numeric(8) not null;
alter table product_master modify sellprice numeric(8,2) not null;
alter table product_master modify costprice numeric(8,2) not null;

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

1. ADD PRIMARY KEY


ALTER TABLE client_master ADD PRIMARY KEY (name);

2. DROPPING INTEGRITY CONSTRAINTS VIA THE ALTER TABLE


ALTER TABLE client_master DROP PRIMARY KEY;

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);

16. SQRT(n): - Returns square root of n. If n<0, NULL.


SELECT SQRT(2) "Square Root";

17. EXP(n): - Returns e raised to nth power, where e = 2.71828183.


SELECT EXP(5) "Exponent";

18. EXTRACT(): - Returns a value extracted from a date or an interval value.


SELECT EXTRACT(YEAR FROM SYSDATE()) "Year";

19. GREATEST(): - Returns the greatest value in a list of expressions.


SELECT GREATEST(4, 5, 17);

20. LEAST(): - Returns the least value in a list of expressions.


SELECT LEAST(4, 5, 17);

21. MOD(m, n): - Returns the remainder of a m divided by n.


SELECT MOD(15, 7) "Mod1", MOD(15.7, 7) "Mod2";

22. TRUNC(number, [decimal_places]): - Returns a number truncated to a certain number of


decimal places.
SELECT TRUNC(125.815,1) "Trunc1", TRUNC(125.815, -2) "Trunc2";
SELECT TRUNCATE(125.815,1) "Trunc1", TRUNCATE(125.815, -2) "Trunc2";

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

1. LOWER: - Returns char, with all letters in lowercase.


select lower('Ivan Bayross') "lower" from client_master;

2. INITCAP: - Returns a string with the first letter of each word in upper case.
select initcap(name) "title case" from client_master;

3. UPPER: - Returns char, with all letters forced to uppercase.


select upper('Ivan Bayross') 'Title Case' from client_master;

4. SUBSTR: - Returns a portion of characters, beginning at character m, and going


upto character n. If n is omitted, the result returned is upto the last character in
the string. The first position of char is 1.
select substr(name,2,6) as new 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;

6. COMPOSE: - Returns a Unicode string. It can be a char, varchar2, nchar,


nvarchar2, clob, or nclob.
select compose('a'||unistr('\0301')) "composerd";

7. DECOMPOSE: - Accepts a string and returns a Unicode string.


select decompose(compose('a'||unistr('\0301'))) "decomposerd";

8. INSTR: - Returns the location of a substring in a string. If the start_position is


negative, the function counts back start_position number of characters from the
end of string1.
select instr('sct on the net','t') "inst1";
select instr('sct on the net', 't', 1, 2) "instr2";

9. TRANSLATE: - Replaces a sequence of characters in a string with another set


of characters.
select translate('1sct523', '123', '7a9') "change";
10. LENGTH: - Returns the length of a word.
select length(name) "length" 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";

13. LPAD: - Returns char1, left-padded to length n with the sequence of


characters specified in char2. If char2 is not specified, Oracle uses blanks by
default.
select lpad('shb',6,'*') as new;

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;

16. TO_NUMBER: - Converts char, a CHARACTER value expressing a number, to a NUMBER


datatype.
select to_number(substr('$100',2,4));

17. TO_CHAR (number conversion): - Converts a value of a NUMBER datatype to a


CHARACTER datatype, using the optional format string. TO_CHAR() accepts a number (n)
and a numeric format (fmt) in which the number has to appear. If fmt is omitted, n is converted
to a char value exactly long enough to hold all significant digits.
select to_char(17145, '$099,999') "char";

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;

19. TO_DATE, STR_TO_DATE: - Converts a character field to a date field.


select to_date('25-jun-1952 10:55 a.m.', 'dd-mon-yy hh:mi a.m.');
select str_to_date("22-may-02", "%d-%m-%y") "to date";
20. ADD_MONTHS, ADDDATE(<date>, INTERVAL <value> <addunit>): - Returns date after
adding the number of months specified in the function.
select add_months(sysdate(), 4) "add months";
select sysdate() "current date", adddate(sysdate(), interval 4 month) "4 months added";

21. LAST_DAY: - Returns the last date of the month specified with the function.
select sysdate(), last_day(sysdate()) "last day";

22. MONTHS_BETWEEN(d1, d2), DATEDIFF(d1, d2): - Returns number of months between d1


and d2.
select months_between('02-feb-2010', '02-jan-2020') "months";
select datediff('2020/02/27', '2020/02/20') "days";

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%';

c. List all clients who stay in ‘Bangalore’ or ‘Mangalore’.


select * from client_master where city in ('Bangalore','Mangalore');

d. List all clients whose baldue is greater than value 10000.


select * from client_master where baldue>10000;

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;

f. List the order information for clientno ‘C00001’ and ‘C00002’.


select * from sales_order where clientno='C00001' or clientno='C00002';

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';

i. Count the total number of orders.


select count(*) "total orders" from sales_order;

j. Calculate the average price of all the products.


SELECT AVG(sellprice) "Average Sell Price", AVG(costprice) "Average Cost Price" FROM
product_master;

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;

c. List the orderdate in the format ‘DD-Month-YY’. e.g. 12-February-02.


SELECT DATE_FORMAT(orderdate, '%d-%M-%y') "Order Date" FROM sales_order;

d. List the date, 15 days after today’s date.


SELECT SYSDATE() "Current Date", ADDDATE(SYSDATE(), INTERVAL 15 DAY) "15
Days Added";
Lab-6 INNER JOIN

Exercise: Exercise on JOIN and Correlation:

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());

c. List the ProductNo and description of constantly sold (i.e. rapidly


moving) products.

d. Find the names of clients who have purchased ‘Trousers’.


select c.name from product_master p inner join sales_order_details so on
p.productno=so.productno inner join sales_order s on s.orderno=so.orderno
inner join client_master c on c.clientno=s.clientno where
p.description='Trousers';

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

Exercise: Exercise on using Having and Group By Clauses:

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;

b. Find the value of each product sold.


SELECT description, SUM(qtyordered*productrate) Value FROM
sales_order_details sod, product_master pm 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

a. Find the product no and description of non moving products .


select productno,description from product_master where productno not in
(select productno from sales_order_details);

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

create VIEW vw AS select sales_order.orderno, orderdate, orderstatus


,product_master.productno ,productrate ,qtyordered from sales_order ,
sales_order_details, product_master where sales_order.orderno =
sales_order_details.orderno and
product_master.productno=sales_order_details.productno;
select * from vw;
insert into sales_order (orderno, clientno, orderdate, salesmanno, delytype,
billyn, delydate, orderstatus)
values ("o19023", "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 vw(orderno, orderdate, orderstatus)values("o12",str_to_date("12-june-
04", "%d-%m-%y"),"in process");

You might also like