Student DB

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

student (usn, sname, address, phone, gender)

semsec (ssid, sem, sec)

class (usn, ssid)

subject (subcode, title, sem, credits)

iamarks (usn, subcode, ssid, test1, test2, test3, finalia)

write sql queries to

1. list all the student details studying in fourth semester ‘c’ section.

2. compute the total number of male and female students in each semester and in each
section.

3. create a view of test1 marks of student usn ‘1bi15cs101’ in all subjects.

4. calculate the finalia (average of best two test marks) and update the corresponding
table for all students.

5. categorize students based on the following criterion:

if finalia = 17 to 20 then cat = ‘outstanding’

if finalia = 12 to 16 then cat = ‘average’

if finalia< 12 then cat = ‘weak’

give these details only for 8th semester a, b, and c section students.
ddl commands:

create table student


(
usn varchar(10),
sname varchar(20),
address varchar(20),
phone numeric(10),
gender varchar(2),
primary key(usn)
);

create table semsec


(
ssid varchar(5),
sem int,
sec char(2),
primary key(ssid)
);

create table class


(
usn varchar(10),
ssid varchar(5),
primary key(usn,ssid),
foreign key(usn) references student (usn) on delete cascade,
foreign key(ssid) references semsec(ssid) on delete cascade
);

create table subject


(
subcode varchar(8),
title varchar(20),
sem int,
credits int,
primary key(subcode)
);

create table iamarks


(
usn varchar(10),
subcode varchar(8),
ssid varchar(5),
test1 int,
test2 int,
test3 int,
finalia float,
primary key(usn,subcode,ssid),
foreign key(usn) references student (usn) on delete cascade,
foreign key(subcode) references subject (subcode) on delete cascade,
foreign key(ssid) references semsec(ssid) on delete cascade
);

dml statements:

inserting values into student table:

insert into student values ('4pm13cs020','akshay','belagavi', 8877881122,'m');


insert into student values ('4pm13cs062','sandhya','shivamogga', 7722829912,'f');
insert into student values ('4pm13cs091','teesha','shivamogga', 7712312312,'f');
insert into student values ('4pm13cs066','supriya','mangaluru', 8877881122,'f');
insert into student values ('4pm14cs010','abhay','shivamogga', 9900211201,'m');
insert into student values ('4pm14cs032','bhaskar','shivamogga', 9923211099,'m');
insert into student values ('4pm14cs025','asmi','shivamogga', 7894737377,'f');
insert into student values ('4pm15cs011','ajay','tumkur', 9845091341,'m');
insert into student values ('4pm15cs029','chitra','davangere', 7696772121,'f');
insert into student values ('4pm15cs045','jeeva','bellary', 9944850121,'m');
insert into student values ('4pm15cs091','santosh','mangaluru', 8812332201,'m');
insert into student values ('4pm16cs045','ismail','kalburgi', 9900232201,'m');
insert into student values ('4pm16cs088','sameera','bengaluru', 9905542212,'f');
insert into student values ('4pm16cs122','vinayaka','chikamagalur', 8800880011,'m');

inserting values into semsec table:

insert into semsec values ('cse8a', 8,'a');


insert into semsec values ('cse8b', 8,'b');
insert into semsec values ('cse8c', 8,'c');
insert into semsec values ('cse7a', 7,'a');
insert into semsec values ('cse7b', 7,'b');
insert into semsec values ('cse7c', 7,'c');
insert into semsec values ('cse6a', 6,'a');
insert into semsec values ('cse6b', 6,'b');
insert into semsec values ('cse6c', 6,'c');
insert into semsec values ('cse5a', 5,'a');
insert into semsec values ('cse5b', 5,'b');
insert into semsec values ('cse5c', 5,'c');
insert into semsec values ('cse4a', 4,'a');
insert into semsec values ('cse4b', 4,'b');
insert into semsec values ('cse4c', 4,'c');
insert into semsec values ('cse3a', 3,'a');
insert into semsec values ('cse3b', 3,'b');
insert into semsec values ('cse3c', 3,'c');
insert into semsec values ('cse2a', 2,'a');
insert into semsec values ('cse2b', 2,'b');
insert into semsec values ('cse2c', 2,'c');
insert into semsec values ('cse1a', 1,'a');
insert into semsec values ('cse1b', 1,'b');
insert into semsec values ('cse1c', 1,'c');

