0% found this document useful (0 votes)
9 views3 pages

SQL Queries for Student Enrollment Analysis

The document describes tables for students, classes, class enrollments, and faculty. It provides sample data and asks 10 multi-part questions about querying the tables to return names, ages, class details, enrollments, rooms, and other attributes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

SQL Queries for Student Enrollment Analysis

The document describes tables for students, classes, class enrollments, and faculty. It provides sample data and asks 10 multi-part questions about querying the tables to return names, ages, class details, enrollments, rooms, and other attributes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab Assignment-9

Consider the following tables:

Table Student:
+------+---------+---------+-------+------+
| snum | sname | major | level | age |
+------+---------+---------+-------+------+
| 101 | Jhon | CS | SR | 19 |
| 102 | Smith | CS | JR | 20 |
| 103 | Jacob | ECE | SR | 20 |
| 104 | Tom | CS | JR | 20 |
| 105 | Sid | CS | JR | 20 |
| 106 | Harry | History | SR | 21 |
| 107 | Hellen | CS | JR | 21 |
| 108 | Bob | English | SR | 22 |
| 109 | Andy | ECE | JR | 21 |
| 110 | Charles | History | SR | 23 |
+------+---------+---------+-------+------+

Table Class:
+--------+----------+------+------+
| cname | meets_at | room | fid |
+--------+----------+------+------+
| CSC342 | Morning | R128 | 201 |
| CSC343 | Noon | R128 | 203 |
| CSC345 | Night | R154 | 204 |
| ECE300 | Morning | R111 | 202 |
| ECE301 | Noon | R111 | 203 |
| ENG366 | Morning | R154 | 203 |
| ENG367 | Evening | R111 | 205 |
| HIS320 | Evening | R128 | 205 |
+--------+----------+------+------+
Table Enrolled:
+------+--------+
| snum | cname |
+------+--------+
| 101 | CSC342 |
| 101 | CSC343 |
| 101 | CSC345 |
| 101 | ECE300 |
| 101 | ENG366 |
| 102 | CSC343 |
| 102 | CSC345 |
| 102 | ECE301 |
| 103 | ECE300 |
| 103 | ECE301 |
| 104 | CSC342 |
| 104 | ECE301 |
| 105 | CSC345 |
| 105 | ECE300 |
| 106 | ENG366 |
| 106 | HIS320 |
| 107 | CSC342 |
| 107 | ENG366 |
| 108 | ENG367 |
| 108 | HIS320 |
| 109 | ECE300 |
| 109 | ECE301 |
| 110 | ENG366 |
| 110 | HIS320 |
+------+--------+

Table Faculty:
+-----+-----------+--------+
| fid | fname | deptid |
+-----+-----------+--------+
| 201 | S. Jackson| 301 |
| 202 | M. Shanks | 302 |
| 203 | I. Teach | 302 |
| 204 | A. Zobrah | 303 |
| 205 | M. Jensen | 303 |
+-----+-----------+--------+

The meaning of these relations is straightforward; for example, Enrolled has one
record per student-class pair such that the student is enrolled in the class.

Write the SQL statements required to create these relations, including


appropriate versions of all primary and foreign key integrity constraints.

Read all questions first and insert values accordingly.


No duplicates should be printed in any of the

answers. Write the following queries in SQL:

1. Find the names of all Juniors(Level = JR) who are enrolled in a class
taught by I. Teach.

2. Find the age of the oldest student who is either a History major or
enrolled in a course taught by I. Teach.

3. Find the names of all classes that either meet in room R128 or
have five or more students enrolled.

4. Find the names of all students who are enrolled in two class that
meet at the same time.

5. Find the names of faculty members who teach in every room in


which some class is taught.

6. Find the names of faculty members for whom the combined


enrollment of the course that they teach is less than five.

7. For each level, print the level and the average age of students for that
level.

8. For all levels except JR, print the level and the average age of
students for that level.

9. For each faculty member that has taught class only in room R128
print the faculty member’s name and the total number of classes he
or she has taught.

10. Find the names of students enrolled in the maximum number of


classes.

Common questions

Powered by AI

To identify juniors enrolled in a class taught by I. Teach, join the 'Enrolled', 'Class', 'Faculty', and 'Student' tables. Specifically, join on 'snum' to 'snum' between 'Enrolled' and 'Student', 'cname' to 'cname' between 'Enrolled' and 'Class', and 'fid' to 'fid' between 'Class' and 'Faculty'. Ensure 'level' is 'JR' and 'fname' is 'I. Teach'. This results in: SELECT DISTINCT sname FROM Student JOIN Enrolled ON Student.snum = Enrolled.snum JOIN Class ON Enrolled.cname = Class.cname JOIN Faculty ON Class.fid = Faculty.fid WHERE Student.level = 'JR' AND Faculty.fname = 'I. Teach';

