Record Program 5:
SQL EXERCISE -1
Table: STATIONARY
S_ID StationaryName Company Price
DP01 Dot Pen ABC 10
PL02 Pencil XYZ 6
ER05 Eraser XYZ 7
PL01 Pencil CAM 5
GP02 Gel Pen ABC 15
Table: CONSUMER
C_ID ConsumerName Address S_ID
01 Good Learner Delhi PL01
06 Write Well Mumbai GP02
12 Topper Delhi DP01
15 Write & Draw Delhi PL02
16 Motivation Bangalore PL01
SQL COMMANDS:
1. To create the table STATIONARY and CONSUMER.
Ans: create table STATIONARY(S_ID char(4) primary key, StationaryName
varchar(30), Company varchar(20), Price int);
Output: 0 rows affected ( table created)
create table CONSUMER(C_ID int primary key, ConsumerName varchar(30),
Address varchar(20), S_ID char(4), constraint cons foreign key(S_ID)
references STATIONARY(S_ID));
Output: 0 rows affected ( table created)
2. To insert the data into the tables.
Ans: insert into STATIONARY values(‘DP01’,’Dot Pen’, ‘ABC’,10),(‘PL02’,’Pencil’,
‘XYZ’,6), (‘ER05’, ‘Eraser’,’XYZ’, 7),(‘PL01’, ‘Pencil’, ‘CAM’, 5), (‘GP02’,’Gel
Pen’, ‘ABC’, 15);
Output: 5 rows affected
Ans: insert into CONSUMER values( 01, ‘Good Learner’,’Delhi’,’PL01’),( 06,’Write
Well’, ‘Mumbai’,’GP02’),( 12,’Topper’,’Delhi’,’DP01’),( 15,’Write &
Draw’,’Delhi’,’PL02’), (16,’Motivation’,’Bangalore’,’PL01’);
Output: 5 rows affected
3. To display distinct Company from STATIONARY
Ans: select distinct Company from STATIONARY;
Output:
distinct Company
ABC
XYZ
CAM
4. To display the details of Stationary whose Price is in the range of 8 to 15 (Both
values included)
Ans: select * from STATIONARY where Price between 8 and 15;
Output:
S_ID StationaryName Company Price
DP01 Dot Pen ABC 10
GP02 Gel Pen ABC 15
5. To display the ConsumerName, Address from table Consumer and Company and
Price from table Stationary with their corresponding matching S_ID
Ans: select ConsumerName , Address, Company, Price from STATIONARY natural
join CONSUMER;
Output: ConsumerName Adrress Company Price
Topper Delhi ABC 10
Write & Draw Delhi XYZ 6
Good Learner Delhi CAM 5
Motivation Bangalore CAM 5
Write Well Mumbai ABC 15
Record Program No. 6:
SQL EXERCISE -2
TABLE : WORKER
ECODE NAME DESIG PLEVEL DOJ DOB
11 RadheShyam Supervisor P001 2004-09-12 1981-08-23
12 Chandernath Operator P003 2010-02-22 1987-07-12
13 Fizza Operator P003 2009-06-14 1983-10-14
14 Ameen Ahmed Mechanic P002 2006-08-21 1984-03-13
15 Sanya Clerk P002 2005-12-19 1983-06-09
18 Sarsa Supervisor P001 2010-01-20 1982-02-01
TABLE : PAYLEVEL
PLEVEL PAY ALLOWANCE
P001 26000 12000
P002 22000 10000
P003 12000 6000
SQL COMMANDS:
1. To display the details of all the WORKERS in descending order of their DOJ.
Ans: SELECT * FROM WORKER ORDER BY DOJ DESC;
Output:
ECODE NAME DESIG PLEVEL DOJ DOB
12 Chandernath Operator P003 2010-02-22 1987-07-12
18 Sarsa Supervisor P001 2010-01-20 1982-02-01
13 Fizza Operator P003 2009-06-14 1983-10-14
14 Ameen Ahmed Mechanic P002 2006-08-21 1984-03-13
15 Sanya Clerk P002 2005-12-19 1983-06-09
11 RadheShyam Supervisor P001 2004-09-12 1981-08-23
2. To display the NAME and DESIG of those WORKERS whose PLEVEL is either P001 or
P002.
Ans: SELECT NAME, DESIG FROM WORKER WHERE PLEVEL IN('P001','P002');
Output:
NAME DESIG
RadheShyam Supervisor
Ameen Ahmed Mechanic
Sanya Clerk
Sarsa Supervisor
3. To display the number of workers whose PAY+ALLOWANCE is more than 30000 for
every PLEVEL.
Ans: SELECT COUNT(*) FROM WORKER, PAYLEVEL WHERE WORKER.PLEVEL =
PAYLEVEL.PLEVEL AND PAY+ALLOWANCE >30000 GROUP BY WORKER. PLEVEL;
Output:
COUNT(*)
2
2
4. To increase the ALLOWANCE by 1000 where the pay is greater than 20000.
Ans: UPDATE PAYLEVEL SET ALLOWANCE =ALLOWANCE+1000
WHERE PAY>20000;
Output:
2 rows affected
5. To display the number of Workers designation wise.
Ans: SELECT COUNT(*),DESIG FROM WORKER GROUP BY DESIG;
Output:
COUNT(*) DESIG
2 Supervisor
2 Operator
1 Mechanic
1 Clerk