DBMS SQL

You might also like

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 6

d5.

sql
Who has access

System properties
Type
SQL
Size
7 KB
Storage used
7 KB
Location
MySQL
Owner
Geek n Tech Hindi
Modified
Apr 11, 2019 by Geek n Tech Hindi
Opened
12:20 AM by me
Created
Apr 11, 2019
No description
Viewers can download
create table suppliers
(
sno varchar(5) primary key,
sname varchar(30),
status int(5),
scity varchar(10)
);

insert into suppliers values ('s101', 's1', 18, 'new york');


insert into suppliers values ('s102', 's2', 20, 'london');
insert into suppliers values ('s103', 's3', 25, 'paris');
insert into suppliers values ('s104', 's4', 28, 'london');
insert into suppliers values ('s105', 's5', 30, 'paris');

+------+-------+--------+----------+
| sno | sname | status | scity |
+------+-------+--------+----------+
| s101 | s1 | 18 | new york |
| s102 | s2 | 20 | london |
| s103 | s3 | 25 | paris |
| s104 | s4 | 28 | london |
| s105 | s5 | 30 | paris |
+------+-------+--------+----------+
5 rows in set (0.06 sec)

create table parts


(
pno varchar(5) primary key,
pname varchar(30),
pcolor varchar(10),
pweight float(5),
pcity varchar(30)
);

insert into parts values('p101', 'p1', 'orange', 20, 'new york');


insert into parts values('p102', 'p2', 'white', 18, 'london');
insert into parts values('p103', 'p3', 'pink', 16, 'paris');
insert into parts values('p104', 'p4', 'red', 15, 'london');
insert into parts values('p105', 'p5', 'yellow', 11, 'new york');

mysql> select * from parts;


+------+-------+--------+---------+----------+
| pno | pname | pcolor | pweight | pcity |
+------+-------+--------+---------+----------+
| p101 | p1 | orange | 20 | new york |
| p102 | p2 | white | 18 | london |
| p103 | p3 | pink | 16 | london |
| p104 | p4 | red | 15 | london |
| p105 | p5 | yellow | 11 | paris |
+------+-------+--------+---------+----------+
5 rows in set (0.03 sec)

create table project


(
jno varchar(5) primary key,
jname varchar(30),
jcity varchar(30)
);

insert into project values('j101', 'main', 'new york');


insert into project values('j102', 'action', 'london');
insert into project values('j103', 'nothing', 'paris');
insert into project values('j104', 'zoom', 'london');
insert into project values('j105', 'game', 'paris');

+------+---------+----------+
| jno | jname | jcity |
+------+---------+----------+
| j101 | main | new york |
| j102 | action | london |
| j103 | nothing | paris |
| j104 | zoom | london |
| j105 | game | paris |
+------+---------+----------+
5 rows in set (0.00 sec)
create table shipment
(
s_sno varchar(5),
s_pno varchar(5),
s_jno varchar(5),
quantity float(5),
primary key (s_sno, s_pno, s_jno),
foreign key(s_sno) references suppliers(sno),
foreign key(s_pno) references parts(pno),
foreign key(s_jno) references project(jno)
);

insert into shipment values ('s101', 'p101', 'j101', 200);


insert into shipment values ('s102', 'p102', 'j102', 325);
insert into shipment values ('s103', 'p103', 'j103', 800);
insert into shipment values ('s104', 'p104', 'j104', 400);
insert into shipment values ('s105', 'p105', 'j105', 300);

mysql> select * from shipment;


+-------+-------+-------+----------+
| s_sno | s_pno | s_jno | quantity |
+-------+-------+-------+----------+
| s101 | p101 | j101 | 200 |
| s102 | p102 | j102 | 325 |
| s103 | p103 | j103 | 800 |
| s104 | p104 | j104 | 400 |
| s105 | p105 | j105 | 300 |
+-------+-------+-------+----------+
5 rows in set (0.00 sec)

b) select sno, status from suppliers where scity='Paris' and status>20;

+------+--------+
| sno | status |
+------+--------+
| s103 | 25 |
| s105 | 30 |
+------+--------+
2 rows in set (0.05 sec)

c) select sno, sname from suppliers, parts, shipment where parts.pname='P2' and
suppliers.sno=shipment.s_sno and parts.pno=shipment.s_pno;

+------+-------+
| sno | sname |
+------+-------+
| s102 | s2 |
+------+-------+
1 row in set (0.00 sec)

d) select sno, sname from suppliers, parts, shipment where suppliers.sno=shipment.s_sno and
parts.pno=shipment.s_pno and not parts.pname='p2';

+------+-------+
| sno | sname |
+------+-------+
| s101 | s1 |
| s103 | s3 |
| s104 | s4 |
| s105 | s5 |
+------+-------+

e) select s_sno, s_pno, s_jno, parts.pweight*shipment.quantity as " shipment" from shipment, parts
where parts.pno=shipment.s_pno;

+-------+-------+-------+----------+
| s_sno | s_pno | s_jno | shipment |
+-------+-------+-------+----------+
| s101 | p101 | j101 | 4000 |
| s102 | p102 | j102 | 5850 |
| s103 | p103 | j103 | 12800 |
| s104 | p104 | j104 | 6000 |
| s105 | p105 | j105 | 3300 |
+-------+-------+-------+----------+
5 rows in set, 1 warning (0.05 sec)

f)select * from shipment where quantity between 300 and 700;

+-------+-------+-------+----------+
| s_sno | s_pno | s_jno | quantity |
+-------+-------+-------+----------+
| s102 | p102 | j102 | 325 |
| s104 | p104 | j104 | 400 |
| s105 | p105 | j105 | 300 |
+-------+-------+-------+----------+
3 rows in set (0.02 sec)

g) select distinct pno from parts, suppliers where parts.pweight>16 or suppliers.sname='s2';

+------+
| pno |
+------+
| p101 |
| p102 |
| p103 |
| p104 |
| p105 |
+------+
5 rows in set (0.01 sec)

h) select pcity from parts, shipment where parts.pno=shipment.s_pno and parts.pcolor='red' and
shipment.quantity>5;

+--------+
| pcity |
+--------+
| london |
+--------+
1 row in set (0.00 sec)

i) select pno, pname, pcolor, pcity, pweight from parts, shipment, suppliers where
suppliers.sno=shipment.s_sno and shipment.s_pno=parts.pno and suppliers.scity='london';

+------+-------+--------+--------+---------+
| pno | pname | pcolor | pcity | pweight |
+------+-------+--------+--------+---------+
| p102 | p2 | white | london | 18 |
| p104 | p4 | red | london | 15 |
+------+-------+--------+--------+---------+
2 rows in set (0.00 sec)

j) select pno from parts, suppliers, shipment, project where project.jcity='london' and
suppliers.scity='london' and suppliers.sno=shipment.s_sno and shipment.s_pno= parts.pno and
project.jno=shipment.s_jno;
+------+
| pno |
+------+
| p102 |
| p104 |
+------+
2 rows in set (0.00 sec)

k) select count(jno) from suppliers, shipment, project where suppliers.sno = shipment.s_sno and
project.jno=shipment.s_jno and suppliers.sname='s1' group by jno;

+------------+
| count(jno) |
+------------+
| 1|
+------------+
1 row in set (0.03 sec)

l) select sum(quantity) from suppliers, parts, shipment where suppliers.sname = 's1' and
parts.pname='p1' and suppliers.sno=shipment.s_sno and parts.pno = shipment.s_pno group by sno;

+---------------+
| sum(quantity) |
+---------------+
| 200 |
+---------------+

You might also like