inserting values into class table:

insert into class values ('4pm13cs020','cse8a');


insert into class values ('4pm13cs062','cse8a');
insert into class values ('4pm13cs066','cse8b');
insert into class values ('4pm13cs091','cse8c');
insert into class values ('4pm14cs010','cse7a');
insert into class values ('4pm14cs025','cse7a');
insert into class values ('4pm14cs032','cse7a');
insert into class values ('4pm15cs011','cse4a');
insert into class values ('4pm15cs029','cse4a');
insert into class values ('4pm15cs045','cse4b');
insert into class values ('4pm13cs091','cse4c');
insert into class values ('4pm16cs045','cse3a');
insert into class values ('4pm16cs088','cse3b');
insert into class values ('4pm16cs122','cse3c');

inserting values into subject table:

insert into subject values ('10cs81','aca', 8, 4);


insert into subject values ('10cs82','ssm', 8, 4);
insert into subject values ('10cs83','nm', 8, 4);
insert into subject values ('10cs84','cc', 8, 4);
insert into subject values ('10cs85','pw', 8, 4);
insert into subject values ('10cs71','ooad', 7, 4);
insert into subject values ('10cs72','ecs', 7, 4);
insert into subject values ('10cs73','ptw', 7, 4);
insert into subject values ('10cs74','dwdm', 7, 4);
insert into subject values ('10cs75','java', 7, 4);
insert into subject values ('10cs76','san', 7, 4);
insert into subject values ('15cs51', 'me', 5, 4);
insert into subject values ('15cs52','cn', 5, 4);
insert into subject values ('15cs53','dbms', 5, 4);
insert into subject values ('15cs54','atc', 5, 4);
insert into subject values ('15cs55','java', 5, 3);
insert into subject values ('15cs56','ai', 5, 3);

inserting values into iamarks table:

insert into iamarks (usn, subcode, ssid, test1, test2, test3) values ('4pm13cs091','10cs81','cse8c', 15, 16,
18);

insert into iamarks (usn, subcode, ssid, test1, test2, test3) values ('4pm13cs091','10cs82','cse8c', 12, 19,
14);

insert into iamarks (usn, subcode, ssid, test1, test2, test3) values ('4pm13cs091','10cs83','cse8c', 19, 15,
20);

insert into iamarks (usn, subcode, ssid, test1, test2, test3) values ('4pm13cs091','10cs84','cse8c', 20, 16,
19);

insert into iamarks (usn, subcode, ssid, test1, test2, test3) values ('4pm13cs091','10cs85','cse8c', 15, 15,
12);

sql> select * from student;

usn sname address phone ge


4pm13cs020 akshay belagavi 8877881122 m
4pm13cs062 sandhya shivamogga 7722829912 f
4pm13cs091 teesha shivamogga 7712312312 f
4pm13cs066 supriya mangaluru 8877881122 f
4pm14cs010 abhay shivamogga 9900211201 m
4pm14cs032 bhaskar shivamogga 9923211099 m
4pm14cs025 asmi shivamogga 7894737377 f
4pm15cs011 ajay tumkur 9845091341 m
4pm15cs029 chitra davangere 7696772121 f
4pm16cs045 ismail kalburgi 9900232201 m
4pm16cs088 sameera bengaluru 9905542212 f
4pm16cs122 vinayaka chikamagalur 8800880011 m
4pm15cs045 jeeva bellary 9944850121 m

sql> select * from semsec;

