Problems and Exercises 1 through 9 are based on the class scheduling 3NF relations along with some sample data shown in Figure 6-11. Not shown in this figure are data for an ASSIGNMENT relation, which represents a many-to- any relationship between faculty and sections.
Write SQL queries to answer the following questions:
a. Which students are enrolled in Database and Networking? (Hint: Use SectionNo for each class so you can determine the answer from the Registration table by itself.)
b. Which instructors cannot teach both Syst Analysis and Syst Design?
c. Which courses were taught in the first semester of 2008but not in the second semester of 2008?
(a)
Assume database course had course ID ISM 4212 and is in section 2714 of registration table and networking course has ID ISM 4930 and is in section 2715. The command used to select the students enrolled in database and networking course is given below:
Query:
SELECT StudentID, COUNT(*) AS Total
FROM Registration_T
WHERE SectionNo IN (2714, 2715)
GROUP BY StudentID
HAVING COUNT(*) > 1;
Explanation:
• SELECT – To query the database and get back the specified fields SQL uses the select statement.
o Student_ID is a column name.
o The function COUNT(*)retrieves the number of records in the table.
o Total is a label.
• FROM – To query the database and get back the preferred information by specifying the table name.
o Registration_T is a table name.
• WHERE – This clause states which data values or rows will be displayed.
o The condition is to check whether the SectionNo is 2714 and 2715.
• GROUP BY – This clause is used to group the result of a SELECT statement done on a table, where the tuple values are similar for more than one column.
• The HAVING clause in SQL is used to filter data based on a specified condition. It is similar to the WHERE clause.
o The condition is to check whether the number of records is greater than 1.
For Registration_T table, refer to FIGURE 6-11 in the textbook.
Output:
StudentID | Total |
54907 | 2 |
(b)
The command used to select the instructors who cannot teach both system analysis and system design is given below:
Query:
SELECT FacultyID, COUNT(*)AS TotalCount
FROM Qualified_T
WHERE CourseID NOT IN ('ISM 3113','ISM 3112')
GROUP BY FacultyID;
Explanation:
• SELECT – To query the database and get back the specified fields SQL uses the select statement.
o Faculty_ID is a column name.
o The function COUNT(*)retrieves the number of records in the table.
o TotalCount is a label.
• FROM – To query the database and get back the preferred information by specifying the table name.
o Qualified_T is a table name.
• WHERE – This clause states which data values or rows will be displayed.
o The condition is to check whether the CourseID is not ISM 3113 and ISM 3112.
• GROUP BY – This clause is used to group the result of a SELECT statement done on a table, where the tuple values are similar for more than one column.
For Qualified_T table, refer to FIGURE 6-11 in the textbook.
Output:
FacultyID | TotalCount |
3467 | 2 |
(c)
The command used to select the courses covered in first semester of 2008 but not in second semester of 2008 is given below:
Query:
SELECT CourseID,Semester
FROM Section_T
WHERE Semester = ‘I-2008’
AND CourseID NOT IN (SELECT CourseID
FROM Section_T WHERE Semester = ‘II-2008’);
• SELECT – To query the database and get back the specified fields SQL uses the select statement.
o CourseID and Semester are column names.
o TotalCount is a label.
• FROM – To query the database and get back the preferred information by specifying the table name.
o Section_T is a table name.
• WHERE – This clause states which data values or rows will be displayed.
• The condition is to check whether the semester is equal to first semester and at the same time CourseID is not in second semester.
For Section_T table, refer to FIGURE 6-11 in the textbook.
Output:
CourseID | Semester |
ISM 3113 | I-2008 |
ISM 3113 | I-2008 |
ISM 4212 | I-2008 |
ISM 4930 | I-2008 |