根据上一篇文章建立的表,我们来做一些多表练习:
没建立表的可以点击此链接去建立练习用的表:
目录
12.查询被"Tom"和"Jerry"教的课程的最高分和最低分
15.查询课程编号为1且课程成绩在60分以上的学生的学号和姓名(子查询)
16. 查询平均成绩大于等于70的所有学生学号、姓名和平均成绩
19.查询每门课程的平均成绩,结果按照平均成绩降序排列,如果平均成绩相同,再按照课程编号升序排列
20.查询平均成绩大于60分的同学的学生编号和学生姓名和平均成绩
28.查询课程名称为"java",且分数低于60分的学生姓名和分数
进行student表和scores表的id相连接,course表和scores表的id相连接
- SELECT
- s.id sid,
- s.`name` sname,
- c.`name` cname,
- sc.score
- FROM
- student s
- LEFT JOIN scores sc ON s.id = sc.s_id
- LEFT JOIN course c ON c.id = sc.c_id
- WHERE
- s.id = 1;
- SELECT
- c.id,
- c.`name`,
- AVG( sc.score ),
- max( sc.score )
- FROM
- course c
- LEFT JOIN scores sc ON c.id = sc.c_id
- GROUP BY
- c.id,
- c.`name`;
- SELECT
- s.id,
- s.`name`,
- c.`name` cname,
- sc.score
- FROM
- student s
- LEFT JOIN scores sc ON sc.s_id = s.id
- LEFT JOIN course c ON c.id = sc.c_id
- WHERE
- s.`name` LIKE '张%';
- SELECT
- t.id,
- t.NAME,
- c.id,
- c.NAME,
- r.score
- FROM
- (
- SELECT
- s.id,
- s.NAME,(
- SELECT
- max( score )
- FROM
- scores r
- WHERE
- r.s_id = s.id
- ) score
- FROM
- student s
- ) t
- LEFT JOIN scores r ON r.s_id = t.id
- AND r.score = t.score
- LEFT JOIN course c ON r.c_id = c.id;
- SELECT
- *
- FROM
- student s
- WHERE
- id IN (
- SELECT DISTINCT
- r.s_id
- FROM
- (
- SELECT
- c.id,
- c.NAME,
- max( score ) score
- FROM
- student s
- LEFT JOIN scores r ON r.s_id = s.id
- LEFT JOIN course c ON c.id = r.c_id
- GROUP BY
- c.id,
- c.NAME
- ) t
- LEFT JOIN scores r ON r.c_id = t.id
- AND t.score = r.score
- );
- SELECT
- s.id,
- s.NAME sname,
- sc.score,
- c.NAME
- FROM
- student s
- LEFT JOIN scores sc ON s.id = sc.s_id
- LEFT JOIN course c ON sc.c_id = c.id
- WHERE
- s.NAME LIKE '%张%'
- OR s.NAME LIKE '%李%';
- SELECT
- *
- FROM
- student
- WHERE
- id IN (
- SELECT
- sc.s_id
- FROM
- scores sc
- GROUP BY
- sc.s_id
- HAVING
- avg( sc.score ) >= 70
- );
- SELECT
- s.id,
- s.NAME,
- sum( sc.score ) score
- FROM
- student s
- LEFT JOIN scores sc ON s.id = sc.s_id
- GROUP BY
- s.id,
- s.NAME
- ORDER BY
- score DESC,
- s.id ASC;
- SELECT
- c.NAME,
- max( sc.score ),
- min( sc.score ),
- avg( sc.score )
- FROM
- course c
- LEFT JOIN scores sc ON c.id = sc.c_id
- WHERE
- c.NAME = '数学';
- SELECT
- c.id,
- c.NAME,
- avg( sc.score ) score
- FROM
- course c
- LEFT JOIN scores sc ON c.id = sc.c_id
- GROUP BY
- c.id,
- c.NAME
- ORDER BY
- score DESC;
- SELECT
- t.id,
- t.NAME,
- c.id cid,
- c.NAME cname,
- avg( r.score )
- FROM
- teacher t
- LEFT JOIN course c ON t.id = c.t_id
- LEFT JOIN scores r ON r.c_id = c.id
- GROUP BY
- t.id,
- t.NAME,
- c.id,
- c.NAME;
- SELECT
- t.id,
- t.NAME,
- c.id cid,
- c.NAME cname,
- max( r.score ),
- min( r.score )
- FROM
- teacher t
- LEFT JOIN course c ON t.id = c.t_id
- LEFT JOIN scores r ON r.c_id = c.id
- GROUP BY
- t.id,
- t.NAME,
- c.id,
- c.NAME
- HAVING
- t.NAME IN ( 'Tom', 'Jerry' );
- SELECT
- t.id,
- t.sname,
- r.c_id,
- c.NAME,
- t.score
- FROM
- (
- SELECT
- s.id,
- s.NAME sname,
- max( r.score ) score
- FROM
- student s
- LEFT JOIN scores r ON r.s_id = s.id
- GROUP BY
- s.id,
- s.NAME
- ) t
- LEFT JOIN scores r ON r.s_id = t.id
- AND r.score = t.score
- LEFT JOIN course c ON r.c_id = c.id;
- SELECT
- s.id,
- s.NAME,
- c.id,
- c.NAME,
- r.score
- FROM
- student s
- LEFT JOIN scores r ON s.id = r.s_id
- LEFT JOIN course c ON c.id = r.c_id;
- SELECT
- s.*,
- r.*
- FROM
- student s
- LEFT JOIN scores r ON s.id = r.s_id
- WHERE
- r.c_id = 1
- AND r.score > 60
- SELECT
- s.id,
- s.NAME,
- t.score
- FROM
- student s
- LEFT JOIN ( SELECT r.s_id, avg( r.score ) score FROM scores r GROUP BY r.s_id ) t ON s.id = t.s_id
- WHERE
- t.score >= 70;
- SELECT
- *
- FROM
- student s
- WHERE
- id IN ( SELECT r.s_id FROM scores r GROUP BY r.s_id HAVING min( r.score ) < 60 );
- SELECT
- c.id,
- c.NAME,
- count(*)
- FROM
- course c
- LEFT JOIN scores r ON c.id = r.c_id
- GROUP BY
- c.id,
- c.NAME;
- SELECT
- c.id,
- c.NAME,
- avg( score ) score
- FROM
- course c
- LEFT JOIN scores r ON c.id = r.c_id
- GROUP BY
- c.id,
- c.NAME
- ORDER BY
- score DESC,
- c.id ASC;
- SELECT
- s.id,
- s.NAME sname,
- avg( r.score ) score
- FROM
- student s
- LEFT JOIN scores r ON r.s_id = s.id
- LEFT JOIN course c ON c.id = r.c_id
- GROUP BY
- s.id,
- s.NAME
- HAVING
- score > 65;
- SELECT
- s.id,
- s.NAME,
- s.gender
- FROM
- student s
- LEFT JOIN scores r ON s.id = r.s_id
- WHERE
- r.score > 80
- GROUP BY
- s.id,
- s.NAME,
- s.gender
- HAVING
- count(*) = 1;
- SELECT
- s.id,
- s.NAME,
- s.gender
- FROM
- student s
- LEFT JOIN scores r ON s.id = r.s_id
- GROUP BY
- s.id,
- s.NAME,
- s.gender
- HAVING
- count(*) = 3;
- SELECT
- *
- FROM
- course c
- WHERE
- id IN (
- SELECT
- r.c_id
- FROM
- scores r
- GROUP BY
- r.c_id
- HAVING
- min( r.score ) < 60
- );
- SELECT
- s.id,
- s.NAME
- FROM
- student s
- LEFT JOIN scores r ON s.id = r.s_id
- GROUP BY
- s.id,
- s.NAME
- HAVING
- count(*) >= 4;
- SELECT
- *
- FROM
- student
- WHERE
- id IN (
- SELECT
- r.s_id
- FROM
- scores r
- GROUP BY
- r.s_id
- HAVING
- count(*) != 5
- );
- SELECT
- s.id,
- s.NAME,
- count(*) number
- FROM
- student s
- LEFT JOIN scores r ON s.id = r.s_id
- GROUP BY
- s.id,
- s.NAME
- HAVING
- number = ( SELECT count(*) FROM course );
- SELECT
- s.id,
- s.NAME,
- count(*) number
- FROM
- student s
- LEFT JOIN scores r ON s.id = r.s_id
- GROUP BY
- s.id,
- s.NAME;
- SELECT
- s.id,
- s.NAME,
- r.score
- FROM
- student s
- LEFT JOIN scores r ON s.id = r.s_id
- LEFT JOIN course c ON r.c_id = c.id
- WHERE
- c.NAME = 'java'
- AND r.score < 60;
- SELECT
- s.id,
- s.NAME
- FROM
- student s
- LEFT JOIN scores r ON r.s_id = s.id
- LEFT JOIN course c ON c.id = r.c_id
- LEFT JOIN teacher t ON t.id = c.t_id
- WHERE
- t.NAME = 'Tom';
- SELECT
- *
- FROM
- student
- WHERE
- id NOT IN (
- SELECT DISTINCT
- s.id
- FROM
- student s
- LEFT JOIN scores r ON r.s_id = s.id
- LEFT JOIN course c ON c.id = r.c_id
- LEFT JOIN teacher t ON t.id = c.t_id
- WHERE
- t.NAME = 'Tom'
- )