usn ssid
4pm13cs020 cse8a
4pm13cs062 cse8a
4pm13cs066 cse8b
4pm13cs091 cse8c
4pm14cs010 cse7a
4pm14cs025 cse7a
4pm14cs032 cse7a
4pm15cs011 cse4a
4pm15cs029 cse4a
4pm15cs045 cse4b
4pm16cs045 cse3a
4pm16cs088 cse3b
4pm16cs122 cse3c
4pm13cs091 cse4c

sql> select * from class;

ssid
sem sec

cse8a a
8

cse8b b
8

cse8c c
8

cse7a a
7

cse7b b
7

cse7c c
7

cse6a a
6

cse6b b
6

cse6c c
6

cse5a a
5

cse5b b
5

cse5c c
5
cse4a a
4

cse4b b
4

cse4c c
4

cse3a a
3

cse3b b
3

cse3c c
3

cse2a a
2

cse2b b
2

cse2c c
2

cse1a a
1

cse1b b
1

cse1c c
1

sql> select * from iamarks;

subcode ssid test1 test2 test3 finalia


usn

4pm13cs091 10cs81 cse8c 15 16 18

4pm13cs091 10cs82 cse8c 12 19 14

4pm13cs091 10cs83 cse8c 19 15 20

4pm13cs091 10cs84 cse8c 20 16 19

4pm13cs091 10cs85 cse8c 15 15 12


queries:

1. list all the student details studying in fourth semester ‘c’ section.

select *
from student s,semsec ss,class c
where s.usn=c.usn and ss.ssid=c.ssid and sem=4 and sec='c';

or

select *
from student natural join semsec natural join class
where sem=4 and sec='c';

ssid sname address phone ge sem se


usn
4pm13cs091 cse4c teesha shivamogga 7712312312 f 4 c

2. compute the total number of male and female students in each semester and in each
section.

select *
from (select ssid,sec,count(gender) female_students
from student natural join class natural join semsec
where gender='f' group by ssid,sec) q1
left outer join
(select ssid,sec,count(gender) male_students
from student natural join class natural join semsec
where gender='m' group by ssid,sec) q2
on q1.ssid=q2.ssid and q1.sec=q2.sec;

ssid se female_students ssid se male_students


cse4a a 1 cse4a a 1
cse7a a 1 cse7a a 2
cse8a a 1 cse8a a 1
cse8c c 1
cse8b b 1
cse4c c 1
cse3b b 1
3. create a view of test1 marks of student usn ‘1bi15cs101’ in all subjects.

create view test1_marks


as
select usn,subcode,test1
from iamarks
where usn='4pm13cs091';

view gets created. to view the contents type:

sql>select * from test1_marks;


usn subcode test1
4pm13cs091 10cs81 15
4pm13cs091 10cs82 12
4pm13cs091 10cs83 19
4pm13cs091 10cs84 20
4pm13cs091 10cs85 15

4. calculate the finalia (average of best two test marks) and update the corresponding
table for all students.

update iamarks
set finalia= (case when test1>=test2 and test2>=test3 then (test1+test2)/2
when test2>=test1 and test3>=test2 then (test2+test3)/2
when test1>=test3 and test2>=test3 then (test1+test2)/2
else (test1+test3)/2

or

update iamarks set finalia=((test1+test2+test3)-least(test1, test2, test3))/2;


5. categorize students based on the following criterion:
if finalia = 17 to 20 then cat = ‘outstanding’

if finalia = 12 to 16 then cat = ‘average’

if finalia< 12 then cat = ‘weak’

give these details only for 8th semester a, b, and c section students.

select usn, sname, finalia,(case when finalia between 17 and 20 then 'outstanding'
when finalia between 12 and 16 then 'average'
when finalia between 0 and 11 then 'weak'
else 'na'
end) as category
from iamarks natural join student natural join class natural join semsec
where sem=8 and sec in ('a','b','c');

You might also like