写出创建满足下列要求的视图的SQL 语句。 (1)查询学生的学号、姓名、所在系、课程号、课程名、课程学分。 (2)查询学生的学号、姓名、选修的课程名和考试成绩。 (3)统计每个学生的选课门数,列出学生的学号和选课门数。 (4)统计每个学生的修课总学分,列出学生的学号和选课门数。
时间: 2024-11-15 19:28:25 浏览: 1
假设我们有两个表,一个是`Students`,包含字段`StudentID`, `Name`, 和`Department`;另一个是`Courses`,包含字段`CourseID`, `CourseName`, `Credits`,还有一个关联学生和课程的表`Enrollments`,包含字段`StudentID`, `CourseID`和`Grade`。
以下是满足要求的SQL语句:
1. 查询学生的学号、姓名、所在系、课程号、课程名、课程学分:
```sql
SELECT s.StudentID, s.Name, c.Department, c.CourseID, c.CourseName, c.Credits
FROM Students s
JOIN Courses c ON s.Department = c.Department AND s.StudentID = Enrollments.StudentID;
```
2. 查询学生的学号、姓名、选修的课程名和考试成绩:
```sql
SELECT s.StudentID, s.Name, c.CourseName, e.Grade
FROM Students s
JOIN Enrollments e ON s.StudentID = e.StudentID
JOIN Courses c ON e.CourseID = c.CourseID;
```
3. 统计每个学生的选课门数:
```sql
SELECT s.StudentID, COUNT(DISTINCT e.CourseID) AS CourseCount
FROM Students s
JOIN Enrollments e ON s.StudentID = e.StudentID
GROUP BY s.StudentID;
```
4. 统计每个学生的修课总学分:
```sql
SELECT s.StudentID, SUM(c.Credits) AS TotalCredits
FROM Students s
JOIN Enrollments e ON s.StudentID = e.StudentID
JOIN Courses c ON e.CourseID = c.CourseID
GROUP BY s.StudentID;
```
阅读全文