Week 3 Integrity Constraints Student
Week 3 Integrity Constraints Student
Schema:
(i) Student (sno, sname, doj, address, category) // sno is student roll.no
(i) Student
(ii) First
(iii) Fees
Sno Category amount
1 A 68,000.00
2 B 98,500.00
3 C 3,00,000.00
Create tables:
sname varchar(20),
doj date check (doj <= sysdate), -- doj less than or equal to current date
address varchar(15),
mid number(1) check (mid in (1, 2)), -- mid term must be either 1 or 2
);
sno number(1),
amount number(7, 2)
);
Inserting values:
insert into student (sno, sname, dob, doj, address, category) values
insert into first (sno, mid, cp, english, drawing, chemistry) values
(5, 2, 0, 0, 0, 0),
2) List the student details whose name starts with ‘p’ and ends with ‘i’.
SQL> select * from student
2 where sname like 'P%i';
Output:
3) List the details of the students who were born in the year 2000 and March month.
SQL> select * from student
2 where dob like '%MAR%00';
Output:
4) Display the day of the week, of all students who were born in the month of Feb.
SQL> select dob, to_char(dob,'day') from student
2 where dob like '%FEB%';
Output:
5) Who are the students who joined on 4th august and come from ameerpet.
SQL> select * from student
2 where addr = 'Ameerpet' and doj like '04-AUG%';
Output:
6) Give the details of the students who have taken admission under ‘B’ category.
SQL> select * from student
2 where cat = 'B';
Output:
9) On which date, maximum no.of students joined the college? On which day of the
week is it? How many days lapsed, from January until that date.
SQL> select max(doj) as max_date,
2 count(*) as student_count,
3 to_char(max(doj), 'Day') as day_of_week,
4 (max(doj) - to_date('01-JAN-2023', 'DD-MON-YYYY')) as days_lapsed
5 from student
6 group by doj
7 having count(*) = (select max(student_count)
8 from (select count(*) as student_count
9 from student
10 group by doj));
Output:
10) Display the data in the category column of the student table as follows:
A as A-Cat, B as B-Cat.
SQL> select concat(cat,'-cat') from fees;
Output:
11) Display the highest marks secured in each subject, mid wise.
SQL> select mid,max(cp),max(eng),max(draw),max(chem) from first
2 group by mid;
Output:
12) Who secured the highest grade in each subject and in each mid.
SQL> select mid,
2 max(cp) as highest_cp,
3 max(english) as highest_english,
4 max(drawing) as highest_drawing,
5 max(chemistry) as highest_chemistry
6 from first
7 group by mid;
Output:
13) Display the final marks secured by each student, in each subject.
SQL> select sno, max(cp),max(eng),max(draw),max(chem) from first
2 GROUP BY
3 sno;
Output:
14) Display the student details along with the annual fee paid by them.
SQL> select s.sno,s.sname,f.amt from student s inner join fees f
2 on s.cat = f.cat
3 order by sno;
Output:
15) Using DEFINE function, calculate the total marks scored by each student, in each
mid.
SQL> Define tot = cp+eng+draw+chem
SQL> select sno, mid, &tot as total from first;
old 1: select sno, mid, &tot as total from first
new 1: select sno, mid, cp+eng+draw+chem as total from first
Output:
16) Which student was absent for more no.of subjects in 2 nd mid. Give the complete
details of the student.
SQL> select f.sno,
2 f.mid,
3 s.sname,
4 f.cp,
5 f.english,
6 f.drawing,
7 f.chemistry,
8 (case when f.cp = 0 then 1 else 0 end +
9 case when f.english = 0 then 1 else 0 end +
10 case when f.drawing = 0 then 1 else 0 end +
11 case when f.chemistry = 0 then 1 else 0 end) as absent_count
12 from first f
13 join student s on f.sno = s.sno
14 where f.mid = 2
15 order by absent_count desc
16 fetch first 1 rows only;
Output:
17) Who is the topper of the class, display the name of the student, along with the
category of admission and the fee paid by that student.
SQL> select s.sname,
2 s.category,
3 f.amount
4 from first fr
5 join student s on fr.sno = s.sno
6 join fees f on s.sno = f.sno
7 where (fr.cp + fr.english + fr.drawing + fr.chemistry) = (
8 select max(fr2.cp + fr2.english + fr2.drawing + fr2.chemistry)
9 from first fr2
10 )
11 fetch first 1 rows only;
Output:
20) Display the total fee collected from the students, category-wise.
SQL> select s.cat, sum(f.amt) from student s inner join fees f
2 on s.cat = f.cat
3 group by s.cat,f.amt;
Output: