Q Create a table <Products> as per given data structure:
A create table products (PRODID varchar(5), PRODNAME varchar(10),
PRICE double(7,2), QTY int(2), DOP date, TYPE varchar(10);
Q Enter data as given below:
A mysql> insert into products
values("P001","MOUSE",250,5,"2020/02/12","COMPUTER"),("P002","COOKER",1500,3,"2019/12/10","KITC
HEN");
mysql> insert into products
values("P003","SPEAKERS",600,8,"2019/04/23","COMPUTER"),("P004","COOLER",4500,4,"2020/08/05","HO
ME");
mysql> insert into products
values("P005","MONITOR",5400,7,"2019/04/23","COMPUTER"),("P006","CHIMNEY",7600,3,"2020/02/13","
KITCHEN");
Q1 List all products whose price is more than 500 but less than 1500
A select * from products where PRICE>500 and PRICE<1500;
Q2 List those products where PRODNAME contains letter ‘O’ at 3rd place.
A select * from products where PRODNAME like "__O%";
Q3 List all computer products.
A select * from products where TYPE="COMPUTER";
Q4 List all kitchen products whose QTY is below 5.
A select * from products where TYPE='KITCHEN' and QTY<5;
Q5 Display name and price of computer products so that the record of highest priced is displayed first.
A select PRODNAME, PRICE from products where TYPE='COMPUTER' order by PRICE;
Q6 List various types of products.
A select distinct TYPE from products;
Q7 List all products purchased in the month of February of any year.
A select * from products where DOP like "____-02%";
Q8 List all computer products price below 800 and Kitchen products price above 800.
A select * from products where (TYPE='KITCHEN' and PRICE>800) or (TYPE='COMPUTER' and PRICE<800);
Q9 Display name, price, qty and total cost of each product where total cost is calculated as (price*qty).
A select PRODNAME, PRICE, QTY,PRICE*QTY as TotalCost from products;
Q10 Display name, type and qty of kitchen products whose quantity is between 3 to 5 and price above 2000.
A select PRODNAME, TYPE, QTY from products where QTY>3 and QTY<5 and PRICE>2000;
Q11 Decrease the price of all products by 5%.
A update products set PRICE = PRICE-PRICE*5/100;
Q12 Increase the price of Home products by 3%.
A update products set PRICE = PRICE+PRICE*3/100 where TYPE='HOME';
Q13 Change the DOP of COOLER to 14/04/2014.
A update products set DOP = "2014-04-14" where PRODNAME="COOLER";
Q14 Remove the records of products that are priced below 500.
A DELETE from products where PRICE<500;
Q1 mysql> Select distinct QTY from products; mysql> select price+100 from products
+------+ where qty>3 and qty<8;
| QTY | +-----------+
+------+ | price+100 |
| 3| +-----------+
| 8| | 4771.41 |
| 4| | 5230.00 |
| 7| +-----------+
+------+
Q3 mysql> select PRODNAME, PRICE*QTY"TOTAL COST" from products mysql> select distinct TYPE from
where TYPE="computer"; PRODUCTS where QTY>3;
+----------+------------+ +----------+
| PRODNAME | TOTAL COST | | TYPE |
+----------+------------+ +----------+
| SPEAKERS | 4560.00 | | COMPUTER |
| MONITOR | 35910.00 | | HOME |
+----------+------------+ +----------+
Q5 mysql> select prodname, qty from products where prodname like "C__%" order by qty;
+----------+------+
| prodname | qty |
+----------+------+
| COOKER | 3 |
| CHIMNEY | 3 |
| COOLER | 4 |
+----------+------+
ALTER TABLE COMMAND
Q Create a table fruits as per given data:
Q Fill data accordingly.
Q Add primary constant at FRID.
alter table fruits modify FRID char (4) primary key;
Q Add column QTY.
update fruits set qty=10;
Q Changed column name QTY to QUANTITY.
alter table fruits change qty QUANTITY int(4) not
null;
Q Set Fruit price to zero.
alter table fruits alter frprice set default 0;
Sample table - products
Q Display computer products. select * from products where type=’computer’;
Q Display product name and price whose price is select prodname, price from products where
greater than 2000. price>2000;
Q Display product ID and QTY which doesn’t belong select prodid, qty from products where
from kitchen. type<>'kitchen';
Q Display all details of products from kitchen and select * from products where type='home' or
home. type='kitchen';
Q Display all details of products where type is select * from products where type='computer' and
computer and price is above 1000. price>1000;
Q Display all details from computer and kitchen select * from products where (type='computer' or
whose price is above 2500. type='kitchen') and price>2500;
Q Display all products whose price is above 2500 and select * from products where price between 2500 and
less than equal to 5000. 5000;
OR
select * from products where price>2500 and
price<=5000;
Q Display details of cooler, chimney and cooker. select * from products where prodname in
('chimney','cooker','cooler');
Q
S
O
R
T
I
N
G
mysql> select type,count(*) from products group mysql> select type,count(*) from products group by
by type; type having count(*)>1;
+-------------+------------+ +------------+-------------+
| type | count(*) | | type | count(*) |
+-------------+------------+ +------------+-------------+
| KITCHEN | 2| | KITCHEN | 2|
| COMPUTER | 2| | COMPUTER | 2|
| HOME | 1| +------------+-------------+
+------------+-------------+