To find the oldest student's age who is either a History major or enrolled in a class taught by I. Teach, create a subquery to select ages where 'major' is 'History'. Another subquery should select ages where 'snum' is part of a class taught by I. Teach by joining 'Enrolled', 'Class', and 'Faculty'. Use the 'UNION' of both subqueries to eliminate duplicates and finally select the 'MAX' of these ages. The query is: SELECT MAX(age) FROM (SELECT age FROM Student WHERE major = 'History' UNION SELECT Student.age FROM Student JOIN Enrolled ON Student.snum = Enrolled.snum JOIN Class ON Enrolled.cname = Class.cname JOIN Faculty ON Class.fid = Faculty.fid WHERE Faculty.fname = 'I. Teach') AS history_or_teach;

First, determine the maximum class count per student using the 'COUNT' function on 'snum' in 'Enrolled', grouping by 'snum'. Next, filter students matching this maximum count. Use a subquery for determining max: WITH ClassCount AS (SELECT snum, COUNT(cname) as num_classes FROM Enrolled GROUP BY snum) SELECT sname FROM Student WHERE snum IN (SELECT snum FROM ClassCount WHERE num_classes = (SELECT MAX(num_classes) FROM ClassCount)); This derives students with peak class enrollments.

To compute average ages for each level, group by 'level' in the 'Student' table and apply the 'AVG' function on 'age'. The SQL query is: SELECT level, AVG(age) as average_age FROM Student GROUP BY level; This provides a breakdown of average ages per academic level.

To find students enrolled in two classes meeting at the same time, self-join the 'Class' table on 'meets_at' ensuring different classes. Join with 'Enrolled' twice to have two class enrollments per student. Use the 'WHERE' clause to match meeting times and group by student numbers filtering 'HAVING' count greater than or equal to two. The query is: SELECT DISTINCT sname FROM Student WHERE snum IN (SELECT e1.snum FROM Enrolled e1 JOIN Enrolled e2 ON e1.snum = e2.snum JOIN Class c1 ON e1.cname = c1.cname JOIN Class c2 ON e2.cname = c2.cname WHERE e1.cname <> e2.cname AND c1.meets_at = c2.meets_at GROUP BY e1.snum HAVING COUNT(DISTINCT e1.cname) >= 2)

Exclude juniors by adding a 'WHERE' clause to filter 'level' excluding 'JR'. Group by 'level' and calculate 'AVG' on 'age'. This is formulated in the query: SELECT level, AVG(age) as average_age FROM Student WHERE level <> 'JR' GROUP BY level; It calculates average ages excluding the junior level.

To find faculty teaching in all class rooms, first determine all unique rooms using the 'Class' table. Count rooms the faculty teaches in by joining 'Class' with 'Faculty' and grouping by 'fid'. Compare this with the total unique room count. The query is: SELECT DISTINCT fname FROM Faculty WHERE NOT EXISTS (SELECT room FROM Class EXCEPT SELECT room FROM Class WHERE Class.fid = Faculty.fid GROUP BY room); This query checks for non-existence of rooms not taught by each faculty member.

To find faculty who have only taught in room R128, join 'Class' to 'Faculty' on 'fid' and group the results by 'fid'. Include faculty with unique 'room' counts of 1 and where 'room' equals R128. The SQL is: SELECT fname FROM Faculty JOIN Class ON Faculty.fid = Class.fid GROUP BY fname HAVING COUNT(DISTINCT room) = 1 AND MIN(room) = 'R128';

To determine the names of classes that meet in room R128 or have 5 or more students enrolled, perform a left join between 'Class' and 'Enrolled' on 'cname'. Group the results by 'cname' and apply the COUNT function to select classes with a count of 5 or more. Use a 'WHERE' clause to filter classes meeting in room R128. The query is: SELECT cname FROM Class LEFT JOIN Enrolled ON Class.cname = Enrolled.cname GROUP BY Class.cname HAVING COUNT(Enrolled.snum) >= 5 OR room = 'R128';

To identify faculty members whose total class enrollments are less than five, join 'Class', 'Enrolled', and 'Faculty', grouping by 'fid' in 'Faculty'. Use 'COUNT' on 'snum' to determine enrollments per faculty. Filter the results having counts fewer than five. The query formulation is: SELECT fname FROM Faculty JOIN Class ON Faculty.fid = Class.fid JOIN Enrolled ON Class.cname = Enrolled.cname GROUP BY Faculty.fname HAVING COUNT(Enrolled.snum) < 5;

You might also like