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

Queries and Solutions

Uploaded by

getop67701
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)
91 views

Queries and Solutions

Uploaded by

getop67701
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/ 3

create table student(rollno char(6) NOT NULL, name varchar(20), course varchar(10), dob

date, primary key(rollno));


create table society(sid char(6) not null, socname varchar(20), mentor varchar(15), totalseats
int, primary key(sid));
create table enrollment(rollno char(6) NOT NULL, sid char(6) NOT NULL, dateofenroll
date, primary key(rollno, sid), foreign key(rollno) references student(rollno), foreign key(sid)
references society(sid) );

1. Retrieve names of students enrolled in any society


SELECT s.name FROM student s JOIN enrollment e ON s.rollno = e.rollno;
2. Retrieve all society names.
SELECT socname FROM society;
3. Retrieve students' names starting with letter 'A'.
SELECT name FROM student where name like ‘A%’;
4. Retrieve students' details studying in courses 'computer science' or 'chemistry'.
SELECT * FROM student WHERE course = 'computer science' or course=
'chemistry';
5. Retrieve students' names whose roll no either starts with 'X' or 'Z' and ends with '9'.
SELECT name FROM student where rollno like ‘X%9’ or rollno like ‘Z%9’;
6. Find society details with more than N TotalSeats where N is entered by user.

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


UPDATE society SET mentor = ‘Amit Kumar’ where socname = ‘NCC’;
8. Find society names in which more than five students have enrolled.
SELECT s.socname FROM society s JOIN enrollment e ON s.sid = e.sid GROUP BY
s.socname HAVING COUNT(e.rollno) > 5;
9. Find the name of youngest student enrolled in society NSS.
SELECT s.name FROM student s JOIN enrollment e ON s.rollno = e.rollno JOIN
society soc ON e.sid = soc.sid WHERE soc.socname = 'NSS' ORDER BY s.dob
DESC LIMIT 1;
10. Find the most two popular society names (one the basis of enrolled students).
SELECT s.socname FROM society s JOIN enrollment e on s.sid= e.sid GROUP BY
s.sid ORDER BY COUNT(*) DESC LIMIT 1;
11. Find the two least popular society names (one the basis of enrolled students).
SELECT s.socname FROM society s JOIN enrollment e on s.sid= e.sid GROUP BY
s.sid ORDER BY COUNT(*) LIMIT 2;
12. Find the student names who are not enrolled in any society.
SELECT name FROM student where rollno NOT IN (select rollno from enrollment);
13. Find the student names enrolled in at least two societies.
SELECT s.name FROM student s JOIN enrollment e on s.rollno= e.rollno GROUP
BY s.rollno HAVING COUNT(*)>=2;
14. Find society names in which maximum students are enrolled.
SELECT s.socname FROM society s JOIN enrollment e on s.sid= e.sid GROUP BY
s.sid ORDER BY COUNT(*) DESC LIMIT 1;
15. Find names of all students who have enrolled in any society and society names in
which at least one student has enrolled.
SELECT DISTINCT s.name FROM student s JOIN enrollment e ON s.rollno =
e.rollno
UNION
SELECT DISTINCT soc.socname FROM society soc JOIN enrollment e ON soc.sid
= e.sid;
16. Find names of students who are enrolled in any of the three societies ‘Debating’,
‘Dancing’ and ‘Sashakt’.
SELECT DISTINCT s.name FROM student s JOIN enrollment e ON s.rollno =
e.rollno JOIN society soc ON e.sid = soc.sid WHERE soc.socname IN ('Debating',
'Dancing', 'Sashakt');
17. Find the society names such that its mentor has a name with “Gupta” in it.
SELECT socname FROM society where mentor like ‘%Gupta%’;
18. Find the society names in which the number of enrolled students is only 10% of its
capacity.
SELECT socname FROM society s WHERE (SELECT COUNT(*) FROM
enrollment e WHERE e.sid= s.sid) <= 0.1* totalseats;
19. Display the vacant seats for each society.
SELECT soc.socname, soc.totalseats - COUNT(e.rollno) AS vacant_seats FROM
society soc LEFT JOIN enrollment e ON soc.sid = e.sid GROUP BY soc.socname,
soc.totalseats;
20. Increment total seats of each society by 10%.
UPDATE society SET totalseats = totalseats + 0.1* totalseats
21. Add enrollment fees paid field in the enrollment table.
ALTER TABLE enrollment ADD COLUMN enrollment_fees_paid CHAR(3)
DEFAULT 'No';
22. Update date of enrollment of society id ‘s1’ to ‘2018-01-15’, ‘s2’ to current date and
‘s3’ to ‘2018-01-02’.
UPDATE enrollment SET dateofenroll = '2018-01-15' WHERE sid = 's1';
UPDATE enrollment SET dateofenroll = CURDATE() WHERE sid = 's2';
UPDATE enrollment SET dateofenroll = '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.
CREATE VIEW v1 AS SELECT society.socname, enrollment.sid, COUNT(*) FROM
society, enrollment WHERE society.sid=enrollment.sid GROUP BY enrollment.sid;
24. Find the student names enrolled in all the societies.
SELECT s.name FROM student s LEFT JOIN enrollment e on s.rollno = e.rollno
GROUP BY s.name HAVING COUNT(DISTINCT e.sid) = (SELECT COUNT(*)
FROM society);
25. Count the number of societies with more than 5 students enrolled in it.
SELECT COUNT(*) FROM (SELECT soc.sid FROM society soc JOIN enrollment e
ON soc.sid = e.sid GROUP BY soc.sid HAVING COUNT(e.rollno) > 5) AS
NoOfSocieties;
26. Add column Mobile number in student table with default value ‘9999999999’.
ALTER TABLE student ADD COLUMN mobile_number CHAR(10) DEFAULT
'9999999999';
27. Find the total number of students whose age is > 20 years.
SELECT COUNT(*) AS total_students_over_20 from student where
timestampdiff(year, dob, curdate()) > 20;
28. Find names of students who are born in 2001 and are enrolled in at least one society.
SELECT s.name FROM student s RIGHT JOIN enrollment e ON s.rollno=e.rollno
WHERE s.dob BETWEEN '2001-01-01' and '2001-12-31' GROUP BY e.rollno;
29. Count all societies whose name starts with ‘S’ and ends with ‘t’ and at least 5 students
are enrolled in the society.
SELECT COUNT(*) FROM society s WHERE s.socname LIKE 'S%t' AND
(SELECT COUNT(*) FROM enrollment e WHERE e.sid=s.sid HAVING
COUNT(*)>5);
30. Display the following information:
Society name Mentor name Total capacity Total enrolled Unfilled seats
SELECT s.socname AS 'Society name',
s.mentor AS 'Mentor name',
s.totalseats AS 'Total capacity',
COUNT(e.rollno) AS 'Total enrolled',
(s.totalseats - COUNT(e.rollno)) AS 'Unfilled seats'
FROM society s
LEFT JOIN enrollment e ON s.sid = e.sid
GROUP BY s.sid

You might also like