SQL PRACTISE
SQL PRACTISE
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";
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;
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;
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 |
+----------+------+
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| +------------+-------------+
+------------+-------------+