. 查询选修了“数据库应用”的学生的基本信息,结果列显示学号,姓名,性别,院系。
时间: 2024-11-13 10:39:17 浏览: 34
如果你有一个包含学生基本信息(如学号、姓名、性别、院系等)和选课记录的数据库,你可以通过JOIN操作来获取选修了“数据库应用”(假设该课程名为'Database Applications')的学生的基本信息。这里是一个基本的SQL查询示例:
```sql
SELECT sc.student_id AS 学号, s.student_name AS 姓名, s.gender AS 性别, s.department AS 院系
FROM Students s
JOIN StudentCourses sc ON s.student_id = sc.student_id
WHERE sc.course_name = 'Database Applications';
```
这个查询做了以下操作:
1. `JOIN Students s ON s.student_id = sc.student_id` 将学生表(Students)和选课表(StudentCourses)通过学生的学号关联起来。
2. `WHERE sc.course_name = 'Database Applications'` 筛选出选修了数据库应用课程的学生。
结果将包括这些学生的学习号、姓名、性别以及他们所在的院系。
相关问题
MySQL以数据库user_db及数据表student、teacher、class、course、sc为基础(8)查询每门课程最低分的学生的学号、姓名、院系、课程名及成绩; 3、SQL连接查询的应用; (1)查询选修“数据库原理”不及格的学生的学号、姓名、成绩; (2)查询和“崔晓”一个院系的其他学生的学号、姓名、院系; (3)查询信息技术学院年龄最大的学生的学号、姓名、性别; (4)查询信息技术学院成绩在60分以下之间的学生的学号、课程号、成绩,其中查询结果先按照课程号升序排列,再按照成绩降序排列;
1、查询每门课程最低分的学生的学号、姓名、院系、课程名及成绩
```
SELECT
sc.s_id AS 学号,
student.s_name AS 姓名,
student.s_dept AS 院系,
course.c_name AS 课程名,
MIN(sc.score) AS 最低分
FROM
sc
INNER JOIN student ON sc.s_id = student.s_id
INNER JOIN course ON sc.c_id = course.c_id
GROUP BY
sc.c_id;
```
2、查询选修“数据库原理”不及格的学生的学号、姓名、成绩
```
SELECT
sc.s_id AS 学号,
student.s_name AS 姓名,
sc.score AS 成绩
FROM
sc
INNER JOIN student ON sc.s_id = student.s_id
INNER JOIN course ON sc.c_id = course.c_id
WHERE
course.c_name = '数据库原理' AND
sc.score < 60;
```
3、查询和“崔晓”一个院系的其他学生的学号、姓名、院系
```
SELECT
s_id AS 学号,
s_name AS 姓名,
s_dept AS 院系
FROM
student
WHERE
s_dept = (SELECT s_dept FROM student WHERE s_name = '崔晓') AND
s_name != '崔晓';
```
4、查询信息技术学院年龄最大的学生的学号、姓名、性别
```
SELECT
s_id AS 学号,
s_name AS 姓名,
s_sex AS 性别
FROM
student
WHERE
s_dept = '信息技术学院'
ORDER BY
s_age DESC
LIMIT 1;
```
5、查询信息技术学院成绩在60分以下之间的学生的学号、课程号、成绩,其中查询结果先按照课程号升序排列,再按照成绩降序排列
```
SELECT
sc.s_id AS 学号,
sc.c_id AS 课程号,
sc.score AS 成绩
FROM
sc
INNER JOIN course ON sc.c_id = course.c_id
INNER JOIN student ON sc.s_id = student.s_id
WHERE
student.s_dept = '信息技术学院' AND
sc.score < 60
ORDER BY
sc.c_id ASC, sc.score DESC;
```
MySQL以数据库user_db及数据表student、teacher、class、course、sc为基础(11)查询信息技术学院的必修课和限选课的数量及平均学分; (12)查询每名学生的选课数量及平均成绩,按照平均成绩降序排列; (13)查询各门课程编号、选课人数及其及格率; (14)查询选课人数前五名的课程; (15)查询选修课程数最多和最少的学生的学号及选课数; 2、SQL子查询的应用; (1)查询信息技术学院年龄最大的学生的学号、姓名、性别; (2)查询每个院系年龄最大的学生的学号、姓名、性别、院系及年龄; (3)查询和“崔晓”一个院系的其他学生的学号、姓名、院系; (4)查询平均成绩在60分以上的课程及其平均成绩; (5)查询平均成绩大于“高莉”的平均成绩的学生学号、姓名及平均成绩; (6)查询课程的学分高于全体课程平均学分的课程编号、名称、学分及所属院系,查询结果按照学分降序排列; (7)查询至少有一名学生选修的所有课程的课程号及课程名;
11. 查询信息技术学院的必修课和限选课的数量及平均学分:
```
SELECT
SUM(CASE WHEN course_type = '必修' THEN 1 ELSE 0 END) AS required_course_count,
SUM(CASE WHEN course_type = '限选' THEN 1 ELSE 0 END) AS elective_course_count,
AVG(CASE WHEN course_type IN ('必修', '限选') THEN credit END) AS avg_credit
FROM
class
WHERE
department = '信息技术学院';
```
12. 查询每名学生的选课数量及平均成绩,按照平均成绩降序排列:
```
SELECT
sc.student_id,
COUNT(*) AS course_count,
AVG(score) AS avg_score
FROM
sc
JOIN
student ON student.id = sc.student_id
GROUP BY
sc.student_id
ORDER BY
avg_score DESC;
```
13. 查询各门课程编号、选课人数及其及格率:
```
SELECT
course_id,
COUNT(*) AS student_count,
SUM(CASE WHEN score >= 60 THEN 1 ELSE 0 END) / COUNT(*) AS pass_rate
FROM
sc
GROUP BY
course_id;
```
14. 查询选课人数前五名的课程:
```
SELECT
course_id,
COUNT(*) AS student_count
FROM
sc
GROUP BY
course_id
ORDER BY
student_count DESC
LIMIT 5;
```
15. 查询选修课程数最多和最少的学生的学号及选课数:
```
SELECT
student_id,
COUNT(*) AS course_count
FROM
sc
GROUP BY
student_id
HAVING
COUNT(*) = (SELECT MAX(cnt) FROM (SELECT COUNT(*) AS cnt FROM sc GROUP BY student_id) AS t)
OR COUNT(*) = (SELECT MIN(cnt) FROM (SELECT COUNT(*) AS cnt FROM sc GROUP BY student_id) AS t);
```
1. SQL子查询的应用:
(1) 查询信息技术学院年龄最大的学生的学号、姓名、性别:
```
SELECT
id, name, gender
FROM
student
WHERE
department = '信息技术学院' AND age = (SELECT MAX(age) FROM student WHERE department = '信息技术学院');
```
(2) 查询每个院系年龄最大的学生的学号、姓名、性别、院系及年龄:
```
SELECT
s.id, s.name, s.gender, s.age, s.department
FROM
student s
JOIN
(SELECT department, MAX(age) AS max_age FROM student GROUP BY department) t
ON
s.department = t.department AND s.age = t.max_age;
```
(3) 查询和“崔晓”一个院系的其他学生的学号、姓名、院系:
```
SELECT
id, name, department
FROM
student
WHERE
department = (SELECT department FROM student WHERE name = '崔晓') AND name != '崔晓';
```
(4) 查询平均成绩在60分以上的课程及其平均成绩:
```
SELECT
course_id,
AVG(score) AS avg_score
FROM
sc
WHERE
course_id IN (SELECT course_id FROM sc GROUP BY course_id HAVING AVG(score) >= 60)
GROUP BY
course_id;
```
(5) 查询平均成绩大于“高莉”的平均成绩的学生学号、姓名及平均成绩:
```
SELECT
id, name, AVG(score) AS avg_score
FROM
student
JOIN
sc ON student.id = sc.student_id
WHERE
AVG(score) > (SELECT AVG(score) FROM student JOIN sc ON student.id = sc.student_id WHERE name = '高莉')
GROUP BY
id, name;
```
(6) 查询课程的学分高于全体课程平均学分的课程编号、名称、学分及所属院系,查询结果按照学分降序排列:
```
SELECT
course.id, course.name, course.credit, course.department
FROM
course
WHERE
credit > (SELECT AVG(credit) FROM course)
ORDER BY
credit DESC;
```
(7) 查询至少有一名学生选修的所有课程的课程号及课程名:
```
SELECT
id, name
FROM
course
WHERE
id IN (SELECT DISTINCT course_id FROM sc);
```
阅读全文