不断补充学习中,持续更新
2016.12.12,13
gruad
为了查看语句运行是否正确,建立如下三张表
1.以查询语句为主
table student 学生表
学号:学生姓名:年龄:性别:系
table course 课程表
课程号:课程名:学时
table sc 学生选课表
学号:课程号:成绩分数
- --新增表sc的结构,增加一列hours 数据类型INT
ALTER TABLE sc ADD COLUMN hours INT ;
运行结果:
2.--查询成绩在70-80之间的学生学号,和课程号,和成绩
SELECT sno,cno,grade FROMsc
WHERE grade between 70 AND 80;
运行结果:
3.-- 查询c03课程成绩最高的分数
SELECT grade
FROM sc
WHERE cno='c03'
ORDER BY grade DESC
LIMIT 1;
运行结果:
4.-- 查询学生都选择了那些课程,列出课程名和课程号
SELECT cname ,cno
FROM course
WHERE cno in(SELECT DISTINCT cno FROM sc);
运行结果:
5.-- 选择选修了c03课程的所有学生的平均成绩,最高成绩,最低成绩
SELECT AVG( grade),MAX(grade),MIN(grade)
FROM sc
WHERE cno='c03';
运行结果:
6.-- 统计每个系的学生人数
SELECT sdept,COUNT(*)
FROM student
WHERE sdept in(SELECT DISTINCT sdept from student)
GROUP BY sdept;
运行结果:
7.-- 统计每门课的平均成绩
select cname,AVG( grade)
FROM sc,course
WHERE sc.cno in(SELECT cno FROM sc) and sc.cno=course.cno
GROUP BY course.cname;
运行结果:
8.-- 统计每门课程的修课人数和考试最高分
SELECT cname,COUNT(*),MAX(grade)
FROM course,sc
WHERE sc.cno in(SELECT DISTINCT cno FROM sc) AND sc.cno=course.cno
GROUP BY course.cname;
运行结果:
9.-- 统计每个学生的选课门数,并按选课门数的递增顺序显示结果。
SELECT student.sname,student.sno,COUNT(sc.sno)
FROM student,sc
WHERE student.sno=sc.sno
GROUP BY student.sname,student.sno
ORDER BY COUNT(sc.sno) ASC;
或:
SELECT student.sname,student.sno,COUNT(sc.sno)
from student
INNER JOIN sc ON sc.sno=student.sno
GROUP BY student.sname,student.sno
ORDER BY COUNT(sc.sno);
运行结果:
10.-- 统计选修课的学生总数和考试的平均成绩
SELECT COUNT(DISTINCT sno),AVG(grade)
FROM sc;
运行结果:
11.-- 查询选课门数超过1门的学生的平均成绩和选课门数
SELECT student.sname,AVG(sc.grade),COUNT(sc.sno)
FROM sc
join student on (sc.sno=student.sno)
join course on (sc.cno=course.cno)
GROUP BY student.sname
HAVING COUNT(DISTINCT course.cno)>1
运行结果:
12.-- 列出总成绩超过150分的学生,要求列出学号、总成绩
SELECT sno,SUM(grade)
FROM sc
GROUP BY sno
HAVING SUM(grade)>150;
运行结果:
13.-- 查询选修了c02号课程的学生的姓名和所在系
SELECT student.sname,student.sdept
FROM student
INNER JOIN sc on sc.sno=student.sno
where sc.cno='c02';
运行结果:
select FirstName as Family, LastName as Name
from Persons
待添加中...