Raghav Dbms Ex - 1c

You might also like

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

Database Management System - 1C

Create a Student table with the attributes Rollno, Sname, Passport Number, Department
code, Age, CGPA, and Club Membership ID. Also add suitable constraints to Student table
Code: create table student(
rollno int primary key,
sname varchar(50),
passport_no int unique,
dept_code int,
age int,
cgpa float,
club_id int
);
insert into student values(1,'vishal',12345,20,18,9.18,14);
insert into student values(2,'sengathir',54324,63,20,9.09,94);
insert into student values(4,'murugavel',24523,23,72,9.3,89);
insert into student values(3,'kavin',24535,26,34,9.21,43);
Que 1: Fetch Rollno and Sname from student table and display StudName instead of Sname
in the output
Code:

select rollno,sname as StudName from student;

Output:

ROLL STUDNA
NO ME

1 vishal

2 sengathir

murugav
4
el

3 kavin

Que 2: Display CGPAChanged instead of CGPA with additionally provide 0.5 if the CGPA of

the students is 5.9 or 6.9 or 7.9 or 8.9

Raghav Somasundaram PL 22i359


Code:

select rollno, case when cgpa in(5.9,6.9,7.9,8.9) then cgpa +0.5 end as CgpaChanged from student;

Output:

ROLL CGPACHANG
NO ED

1 -

2 -

4 -

3 -

Que 3: Display all students that starts with ‘M' or 'V', ends with ‘i’ and are at least 6
characters in Length

Code:

select sname from student where (sname like 'M%' or sname like 'V%') and sname like '%i' and
length(sname)>=6;

Output: no data found

Que 4: Selects all students with a Name that have "a" in the second position or the name
contains any one of the vowels

Code:

select sname from student where sname like '_a' or sname like '%a' or sname like '%e%' or sname
like '%i%' or sname like '%o%' or sname like '%u%';

Output:

SNAME

vishal

sengath
ir

muruga
vel

kavin

Que 5: Display student roll number and name of the student with a Name that should NOT start

with "Z” or “Q”

Code:

Raghav Somasundaram PL 22i359


select rollno, sname from student where not(sname like 'Q%' or sname like 'Z%');

Output:

ROLL
SNAME
NO

1 vishal

sengath
2
ir

muruga
4
vel

3 kavin

Que 6: Find the name of the students who are not having passport number and display the

records in alphabetical order

Code:

insert into student(rollno, sname,dept_code, age,cgpa, club_id) values(5,'adf',26,34,9.21,43);

select sname from student where passport_no is null order by sname;

Output:

SNA
ME

adf

Que 7: Find the roll no and name of the student who are having Club Membership ID

Code:

select rollno,sname from student where club_id is not null;

Output:

ROLL
SNAME
NO

1 vishal

sengath
2
ir

Raghav Somasundaram PL 22i359


muruga
4
vel

3 kavin

5 adf

Que 8: Add three more columns such as part time student, part time employee and Full time

student to the student table (Note: Update the existing records; e.g.: if the part time

student column is set to 1 then it means that the student is studying part time)

Code:

alter table student add ptstu int default 0;

alter table student add ptemp int default 0;

alter table student add ftstu int default 0;

select * from student;

Output:

A
ROLL PASSPORT_ DEPT_CO CG CLUB PTS PTE FTS
SNAME G
NO NO DE PA _ID TU MP TU
E

1 9.1
1 vishal 12345 20 14 0 0 0
8 8

sengath 2 9.0
2 54324 63 94 0 0 0
ir 0 9

muruga 7
4 24523 23 9.3 89 0 0 0
vel 2

3 9.2
3 kavin 24535 26 43 0 0 0
4 1

3 9.2
5 adf - 26 43 0
4 1

Que 9: Create student_ParttimeStudy, student_Parttimeemp and student_Fulltimestudy tables

with Sid, name and phone number from the table student

Code:

Raghav Somasundaram PL 22i359


create table stu_ptst as select rollno as Sid, sname as name, passport_no from student where
ptstu=1;

create table stu_ptemp as select rollno as Sid, sname as name, passport_no from student where
ptemp=1;

create table stu_ftst as select rollno as Sid, sname as name, passport_no from student where
ftstu=1;

Output:Table created.

Table created.

Table created.

Que 10: Display Maximum, minimum and average CGPA of students with name Max_CGPA,

Min_CGPA, and Avg_CGPA

Code:

select max(cgpa) as Max_CGPA, min(cgpa) as Min_CGPA, avg(cgpa) as Avg_CGPA from student;

Output:

MAX_CG MIN_CG AVG_CG


PA PA PA

9.3 9.09 9.198

Que 11: Display total number of students with the name “Tot_Stud”

Code:

select sname,count(*) from student where sname='Tot_stud' group by sname;

select sname,count(*) from student where sname='vishal' group by sname;

Output:

no data found

SNA COUNT
ME (*)

visha
1
l

Que 12: Find the number of students with unique age

Code: select count(unique age) from student;

Output:

COUNT(UNIQUEA

Raghav Somasundaram PL 22i359


GE)

Que 13: Retrieve and compute the summation of total age among students from the Students table.

Code: select sum(age) from student;

Output:

SUM(A
GE)

178

Que 14: Displays number of students in each department

Code: select sname,dept_code from student group by sname,dept_code;

Output:

DEPT_CO
SNAME
DE

sengath
63
ir

vishal 20

muruga
23
vel

adf 26

kavin 26

Que 15: Based on each department, display average CGPA obtained by the students

Code: select avg(cgpa),sname,dept_code from student group by sname,dept_code;

Output:

AVG(CG DEPT_CO
SNAME
PA) DE

sengath
9.09 63
ir

9.18 vishal 20

muruga
9.3 23
vel

Raghav Somasundaram PL 22i359


9.21 adf 26

9.21 kavin 26

Que 16: Display the average CGPA of the IT department

Code: select avg(cgpa) from student where dept_code=20;

Output:

AVG(CG
PA)

9.18

Que 17: Fetch the department name and number of students of those departments that have more

than 4 students

Code: select dept_code, sname from student group by dept_code, sname having count(*)>4;

Output: no data found

Que 18: Find the name of the student who are studying full time and working part time(allow

duplication)

Code: select sname from student where ftstu=1 and ptemp=1;

Output: no data found

Que 19: Find the name of the student who are studying part time but not working part time

Code: select sname from student where ftstu=1 and ptemp=0;

Output:

SNAME

sgd

vishal

sengath
ir

muruga
vel

kavin

adf

Raghav Somasundaram PL 22i359


Que 20: Find the name of the student who are either studying part time or working part

time(restrict duplication)

Code: select distinct sname from student where ptstu=1 union select distinct sname from student
where ptemp=1;

Output:

SfE

fdg

Raghav Somasundaram PL 22i359

You might also like