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

DBMS Practical File

DBMS Practical File

Uploaded by

shellshock4549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DBMS Practical File

DBMS Practical File

Uploaded by

shellshock4549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PRACTICAL

FILE
DBMS

Name: Raghav Nanda


Class: B.A.Prog()
Roll.No.:11024
Section:A
Ques. I Create and use the following student-society database schema for a college to answer the
given (sample) queries using the standalone SQL editor ?

Here Rollno (ENROLLMENT) and SID (ENROLLMENT) are foreign keys.


1. Retrieve names of students enrolled in any society
Ans. Select StudentName from STUDENT where STUDENT.Roll_No=ENROLLMENT.Roll_No ;

2. Retrieve all society names.


Ans. Select Socname from SOCIETY ;

3. Retrieve students' names starting with the letter ‘A’.


Ans. Select StudentName from STUDENT where StudentName like “a%”;

4. Retrieve students' details studying in courses ‘computer science’ or ‘chemistry’.


Ans. Select * from STUDENT WHERE COURSE=”computersc” OR COURSE=”chemistry”;

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;

7. Update society table for the mentor name of a specific society


Ans. Update SOCIETY set MentorName=”Ishaan” where SID=”A45”;

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;

10. Find the name of the most popular society


Ans. Select s.SocName from SOCIETY s
join ENROLLMENT e on s.SocID = e.SID
group by s.SocName
order by count(*) desc
limit 1;

11. Find the name of two least popular societies


Ans. Select s.SocName from SOCIETY s
join ENROLLMENT e on s.SocID = e.SID
group by s.SocName
order by count(*)
limit 2;

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);

13. Find the students names enrolled in at least two societies


Ans Select st.StudentName from STUDENT st
join ENROLLMENT e on st.Roll_no = e.Roll_no
group by st.StudentName
having count(*) >= 2;

14. Find society names in which maximum students are enrolled


Ans. Select s.SocName from SOCIETY s
where (select COUNT(*) from ENROLLMENT e where e.SID = s.SocID) = (
select max(student_count)
from ( SELECT s1.SocID, count(*) as student_count from SOCIETY s1
join ENROLLMENT e1 on s1.SocID = e1.SID group by s1.SocID) as max_count_subquery);

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;

19. Display the vacant seats for each society


Ans. Select s.SocName, s.TotalSeats - count(e.Roll_no) as VacantSeats
from SOCIETY s
left join ENROLLMENT e on s.SocID = e.SID
group by s.SocName, s.TotalSeats;

20. Increment Total Seats of each society by 10%


Ans Update SOCIETY set TotalSeats = TotalSeats + (TotalSeats*0.01);

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;

30. Display the following information:


Society name Mentor name Total Capacity Total Enrolled Unfilled Seats
Ans. Select s.SocName,s.MentorName,s.TotalSeats, count(e.Roll_No) as TotalEnrolled, s.TotalSeats
- count(e.Roll_No) as UnfilledSeats
from SOCIETY s
left join ENROLLMENT e on s.SocID = e.SID
group by s.SocName, s.MentorName, s.TotalSeats;

You might also like