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

Week 3 Integrity Constraints Student

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

Week 3 Integrity Constraints Student

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

EXERCISE – 3

Schema:

(i) Student (sno, sname, doj, address, category) // sno is student roll.no

Column name Data type width constraint


Sno Number 3 Primary key
Sname varchar 20
dob date Check Age < 18
Doj date Doj <= sysdate
Address varchar 15
category char 1 Foreign key

(ii) first (sno, cp, English, drawing, chemistry)

Column name Data type width constraint


Sno Number 3 Foreign key
mid Number 1 Either 1 or 2
Cp Number 2 < = 40
english Number 2 < = 40
drawing Number 2 < = 40
Chemistry Number 2 < = 40

(iii) fees (sno, category, amount) // here sno is serial number

Column name Data type width constraint


Sno Number 1
category char 1 Primary key
amount Number 7,2
Instance Data:

(i) Student

sno sname dob doj address category


1 Victor 24/dec/1999 4/aug/2015 Kukatpally A

2 Pavani 12/mar/2000 5/aug/2015 Ameerpet B

3 Manvitha 09/apr/2000 4/aug/2015 Miyapur B

4 Deekshit 18/jun/2000 11/aug/2015 Kondapur B

5 Paul 09/dec/1999 4/aug/2015 ECIL B

6 Gouri 13/mar/2000 1/sep/2015 Kukatpally A

7 Anvesh 11/sep/2000 28/aug/2015 Miyapur C

8 Rachana 19/jul/2000 4/aug/2015 Ameerpet A

9 Parvathi 02/jul/2000 4/aug/2015 Kondapur C

10 Emmanuel 14/nov/1999 4/sep/2015 Kukatpally B

11 Amjad 11/feb/2000 11/aug/2015 ECIL A

12 Mujeeb 28/feb/2000 4/aug/2015 Miyapur C

(ii) First

Sno Mid CP English Drawing chemistry


1 1 18 32 40 33
2 1 31 25 31 37
3 1 a 34 35 39
4 1 35 31 a 23
5 1 33 34 31 36
6 1 23 27 25 18
7 1 39 36 a 29
8 1 37 31 38 30
9 1 19 11 21 29
10 1 31 32 34 33
11 1 40 39 a 37
12 1 38 38 31 38
1 2 37 35 40 35
2 2 12 32 29 a
3 2 38 39 35 a
4 2 40 28 40 33
5 2 a a a a
6 2 32 27 30 20
7 2 a 38 37 33
8 2 39 29 34 37
9 2 20 15 24 a
10 2 33 a 35 35
11 2 36 a 37 38
12 2 40 38 37 a
Note: Best of two is taken into consideration for calculating the final mid-term marks, while data
entry, enter ‘0’, wherever it’s marked ‘a’. because these fields are numeric.

(iii) Fees
Sno Category amount
1 A 68,000.00
2 B 98,500.00
3 C 3,00,000.00

Create tables:

create table student (

sno number(3) primary key,

sname varchar(20),

dob date check (months_between(sysdate, dob)/12 < 18), -- age check

doj date check (doj <= sysdate), -- doj less than or equal to current date

address varchar(15),

category char(1) references fees(category) -- foreign key to fees table


);

create table first (

sno number(3) references student(sno), -- foreign key to student table

mid number(1) check (mid in (1, 2)), -- mid term must be either 1 or 2

cp number(2) check (cp <= 40), -- cp marks <= 40

english number(2) check (english <= 40), -- english marks <= 40

drawing number(2) check (drawing <= 40), -- drawing marks <= 40

chemistry number(2) check (chemistry <= 40) -- chemistry marks <= 40

);

create table fees (

sno number(1),

category char(1) primary key,

amount number(7, 2)

);

Inserting values:

insert into student (sno, sname, dob, doj, address, category) values

(1, 'victor', to_date('24-dec-1999', 'dd-mon-yyyy'), to_date('04-aug-2015', 'dd-mon-yyyy'),


'kukatpally', 'A'),

(2, 'pavani', to_date('12-mar-2000', 'dd-mon-yyyy'), to_date('05-aug-2015', 'dd-mon-yyyy'),


'ameerpet', 'B'),

