Mysql Code To Create Table, Enter Record and Display Records
Mysql Code To Create Table, Enter Record and Display Records
2. Create a table shop with fields - pid integer, pname varchar(20), price double, SID Integer.
mysql> use purchase;
mysql> create table shop(pid integer, pname varchar(20), price double, SID Integer);
Query OK, 0 rows affected (2.03 sec)
5. Create a table store with fields - SID Integer, sname varchar(20), loc varchar(20)
mysql> create table store(SID Integer, sname varchar(20), loc varchar(20));
Query OK, 0 rows affected (2.25 sec)
7. Write query to display SID, sum of price group wise as per SID from table shop .
mysql> select SID,sum(price) from shop GROUP BY SID;
+------+------------+
| SID | sum(price) |
+------+------------+
| 111 | 14 |
| 222 | 56 |
| 333 | 35 |
+------+------------+
3 rows in set (0.02 sec)
8. Write query to display SID, count of each SID from table shop .
mysql> select sid, count(*) from shop group by SID;
+------+----------+
| sid | count(*) |
+------+----------+
| 111 | 1|
| 222 | 2|
| 333 | 2|
+------+----------+
3 rows in set (0.06 sec)
9. Write query to display SID, Sum of price of SID=222 from table shop.
mysql> select SID, sum(price) from shop GROUP BY SID having SID=222;
+------+------------+
| SID | sum(price) |
+------+------------+
| 222 | 56 |
+------+------------+
1 row in set (0.64 sec)
10. Write query to display PID,PNAME,PRICE from table shop and SNAME,LOC from
table store.
mysql> select pid,pname,price,sname,loc from shop,store where shop.sid=store.sid;
+-----+----------+-------+----------------+-----------+
| pid | pname | price | sname | loc |
+-----+----------+-------+----------------+-----------+
| 1 | Lux | 18 | Arpan Stores | Patna |
| 2 | Dettol | 16 | Ayangar Stores | Chennai |
| 3 | Lifebuoy | 14 | Deppak Stores | New Delhi |
| 4 | Ponds | 19 | Ayangar Stores | Chennai |
| 5 | Dove | 38 | Arpan Stores | Patna |
+-----+----------+-------+----------------+-----------+
5 rows in set (0.45 sec)
11. Write query to display PID,PNAME,PRICE,SID from table shop and SNAME,LOC
from table store.
mysql> select pid,pname,price,shop.sid,sname,loc from shop,store where shop.sid=store.sid;
+-----+----------+-------+------+----------------+-----------+
| pid | pname | price | sid | sname | loc |
+-----+----------+-------+------+----------------+-----------+
| 1 | Lux | 18 | 222 | Arpan Stores | Patna |
| 2 | Dettol | 16 | 333 | Ayangar Stores | Chennai |
| 3 | Lifebuoy | 14 | 111 | Deppak Stores | New Delhi |
| 4 | Ponds | 19 | 333 | Ayangar Stores | Chennai |
| 5 | Dove | 38 | 222 | Arpan Stores | Patna |
+-----+----------+-------+------+----------------+-----------+
5 rows in set (0.00 sec)
12. Write query to display PID,PNAME,PRICE,SID from table shop and SNAME,LOC
from table store for Product Name “Ponds”.
mysql> select pid,pname,price,shop.sid,sname,loc from shop,store where pname="Ponds"
AND shop.sid=store.sid;
+-----+-------+-------+------+----------------+---------+
| pid | pname | price | sid | sname | loc |
+-----+-------+-------+------+----------------+---------+
| 4 | Ponds | 19 | 333 | Ayangar Stores | Chennai |
+-----+-------+-------+------+----------------+---------+
1 row in set (0.41 sec)
13. Write query to display PID,PNAME,PRICE,SID from table shop and SNAME,LOC
from table store in ascending order of price;
mysql> select pid,pname,price,A.sid,sname,loc from shop A,store B where A.sid=B.
sid order by price;
+-----+----------+-------+------+----------------+-----------+
| pid | pname | price | sid | sname | loc |
+-----+----------+-------+------+----------------+-----------+
| 3 | Lifebuoy | 14 | 111 | Deppak Stores | New Delhi |
| 2 | Dettol | 16 | 333 | Ayangar Stores | Chennai |
| 1 | Lux | 18 | 222 | Arpan Stores | Patna |
| 4 | Ponds | 19 | 333 | Ayangar Stores | Chennai |
| 5 | Dove | 38 | 222 | Arpan Stores | Patna |
+-----+----------+-------+------+----------------+-----------+
5 rows in set (0.02 sec)
14. Write query to display all record of table shop whose SID is same as that of pname
as “LUX”.
mysql> select * from shop where sid= (select sid from shop where pname="LUX");
+-----+-------+-------+------+
| pid | pname | price | SID |
+-----+-------+-------+------+
| 1 | Lux | 18 | 222 |
| 5 | Dove | 38 | 222 |
+-----+-------+-------+------+
2 rows in set (0.08 sec)
15. Write query to display record having maximum price from table shop.
mysql> select * from shop where price=(select max(price) from shop);
+-----+-------+-------+------+
| pid | pname | price | SID |
+-----+-------+-------+------+
| 5 | Dove | 38 | 222 |
+-----+-------+-------+------+
1 row in set (0.03 sec)