题目:设计以下表,完成相关问题SQL编写。
学生表 student (sno,sname,sage,ssex)
课程表 course (cno,cname,tno)
成绩表 sc (sno,cno,grade)
教师表 teacher(tno,tname)
问题:
1.查询课程1的成绩比课程2的成绩高的所有学生的学号
2.查询平均成绩大于70分的同学的学号和平均成绩
3.查询所有同学的学号、姓名、选课数、总成绩
4.查询姓“张”老师的个数
5.查询没学过“张甲”老师课的同学的学号和姓名
6.查询同时学过课程1和课程2的同学的学号和姓名
7.查询学过“李丁”老师所教的所有课程的所有同学的学号和姓名
8.查询课程编号1的成绩比课程编号2的成绩高的所有同学的学号和姓名
9.查询至少有一门课程与学号为1的同学所学课程相同的同学的学号和姓名
表结构及数据如下:
学生表 student (sno,sname,sage,ssex)
原始数据
课程表 course (cno,cname,tno)
成绩表 sc (sno,cno,grade)
教师表 teacher(tno,tname)
1.查询课程1的成绩比课程2的成绩高的所有学生的学号
SELECT
c1.sno as "学号"
FROM
( SELECT
t.*
FROM
sc t
WHERE
t.cno = 1 ) c1,
( SELECT
c.*
FROM
sc c
WHERE
c.cno = 2 ) c2
WHERE
c1.sno = c2.sno
AND c1.grade > c2.grade
SELECT
sno,
AVG(grade)
FROM
sc
GROUP BY
sno
HAVING
AVG(grade) > 70
SELECT
z."学号",
g.sname AS "姓名",
z."选课数",
z."总分数"
FROM
( SELECT
s.sno AS "学号",
COUNT(s.cno) AS "选课数",
SUM(s.grade) AS "总分数"
FROM
course c,
sc s
WHERE
c.cno = s.cno
GROUP BY
s.sno
ORDER BY
"总分数" DESC ) z,
student g
WHERE
z."学号" = g.sno
SELECT
COUNT(1) AS "个数"
FROM
teacher
WHERE
tname LIKE '张%'
SELECT
sno AS "学号",
sname AS "姓名"
FROM
student
WHERE
sno NOT IN (
SELECT
g.sno
FROM
sc s,
student g
WHERE
s.cno IN (
SELECT
c.cno
FROM
course c,
teacher t
WHERE
c.tno = t.tno
AND t.tname = '张甲'
)
AND s.sno = g.sno
)
SELECT
g.sno AS "学号",
g.sname AS "姓名"
FROM
student g,
( SELECT
*
FROM
sc
WHERE
cno = 1 ) t
WHERE
t.sno IN (
SELECT
sno
FROM
sc
WHERE
cno = 2
)
AND g.sno = t.sno
7.查询学过“李丁”老师所教的所有课程的所有同学的学号和姓名
SELECT
sno AS "学号",
sname AS "姓名"
FROM
student
WHERE
sno in (
SELECT
p.sno
FROM
(SELECT
*
FROM
sc
WHERE
cno=4) p,
(SELECT
*
FROM
sc
WHERE
cno=5) k
WHERE
p.sno=k.sno
)
8.查询课程编号1的成绩比课程编号2的成绩高的所有同学的学号和姓名
SELECT
g.sno AS "学号",
g.sname AS "姓名"
FROM
student g,
(SELECT
*
FROM
sc
WHERE
cno=1) g1,
(SELECT
*
FROM
sc
WHERE
cno=2) g2
WHERE
g1.sno=g2.sno
AND g1.grade>g2.grade
AND g1.sno=g.sno
9.查询至少有一门课程与学号为1的同学所学课程相同的同学的学号和姓名
SELECT
DISTINCT g1.sno AS "学号",
g1.sname AS "姓名"
FROM
student g1,
sc g2
WHERE
g1.sno=g2.sno
AND g2.cno in (
SELECT
cno
FROM
sc
WHERE
sno=1
)