Mysql50句练习上
前言: 故不积跬步,无以至千里
安装地址
https://www.runoob.com/mysql/mysql-install.html
scrpit:
1 | #!/bin/sh |
sql 答案参考这里
1 | 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数 |
2019.7.23
– 1、查询”01”课程比”02”课程成绩高的学生的信息及课程分数
1 | 1. 个人思路: |
– 2、查询”01”课程比”02”课程成绩低的学生的信息及课程分数
1 | SELECT |
– 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
1 | SELECT |
SELECT
st.*,
avg.avg
FROM
Student st
INNER JOIN ( SELECT s_id, avg( s_score ) AS avg FROM Score GROUP BY s_id ) avg ON st.s_id = avg.s_id
AND avg.avg < 60
ORDER BY
avg.avg DESC
1 | --- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩 |
SELECT
s_name,
res.*
FROM
Student st
LEFT JOIN ( SELECT s_id, COUNT( c_id ) AS ‘选课总数’, SUM( s_score ) AS ‘课程总成绩’ FROM Score GROUP BY s_id ) res ON st.s_id = res.s_id
1 | -- 20200422 (6-10) |
SELECT
COUNT( DISTINCT t_id )
FROM
Teacher
WHERE
t_name LIKE ‘李%’
1 | -- 7、查询学过"张三"老师授课的同学的信息 |
SELECT
st.*
FROM
Student st,
Score sc,
Teacher tc,
Course cs
WHERE
tc.t_id = cs.t_tid
AND tc.t_name = ‘张三’
AND sc.c_id = cs.c_id
AND st.s_id = sc.s_id
1 | -- 8、查询没学过"张三"老师授课的同学的信息 |
SELECT
st.*
FROM
Student st
WHERE
st.s_id NOT IN (
SELECT
sc.s_id
FROM
Score sc,
Teacher tc,
Course cs
WHERE
tc.t_id = cs.t_tid
AND tc.t_name = ‘张三’
AND sc.c_id = cs.c_id)
1 | -- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息 |
SELECT
st.*
FROM
Score a,
Score b,
Student st
WHERE
a.c_id = ‘01’
AND b.c_id = ‘02’
AND a.s_id = b.s_id
AND st.s_id = a.s_id
AND st.s_id = b.s_id
1 | -- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息 |
SELECT
st.*
FROM
Student st,
Score sc
WHERE
sc.s_id NOT IN ( SELECT s_id FROM Score WHERE c_id = ‘02’ )
AND sc.s_id = st.s_id
GROUP BY
sc.s_id
1 | -- 11、查询没有学全所有课程的同学的信息 |
SELECT
st.*
FROM
Student st
LEFT JOIN Score sc ON st.s_id = sc.s_id
GROUP BY
sc.s_id
HAVING
COUNT( sc.c_id ) <3
1 | -- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 |
SELECT
a.s_id
FROM
Student st,
Score a,
Score b
WHERE
a.s_id = st.s_id
AND st.s_id <> ‘01’
AND b.s_id = ‘01’
AND a.c_id = b.c_id
GROUP BY
a.s_id
HAVING
COUNT( a.c_id ) = 3
1 | -- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名 |
SELECT
st1.*
FROM
Student st1
WHERE
st1.s_id NOT IN (
SELECT
st.s_id
FROM
Student st,
Score sc,
Course cs,
Teacher tc
WHERE
st.s_id = sc.s_id
AND sc.c_id = cs.c_id
AND tc.t_id = cs.t_tid
AND tc.t_name = ‘张三’
);
1 | -- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 |
SELECT
st.*,
avg( sc.s_score ) AS avg,
COUNT( sc.c_id ) AS count
FROM
Score sc,
Student st
WHERE
sc.s_id = st.s_id
AND s_score < 60 GROUP BY s_id HAVING count >=2
1 | -- 21、查询不同老师所教不同课程平均分从高到低显示 |
SELECT
c_id,
AVG( s_score ) AS avg
FROM
Teacher tc,
Score
GROUP BY
c_id
ORDER BY
avg DESC
1 | -- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩 |
SELECT
*
FROM
(
SELECT
st.s_id,
st.s_name,
st.s_sex,
st.s_birth,
sc.c_id,
sc.s_score
FROM
Student st,
Score sc
WHERE
st.s_id = sc.s_id
AND sc.c_id = ‘01’
ORDER BY
sc.s_score DESC
LIMIT 1,
2
) a UNION ALL
SELECT
*
FROM
(
SELECT
st.s_id,
st.s_name,
st.s_sex,
st.s_birth,
sc.c_id,
sc.s_score
FROM
Student st,
Score sc
WHERE
st.s_id = sc.s_id
AND sc.c_id = ‘02’
ORDER BY
sc.s_score DESC
LIMIT 1,
2
) b UNION ALL
SELECT
*
FROM
(
SELECT
st.s_id,
st.s_name,
st.s_sex,
st.s_birth,
sc.c_id,
sc.s_score
FROM
Student st,
Score sc
WHERE
st.s_id = sc.s_id
AND sc.c_id = ‘03’
ORDER BY
sc.s_score DESC
LIMIT 1,
2
) c
1 | -- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比 |
SELECT
c.c_id,
c.c_name,((
SELECT
count( 1 )
FROM
Score sc
WHERE
sc.c_id = c.c_id
AND sc.s_score <= 100 AND sc.s_score > 80
)/(
SELECT
count( 1 )
FROM
Score sc
WHERE
sc.c_id = c.c_id
)) “100-85”,((
SELECT
count( 1 )
FROM
Score sc
WHERE
sc.c_id = c.c_id
AND sc.s_score <= 85 AND sc.s_score > 70
)/(
SELECT
count( 1 )
FROM
Score sc
WHERE
sc.c_id = c.c_id
)) “85-70”,((
SELECT
count( 1 )
FROM
Score sc
WHERE
sc.c_id = c.c_id
AND sc.s_score <= 70 AND sc.s_score > 60
)/(
SELECT
count( 1 )
FROM
Score sc
WHERE
sc.c_id = c.c_id
)) “70-60”,((
SELECT
count( 1 )
FROM
Score sc
WHERE
sc.c_id = c.c_id
AND sc.s_score <= 60 AND sc.s_score >= 0
)/(
SELECT
count( 1 )
FROM
Score sc
WHERE
sc.c_id = c.c_id
)) “60-0”
FROM
Course c
ORDER BY
c.c_id
1 | -- 24、查询学生平均成绩及其名次 |
SET @rownum = 0;
SELECT
@rownum := @rownum + 1 AS rownum,
a.*
FROM
( SELECT s_id, AVG( s_score ) FROM Score GROUP BY s_id ORDER BY AVG( s_score ) DESC ) a
1 | -- 25、查询各科成绩前三名的记录 |
SELECT
*
FROM
(
SELECT
st.s_id,
st.s_name,
st.s_sex,
st.s_birth,
sc.c_id,
sc.s_score
FROM
Student st,
Score sc
WHERE
st.s_id = sc.s_id
AND sc.c_id = ‘01’
ORDER BY
sc.s_score DESC
LIMIT 3
) a UNION ALL
SELECT
*
FROM
(
SELECT
st.s_id,
st.s_name,
st.s_sex,
st.s_birth,
sc.c_id,
sc.s_score
FROM
Student st,
Score sc
WHERE
st.s_id = sc.s_id
AND sc.c_id = ‘02’
ORDER BY
sc.s_score DESC
LIMIT 3
) b UNION ALL
SELECT
*
FROM
(
SELECT
st.s_id,
st.s_name,
st.s_sex,
st.s_birth,
sc.c_id,
sc.s_score
FROM
Student st,
Score sc
WHERE
st.s_id = sc.s_id
AND sc.c_id = ‘03’
ORDER BY
sc.s_score DESC
LIMIT 3
) c
```