之前接个毕设 二改项目的时候 写sql手法有点生疏故在网上资料再加上我的自己的想法
先刷这些sql题足够应付牛客和力扣的题目
链接:https://pan.baidu.com/s/1M0ut4VFCtT_7586Ll--Tag?pwd=8888
提取码:8888
链接:https://pan.baidu.com/s/1M0ut4VFCtT_7586Ll--Tag?pwd=8888 提取码:8888 --来自百度网盘超级会员V5的分享
-- 3.1.✨SQL 01——查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select student.*,
score1.s_score
from student,
score AS score1,
score AS score2
WHERE
score1.s_id = student.s_id
AND score1.s_id = score2.s_id
AND score1.c_id = '01'
AND score2.c_id = '02'
AND score1.s_score > score2.s_score;
-- 3.2.SQL 02——查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT student.*,
score1.s_score
FROM student,
score AS score1,
score AS score2
WHERE score1.s_id = student.s_id
AND score1.s_id = score2.s_id
AND score1.c_id = '01'
AND score2.c_id = '02'
AND score1.s_score < score2.s_score;
-- 3.3.SQL 03——查询平均成绩大于等于 60 分的同学的学生编号、学生姓名和平均成绩
# 1.创建临时表 ss
EXPLAIN SELECT
student.s_id,
student.s_name,
ss.avg_score
FROM
student,
(SELECT s_id, AVG(s_score) AS avg_score FROM score GROUP BY s_id) AS ss
WHERE
student.s_id = ss.s_id
AND ss.avg_score >= 60;
# 2.先进行内连接,然后再分组
SELECT
student.s_id,
s_name,
round(AVG(score.s_score), 2) as avg_score
FROM
student
INNER JOIN score ON student.s_id = score.s_id
GROUP BY
student.s_id,
s_name
HAVING
AVG(score.s_score) >= 60
--3.4.✨SQL 04——查询平均成绩小于 60 分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
select
student.s_id,
s_name,
round(AVG(score.s_score),2)
from
student
left outer join score on student.s_id = score.s_id
group by
student.s_id,
s_name
having avg(ifnull(score.s_score,0))<60
--3.5.SQL 05——查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select
student.s_id,
student.s_name,
count(distinct c_id) AS totalCourses,
SUM(s_score) as totalScores
from
student
left outer join score on student.s_id = score.s_id GROUP BY student.s_id,student.s_name;
--3.6.SQL 06——查询"李"姓老师的数量
# 1.模糊查询
SELECT
COUNT(*)
FROM
teacher
WHERE
t_name LIKE '李%'
# 2.正则表达式查询,字符 '^' 匹配以特定字符或者字符串开头的文本
SELECT
count(*)
FROM
teacher
WHERE
t_name REGEXP '^李'
-- 3.7.✨SQL 07——查询学过"张三"老师授课的同学的信息
# 1.使用多表连接(score, course, teacher)找到上张三老师课的同学的 s_id,然后再根据 s_id 从 student 表中查询同学信息
SELECT
student.*
FROM
student
WHERE
s_id IN (
SELECT
s_id
FROM
score,
course,
teacher
WHERE
teacher.t_name = '张三'
AND teacher.t_id = course.t_id
AND course.c_id = score.c_id
)
# 2.多层嵌套子查询(当数据量较大时,一般不推荐使用子查询)
# 在 student 表中根据上过张三老师教的课的学生 s_id 来查询他们的信息
SELECT
student.*
FROM
student
WHERE
student.s_id IN (
# 在 score 表中根据张三老师教的课程 c_id 来查找上这些课的学生 s_id
SELECT DISTINCT
s_id
FROM
score
WHERE
score.c_id IN (
# 在 course 表中根据张三老师的 t_id 查询他所教的课程 c_id
SELECT
c_id
FROM
course
WHERE
course.t_id = (
# 在 teacher 表中查询张三老师的 t_id
SELECT
t_id
FROM
teacher
WHERE
t_name = '张三'
)
)
)
--3.8.SQL 08——查询没学过"张三"老师授课的同学的信息
SELECT
student.*
FROM
student
WHERE
student.s_id NOT IN (
SELECT DISTINCT
s_id
FROM
score
WHERE
score.c_id IN (
SELECT
c_id
FROM
course
WHERE
course.t_id = (
SELECT
t_id
FROM
teacher
WHERE
t_name = '张三'
)
)
)
--3.9.SQL 09——查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select student.*
from student
where student.s_id IN ( select s1.s_id from score as s1 ,score as s2
where s1.s_id =s2.s_id AND s1.c_id = '01' and s2.c_id='02')
--3.10.✨SQL 10——查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT
stu.s_id,
stu.s_name,
stu.s_birth,
stu.s_sex
FROM
student AS stu
JOIN score AS sc ON stu.s_id = sc.s_id
JOIN course AS co ON co.c_id = sc.c_id
WHERE
co.c_id = '01'
AND stu.s_id NOT IN (
# 查询学过编号为 "02" 的课程的同学 id
SELECT
stu.s_id
FROM
student AS stu
JOIN score AS sc ON stu.s_id = sc.s_id
JOIN course AS co ON co.c_id = sc.c_id
WHERE
co.c_id = '02'
)
--3.11.SQL 11——查询没有学全所有课程的同学的信息
select *
from student where s_id IN (select s_id from score group by s_id having count(c_id)<(
select count(*) from course))
--3.12.SQL 12——查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select * from
student
where
s_id IN (select distinct s_id from score
where c_id IN (select c_id from score where s_id='01' ) AND s_id != '01')
--3.13.✨SQL 13——查询和"01"号的同学学习的课程完全相同的其他同学的信息
select *
from student
where s_id IN (select s_id from score where c_id IN (select distinct c_id
from score where s_id='01' ) AND s_id != '01' group by s_id HAVING count(c_id) =
(select count(*) from score where s_id = '01'))
--3.14.SQL 14——查询没学过"张三"老师讲授的任一门课程的学生姓名
select s_name
from student
where s_id NOT IN
(select s_id from score where c_id IN (select c_id from course where
t_id IN (select t_id from teacher where t_name ='张三' )))
--3.15.✨SQL 15——查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select stu.s_id,
stu.s_name, tmp_t.avg_score
from student as stu INNER JOIN (select
s_id , round(avg(s_score),2) as avg_score from score where s_score < 60
GROUP BY s_id having count(s_score)>=2) as tmp_t on stu.s_id = tmp_t.s_id
--3.16.SQL 16——检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT stu.*
from student as stu inner join score on stu.s_id = score.s_id
WHERE c_id = '01' and s_score < 60 ORDER by s_score Desc
--3.17.SQL 17——按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select s_id,
max(case c_id when '01' then s_score else 0 end ) as '01',
max(case c_id when '02' then s_score else 0 end ) as '02',
max(case c_id when '01' then s_score else 0 end ) as '03',
--3.18.✨SQL 18——查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程名称,最高分,最低分,平均分,及格率,中等率,优良率,优秀率。及格为 60-70,中等为 71-80,优良为 81-90,优秀为 >= 91。
select
sc.c_id as "课程ID",
c.c_name as "课程名",
MAX(sc.s_score) AS '最高分',
MIN(sc.s_score) AS '最低分',
AVG(sc.s_score) AS '平均分',
SUM(IF (sc.s_score between 60 and 70 , 1 ,0)) / count(*) as '及格率',
SUM(IF (sc.s_score between 71 and 80 , 1 ,0)) / count(*) as '中等率',
SUM(IF (sc.s_score between 81 and 90 , 1 ,0)) / count(*) as '优秀率',
SUM(IF (sc.s_score between 91 and 100 , 1 ,0)) / count(*) as '优秀率'
from
score as sc
join course as c on sc.c_id = c.c_id
GROUP BY sc.c_id
--3.19.✨SQL 19——按各科成绩进行排序,并显示排名,成绩重复时合并名次
select
sc1.c_id,
sc1.s_id,
sc1.s_score,
count(sc2.s_score)+1 AS rank
from
score as sc1 left join score as sc2
on sc1.s_score < sc2.s_score and sc1.c_id = sc2.c_id
GROUP BY
sc1.c_id,
sc1.s_id,
sc1.s_score
ORDER BY
sc1.c_id,
rank
--3.20.✨SQL 20——查询学生的总成绩并进行排名,总分重复时不保留名次空缺
SELECT
stu.s_id,
stu.s_name,
total_score,
(
SELECT COUNT(DISTINCT total_score)
FROM (SELECT SUM(s_score) AS total_score FROM score GROUP BY s_id) AS sub
WHERE total_score >= tmp.total_score
) AS rank
FROM
student as stu
INNER JOIN (
SELECT
s_id,
SUM(s_score) AS total_score
FROM
score
GROUP BY
s_id
) AS tmp ON stu.s_id = tmp.s_id
ORDER BY
total_score DESC;
-- 3.21.SQL 21——查询不同老师所教不同课程平均分,并从高到低显示
select
teacher.t_id,
t_name,
round(avg(s_score),2) AS avg_score
from
teacher,course,score
where
teacher.t_id = course.t_id AND course.c_id = score.c_id
GROUP BY
teacher.t_id, t_name, score.c_id
ORDER BY
avg(score.s_score) DESC
-- 3.22.SQL 22——查询所有课程的成绩第 2 名到第 3 名的学生信息及该课程成绩
# 1.分别对每门课程进行查询,然后再合并查询结果,但是如果课程太多,该方法就不太合适
SELECT
t1.*
FROM
(
SELECT
st.*,
c.c_id,
c.c_name,
sc.s_score
FROM
student st
LEFT JOIN score sc ON sc.s_id = st.s_id
INNER JOIN course c ON c.c_id = sc.c_id
AND c.c_id = "01"
ORDER BY
sc.s_score DESC
LIMIT 1,
2
) as t1
UNION ALL
SELECT
t2.*
FROM
(
SELECT
st.*,
c.c_id,
c.c_name,
sc.s_score
FROM
student st
LEFT JOIN score sc ON sc.s_id = st.s_id
INNER JOIN course c ON c.c_id = sc.c_id
AND c.c_id = "02"
ORDER BY
sc.s_score DESC
LIMIT 1,
2
) as t2
UNION ALL
SELECT
t3.*
FROM
(
SELECT
st.*,
c.c_id,
c.c_name,
sc.s_score
FROM
student st
LEFT JOIN score sc ON sc.s_id = st.s_id
INNER JOIN course c ON c.c_id = sc.c_id
AND c.c_id = "03"
ORDER BY
sc.s_score DESC
LIMIT 1,
2
) as t3
# 2.一次性查询,需要注意的是 row_number() 在 MySQL 8.0 中才支持
select c_id, student.*,s_score from student inner join (
SELECT s_id,s_score,c_id, row_number() over (PARTITION BY c_id ORDER BY s_score desc
) as rank from score) as tmp_t on tmp_t.s_id = student.s_id WHERE tmp_t.rank IN (2,3)
-- 3.23.✨SQL 23——统计各科成绩各分数段人数:课程编号,课程名称,\[100-85\],\[85-70\],\[70-60\],\[0-60\]及所占百分比
SELECT
score.c_id,
course.c_name,
sum(CASE WHEN s_score BETWEEN 0 AND 60 THEN 1 ELSE 0 END) AS '[0-60]人数',
sum(CASE WHEN s_score BETWEEN 61 AND 70 THEN 1 ELSE 0 END) AS '[61-70]人数',
sum(CASE WHEN s_score BETWEEN 71 AND 85 THEN 1 ELSE 0 END) AS '[71-85]人数',
sum(CASE WHEN s_score BETWEEN 86 AND 100 THEN 1 ELSE 0 END) AS '[86-100]人数',
round(sum(CASE WHEN s_score BETWEEN 0 AND 60 THEN 1 ELSE 0 END) / count(*), 2) AS '[0-60]人数所占百分比',
round(sum(CASE WHEN s_score BETWEEN 61 AND 70 THEN 1 ELSE 0 END) / count(*), 2) AS '[61-70]人数所占百分比',
round(sum(CASE WHEN s_score BETWEEN 71 AND 85 THEN 1 ELSE 0 END) / count(*), 2) AS '[71-85]人数所占百分比',
round(sum(CASE WHEN s_score BETWEEN 86 AND 100 THEN 1 ELSE 0 END) / count(*), 2) AS '[86-100]人数所占百分比'
FROM
score LEFT JOIN course
ON score.c_id = course.c_id
GROUP BY
score.c_id,
course.c_name
--3.24.SQL 24——查询学生平均成绩及其名次
# 在 MySQL 8 中可以使用 rank 函数来实现排名
SELECT
stu.s_id,
stu.s_name,
round(avg(sc.s_score), 2) AS average_score,
(
SELECT COUNT(DISTINCT avg_score)
FROM (SELECT AVG(s_score) AS avg_score FROM score GROUP BY s_id) AS sub
WHERE avg_score >= AVG(sc.s_score)
) AS rank
FROM
student as stu
INNER JOIN score as sc ON stu.s_id = sc.s_id
GROUP BY
stu.s_id,
stu.s_name
ORDER BY
average_score DESC;
--3.25.SQL 25——查询各科成绩前三名的记录
(SELECT c_id, s_score FROM score WHERE c_id = '01' ORDER BY s_score DESC LIMIT 3) UNION ALL
(SELECT c_id, s_score FROM score WHERE c_id = '02' ORDER BY s_score DESC LIMIT 3) UNION ALL
(SELECT c_id, s_score FROM score WHERE c_id = '03' ORDER BY s_score DESC LIMIT 3)
# 2.一次性查询出结果
SELECT DISTINCT
tmp_t.c_id,
tmp_t.s_score
FROM
(
SELECT DISTINCT
student.*,
sc1.c_id,
sc1.s_score,
count(DISTINCT sc2.s_score) + 1 AS rank
FROM
score AS sc1
LEFT JOIN score AS sc2 ON sc1.c_id = sc2.c_id
AND sc1.s_score < sc2.s_score
LEFT JOIN student ON sc1.s_id = student.s_id
GROUP BY
sc1.c_id,
sc1.s_id
ORDER BY
sc1.c_id,
sc1.s_score DESC
) AS tmp_t
WHERE
tmp_t.rank BETWEEN 1 AND 3
--3.26.SQL 26——查询每门课程被选修的学生数
select c_id, count(s_id) AS '选修该门课程的学生数'
from score group by c_id
3.27.SQL 27——查询出只有两门课程的全部学生的学号和姓名
SELECT
student.s_id,
student.s_name
FROM
student,
score
WHERE
student.s_id = score.s_id
GROUP BY
s_id
HAVING
count(c_id) = 2
--3.28.SQL 28——查询男生、女生人数
select sum(case when s_sex = '男' then 1 else null end) as '男生人数',
sum(case when s_sex = '女' then 1 else null end) as '女生人数' FROM student
--3.29.SQL 29——查询名字中含有"风"字的学生信息
--1.使用模糊匹配
SELECT * from student where s_name LIKE '%风%'
--2.使用正则表达式
select * from student where s_name REGEXP '风'
--3.30.✨SQL 30——查询同姓名同性别学生名单,并统计同名人数
SELECT
stu1.s_name,
tmp_t.cnt AS '同名人数'
FROM
student AS stu1
LEFT JOIN (
SELECT s_name, s_sex, count(*) AS cnt
FROM student
GROUP BY s_name, s_sex
) AS tmp_t
ON stu1.s_name = tmp_t.s_name AND stu1.s_sex = tmp_t.s_sex
WHERE
tmp_t.cnt > 1
--3.31.SQL 31——查询 1990 年出生的学生名单
select * from student where s_birth LIKE '1990%'
select * from student where s_birth regexp '^1990'
--3.32.✨SQL 32——查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select score.c_id, course.c_name, round(avg(s_score),2) as avg_score
from score,course
where score.c_id = course.c_id
GROUP BY
c_id ORDER BY
avg_score DESC,
c_id ASC
--3.33.SQL 33——查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select student.s_id,student.s_name,course.c_name,score.s_score
from student,course,score
where student.s_id = score.s_id and score.c_id = course.c_id
ORDER BY s_id
--3.36.SQL 36——查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
SELECT student.s_name,course.c_name,score.s_score
from student,course,score
where student.s_id = score.s_id
AND course.c_id = score.c_id
AND s_score > '70'
--3.37.SQL 37——查询不及格的课程的学生姓名、课程名称和分数
SELECT student.s_name,course.c_name,score.s_score
from student,course,score
where student.s_id = score.s_id
AND course.c_id = score.c_id
AND score.s_score < '60'
--3.38.SQL 38——查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
select student.s_id ,s_name
FROM student,score
where student.s_id = score.s_id
AND c_id = '01'
AND s_score >= '80'
--3.39.SQL 39——求每门课程的学生人数
select c_name,count(s_id) AS '学生人数'
from score,course where score.c_id = course.c_id GROUP BY
score.c_id
--3.40.✨SQL 40——查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
select student.* ,score.s_score
from student,score
where student.s_id = score.s_id
AND c_id IN (SELECT c_id FROM course WHERE t_id IN (SELECT t_id FROM teacher WHERE t_name = '张三'))
ORDER BY
s_score DESC
LIMIT 1
--3.41.SQL 41——查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT sc1.s_id,sc1.c_id,sc2.c_id,sc1.s_score,sc2.s_score
FROM score as sc1,
score as sc2
where
sc1.s_id = sc2.s_id AND sc1.s_score = sc2.s_score and sc1.c_id != sc2.c_id
--3.42.✨SQL 42——查询每门课程成绩最好的前两名
SELECT
sc1.c_id,
sc1.s_id,
count(sc2.s_score) + 1 AS rank
FROM
score AS sc1
LEFT JOIN score AS sc2 ON sc1.c_id = sc2.c_id
AND sc1.s_score < sc2.s_score
GROUP BY
sc1.c_id,
sc1.s_score,
sc1.s_id
HAVING
count(sc2.s_score) < 2
ORDER BY
sc1.c_id,
rank
--3.43.SQL 43——统计每门课程的学生选修人数(超过 5 人的课程才统计),要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT c_id ,count(*) as '选修人数'
from score group by c_id
HAVING count(*)>5
ORDER by '选修人数' DESC,c_id ASC
--3.44.SQL 44——查询至少选修两门课程的学生学号
select s_id FROM score GROUP BY s_id HAVING count(c_id) >= 2
--3.45.SQL 45——查询选修了全部课程的学生信息
SELECT
*
FROM
student
WHERE
# SELECT count(*) FROM course) 查询的是总课程的数量
s_id IN (SELECT s_id FROM score GROUP BY s_id HAVING count(c_id) = (SELECT count(*) FROM course))
--3.46.✨SQL 46——查询各学生的年龄
# 1.按照年份来计算
SELECT
s_id,
s_name,
(YEAR(now()) - YEAR(s_birth)) AS age
FROM
student
/*
2.使用 timestampdiff()
(1) TIMESTAMPDIFF(): 第一个参数设置时间单位,可以精确到年(YEAR)、天(DAY)、小时(HOUR),分钟(MINUTE)和秒(SECOND)。对于比较
的两个时间,时间小的放在前面,时间大的放在后面。
(3) datediff(): 返回值是相差的天数,无法定位到小时、分钟和秒。
*/
SELECT
s_id,
s_name,
timestampdiff(YEAR, s_birth, now()) AS age
FROM
student
--3.47.SQL 47——查询本周过生日的学生
# week(时间): 默认从 0 开始,表示星期天为一个星期的第一天,国外算法
# week(时间, 1): 从 1 开始,表示星期一为一个星期的第一天,国内算法
SELECT
s_id,
s_name
FROM
student
WHERE
WEEK (s_birth) = WEEK (now(), 1)
--3.48.SQL 48——查询下周过生日的学生
SELECT
s_id,
s_name
FROM
student
WHERE
WEEK (s_birth) = WEEK (now(), 1) + 1
--3.49.SQL 49——查询本月过生日的学生
SELECT
s_id,
s_name
FROM
student
WHERE
MONTH (s_birth) = MONTH (now())
--3.50.SQL 50——查询下月过生日的学生
SELECT
s_id,s_name
from student
where (MONTH(s_birth) = (((MONTH(NOW()) + 12) % 12) + 1))
-- SUM() 函数 SUM() 函数返回数值列的总数。
--AVG()函数:作用:平均值计算
--ROUND()函数:作用:round(x,d) 四舍五入取值;x是需要取舍的数值,d是取舍位置,默认d为0,当d大于0时,取舍小数点后d位;当d小于0时,取舍小数点前-d位。
--`INNER JOIN`是SQL中的一种连接类型,用于将两个或多个表中的记录根据某个条件进行匹配,并返回匹配的记录。它只返回那些在两个表中都有匹配的记录。
--GROUP BY 语句 GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组。
--having的用法:having字句可以让我们筛选成组后的各种数据,where字句在聚合前先筛选记录,也就是说作用在group by和having字句前。而 having子句在聚合后对组记录进行筛选。
-- LEFT JOIN 关键字 LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为 NULL。
--CASE 语句CASE语句遍历条件并在满足第一个条件时返回一个值(如IF-THEN-ELSE语句)。因此,一旦条件为真,它将停止读取并返回结果。如果没有条件为 true,则返回 ELSE 子句中的值。