(3, 'manvitha', to_date('09-apr-2000', 'dd-mon-yyyy'), to_date('04-aug-2015', 'dd-mon-yyyy'),


'miyapur', 'B'),
(4, 'deekshit', to_date('18-jun-2000', 'dd-mon-yyyy'), to_date('11-aug-2015', 'dd-mon-yyyy'),
'kondapur', 'B'),

(5, 'paul', to_date('09-dec-1999', 'dd-mon-yyyy'), to_date('04-aug-2015', 'dd-mon-yyyy'),


'ecil', 'B'),

(6, 'gouri', to_date('13-mar-2000', 'dd-mon-yyyy'), to_date('01-sep-2015', 'dd-mon-yyyy'),


'kukatpally', 'A'),

(7, 'anvesh', to_date('11-sep-2000', 'dd-mon-yyyy'), to_date('28-aug-2015', 'dd-mon-yyyy'),


'miyapur', 'C'),

(8, 'rachana', to_date('19-jul-2000', 'dd-mon-yyyy'), to_date('04-aug-2015', 'dd-mon-yyyy'),


'ameerpet', 'A'),

(9, 'parvathi', to_date('02-jul-2000', 'dd-mon-yyyy'), to_date('04-aug-2015', 'dd-mon-yyyy'),


'kondapur', 'C'),

(10, 'emmanuel', to_date('14-nov-1999', 'dd-mon-yyyy'), to_date('04-sep-2015', 'dd-mon-


yyyy'), 'kukatpally', 'B'),

(11, 'amjad', to_date('11-feb-2000', 'dd-mon-yyyy'), to_date('11-aug-2015', 'dd-mon-yyyy'),


'ecil', 'A'),

(12, 'mujeeb', to_date('28-feb-2000', 'dd-mon-yyyy'), to_date('04-aug-2015', 'dd-mon-yyyy'),


'miyapur', 'C');

insert into first (sno, mid, cp, english, drawing, chemistry) values

(1, 1, 18, 32, 40, 33),

(2, 1, 31, 25, 31, 37),

(3, 1, 0, 34, 35, 39),

(4, 1, 35, 31, 0, 23),

(5, 1, 33, 34, 31, 36),

(6, 1, 23, 27, 25, 18),


(7, 1, 39, 36, 0, 29),

(8, 1, 37, 31, 38, 30),

(9, 1, 19, 11, 21, 29),

(10, 1, 31, 32, 34, 33),

(11, 1, 40, 39, 0, 37),

(12, 1, 38, 38, 31, 38),

(1, 2, 37, 35, 40, 35),

(2, 2, 12, 32, 29, 0),

(3, 2, 38, 39, 35, 0),

(4, 2, 40, 28, 40, 33),

(5, 2, 0, 0, 0, 0),

(6, 2, 32, 27, 30, 20),

(7, 2, 0, 38, 37, 33),

(8, 2, 39, 29, 34, 37),

(9, 2, 20, 15, 24, 0),

(10, 2, 33, 0, 35, 35),

(11, 2, 36, 0, 37, 38),

(12, 2, 40, 38, 37, 0);

insert into fees (sno, category, amount) values

(1, 'A', 68000.00),

(2, 'B', 98500.00),

(3, 'C', 300000.00);


Queries:

1) List the students who come from Miyapur.


SQL> select * from student
2 where addr = 'Miyapur';
Output:

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:

7) Which quarter of the year did Anvesh join the institute?


SQL> select case
2 when extract(month from doj) in (1, 2, 3) then 'q1'
3 when extract(month from doj) in (4, 5, 6) then 'q2'
4 when extract(month from doj) in (7, 8, 9) then 'q3'
5 when extract(month from doj) in (10, 11, 12) then 'q4'
6 end as quarter
7 from student
8 where sname = 'Anvesh';
Output:
8) How many students come from Kondapur? Who are they?
SQL> select count(*) as coun from student
2 where addr = 'Kondapur'
3 ;
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:

18) Display the fee paid under each category, in words.


SQL> select cat,amt,to_char(to_date(amt,'j'),'jsp')
2 In_words from fees;
Output:
19) Display the count of students admitted under each category.
SQL> select cat, count(*) from student
2 group by cat;
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:

You might also like