0% found this document useful (0 votes)
48 views

Mysql Code To Create Table, Enter Record and Display Records

The document describes 15 SQL queries performed on databases and tables created in MySQL. The queries create databases and tables, insert records, display table structures and records, perform aggregation with GROUP BY and HAVING, JOIN tables, filter records, and find maximum values.

Uploaded by

Vipul
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Mysql Code To Create Table, Enter Record and Display Records

The document describes 15 SQL queries performed on databases and tables created in MySQL. The queries create databases and tables, insert records, display table structures and records, perform aggregation with GROUP BY and HAVING, JOIN tables, filter records, and find maximum values.

Uploaded by

Vipul
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No. 10.

1. Create a database named purchase.


mysql> create database purchase;

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)

3. Show the structure of table shop.


mysql> desc shop;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| pid | int(11) | NO | PRI | NULL | |
| pname | varchar(20) | YES | | NULL | |
| price | double | YES | | NULL | |
| SID | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.14 sec)

4. Insert 05 records as given below in table shop.


+-----+----------+-------+------+
| pid | pname | price | SID |
+-----+----------+-------+------+
| 1 | Lux | 18 | 222 |
| 2 | Dettol | 16 | 333 |
| 3 | Lifebuoy | 14 | 111 |
| 4 | Ponds | 19 | 333 |
| 5 | Dove | 38 | 222 |
+-----+----------+-------+------+

mysql> insert into shop values(1,'Lux',18.00, 222);


Query OK, 1 row affected (0.16 sec)

mysql> insert into shop values(2,'Dettol',16.00, 333);


Query OK, 1 row affected (0.03 sec)

mysql> insert into shop values(3,'Lifebuoy',14.00,111);


Query OK, 1 row affected (0.05 sec)

mysql> insert into shop values(4,'Ponds',19.00,333);


Query OK, 1 row affected (0.06 sec)

mysql> insert into shop values(5,'Dove',38.00,222);


Query OK, 1 row affected (0.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)

6. Insert detail of 03 stores as given below in table store.


| SID | sname | loc |
| 111 | Deepak Stores | New Delhi |
| 222 | Arpan Stores | Patna |
| 333 | Ayangar Stores | Chennai |
mysql> insert into store values(111,'Deepak Stores','New Delhi');
Query OK, 1 row affected (0.16 sec)

mysql> insert into store values(222,'Arpan Stores','Patna');


Query OK, 1 row affected (0.02 sec)

mysql> insert into store values(333,'Ayangar Stores','Chennai');


Query OK, 1 row affected (0.05 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)

You might also like