Uploaded by Laura Davis

SQL Queries (2019 Spr) in video for Student Enrollment Database-1

advertisement
ISYS7700, Spring 2019
The ERD that corresponds to this database is at
https://www.lucidchart.com/invitations/accept/d62a4c5f-52bf-4fa3abcd-6d8fab5e2703
-- - =======================
-- - Teacher and Department Information
-- - This lists teacher information from a single table, the Teachers
table
-- SELECT t_Fname, t_Lname, t_Dept
FROM Teachers;
-- - This lists a combination of teacher and department information
-- - using the WHERE clause to join tables
-- SELECT t_Fname, t_Lname, t_dept, D_DeptName, D_Office
FROM Teachers, Departments
WHERE Teachers.t_Dept = Departments.d_id;
-- - This lists a combination of teacher and department information
-- - using the INNER JOIN clause
-- - We will use the INNER JOIN for the course.
-- SELECT t_Fname, t_Lname, t_dept, D_DeptName, D_Office
FROM Teachers INNER JOIN Departments
ON Teachers.t_Dept = Departments.d_id;
-- - Simple listing of the Enrollment table, students and courses
-- SELECT E_Student_ID, E_Course_ID
FROM Enrollments;
-- - Listing of the complete Enrollment table, students and courses
-- - ordered by Course ID
-- SELECT E_Student_ID, E_Course_ID
FROM Enrollments
ORDER BY e_course_id;
-- - Listing of the complete Enrollment table with the student's name
and the course ID
-- - for the course that they are enrolled in. It does not include
the course name.
-- - The output is ordered by Course ID
-- SELECT E_Student_ID, s_Fname, S_Lname, e_course_id
FROM Enrollments INNER JOIN Students
ON Enrollments.e_student_id = students.s_id
ORDER BY e_course_id;
-- - Listing of the complete Enrollment table with the student's name
and course name.
-- - This requires joining together three tables.
-- SELECT s_Fname, S_Lname, e_course_id, c_CourseName
FROM ((Enrollments
INNER JOIN Students ON Enrollments.e_student_id = students.s_id)
INNER JOIN Courses ON Enrollments.e_course_id = courses.c_id)
ORDER BY e_course_id;
-- - This lists the courses taken by students with the last name of
'A'.
-- - There might be more than one student with the last name of 'A'.
-- SELECT s_Fname, S_Lname, e_course_id, c_CourseName AS 'Course Name'
FROM ((Enrollments
INNER JOIN Students ON Enrollments.e_student_id = students.s_id)
INNER JOIN Courses ON Enrollments.e_course_id = courses.c_id)
WHERE Students.s_Lname = 'A';
-- - This lists all of the students in the course mktg1000, along with
the course name
-- SELECT s_Fname, S_Lname, c_CourseName AS 'Course Name', e_course_id
FROM ((Enrollments
INNER JOIN Students ON Enrollments.e_student_id = students.s_id)
INNER JOIN Courses ON Enrollments.e_course_id = courses.c_id)
WHERE e_course_id = 'mktg1000'
ORDER BY students.s_lname;
-- - This lists the courses and the name of the teacher who teaches
the course
-- SELECT c_id as 'Course ID', c_CourseName AS 'Course Name', t_Fname,
t_Lname
FROM (Courses
INNER JOIN Teachers ON courses.c_teacher = teachers.t_id);
LNE - first version Nov. 12, 2017, last updated March 21, 2019
Download