DBMS Practical File
DBMS Practical File
FILE
DBMS
5. Retrieve students’ names whose roll no either starts with ‘X’ or ‘Z’ and ends with ‘9’
Ans. Select StudentName from STUDENT where Roll_No like “X%” or Roll_No like “Z%” and Roll_No like
“%9”;
6. Find society details with more than N TotalSeats where N is to be input by the user
Ans. Declare @N int = [Enter_Desired_Value_Here];
Select * from SOCIETY where TotalSeats > @N;
8. Find society names in which more than five students have enrolled
Ans. Select s.SocID, s.SocName from SOCIETY s JOIN ENROLLMENT e on s.SocID = e.SID
group by s.SocID, s.SocName having count(*) > 5;
9. Find the name of the youngest student enrolled in society ‘NSS’
Ans. Select s.SocName, st.StudentName from STUDENT st
join ENROLLMENT e on st.Roll_no = e.Roll_no
join SOCIETY s on e.SID = s.SocID
where s.SocName = 'NSS'
order by st.DOB desc
limit 1;
12. Find the students' names who are not enrolled in any society.
Ans. Select s.StudentName from STUDENT s
where not exists ( select 1 from ENROLLMENT e where s.Roll_No = e.Roll_No);
15. Find names of all students who have enrolled in any society and society names in which at least
one student has enrolled
Ans. Select st.StudentName, s.SocName from STUDENT st
join ENROLLMENT e on st.Roll_No = e.Roll_No
join SOCIETY s on e.SID = s.SocID;
16. Find names of students who are enrolled in any of the three societies ‘Debating’, ‘Dancing’ and
‘Sashakt’.
Ans. Select st.StudentName from STUDENT st
join ENROLLMENT e on st.Roll_No = e.Roll_No
join SOCIETY s on e.SID = s.SocID
where s.SocName in ('Debating', 'Dancing', 'Sashakt');
17. Find society names such that its mentor has a name with ‘Gupta’ in it.
Ans. Select Socname from SOCIETY where MentorName like “%gupta%”;
18. Find the society names in which the number of enrolled students is only 10% of its capacity.
Ans. Select s.SocName from SOCIETY s
join ENROLLMENT e on s.SocID = e.SID
group by s.SocName
having count(*) <= s.TotalSeats * 0.1;
21. Add the enrollment fees paid (‘yes’/’No’) field in the enrollment table.
Ans. Alter Table ENROLLMENT add EnrollFeesPaid varchar(4);
22. Update date of enrollment of society id ‘s1’ to ‘2018-01-15’, ‘s2’ to the current date and ‘s3’ to
‘2018-01-02’.
Ans Update ENROLLMENT
Set DateOfEnrollment=‘2018/01/15’
Where SID=”s1”;
Update ENROLLMENT
Set DateOfEnrollment=select current_date;
Where SID=”s2”;
Update ENROLLMENT
Set DateOfEnrollment=‘2018/01/02’
Where SID=”s3”;
23. Create a view to keep track of society names with the total number of students enrolled in it.
Ans. Select s.SocName, COUNT(e.Roll_no) AS TotalEnrolled
from SOCIETY s
join ENROLLMENT e ON s.SocID = e.SID
group by s.SocName;
24. Find student names enrolled in all the societies.
Ans. Select st.StudentName from STUDENT st
where not exists ( select 1 from SOCIETY s
where not exists ( select 1 from ENROLLMENT e
where e.SID = s.SocID and e.Roll_No = st.Roll_No));
25. Count the number of societies with more than 5 students enrolled in it
Ans. Select count(*) as SocietyCount from (
select s.SocID from SOCIETY s
join ENROLLMENT e on s.SocID = e.SID
group by s.SocID
having count(*) > 5
) as SocietyWithMoreThan5Students;
26. Add column Mobile number in student table with default value ‘9999999999’
Ans. Alter table STUDENT
add mobile_no int default 9999999999;
27. Find the total number of students whose age is > 20 years.
Ans. Select count(*) from STUDENT
where year(curdate()) - year(DOB) > 20;
28. Find names of students who were born in 2001 and are enrolled in at least one society.
Ans. Select st.StudentName from STUDENT st
join ENROLLMENT e on st.Roll_No = e.Roll_No
where st.DOB between “2001/01/01” and “2001/12/31”
group by st.StudentName
having count(*) >= 1;
29. Count all societies whose name starts with ‘S’ and ends with ‘t’ and at least 5 students are
enrolled in the society.
Ans. Select count(*) as SocietyCount from (
select s.SocID from SOCIETY s
join ENROLLMENT e on s.SocID = e.SID
where SocName like “S%” and SocName like “%t”
group by s.SocID
having count(*) > 5
) as SocietyWithMoreThan5Students;