前言
最近博主在准备数据分析岗位的笔试和面试,其中的考察重点就是SQL。听说练了这50道题目(题目和数据来源参考:互联网校招SQL笔试经典50题及答案解析),什么现场手撕SQL统统都不怕,我就赶紧来练习了!壮士们,练完以后就什么都不怕啦~
我看了原博主的查询语句,有些不够简洁,有些或许存在问题。下面都是我自己写的查询语句,保证在Navicat上跑得通,但是不排除存在写的不对不好的地方,欢迎大家提出来,一起学习,共同进步。
环境:
Navicat Premium 12
说明:
本篇练习的是题目21-30
其余题目请参考链接:
SQL笔试经典50题及答案解析(题目1-10)
SQL笔试经典50题及答案解析(题目11-20)
SQL笔试经典50题及答案解析(题目31-40)
SQL笔试经典50题及答案解析(题目41-50)
本次创建的表有四张:
# 创建学生表
create table Student(sid varchar(10),sname varchar(10),sage datetime,ssex nvarchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
# 创建课程表
create table Course(cid varchar(10),cname varchar(10),tid varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
# 创建教师表
create table Teacher(tid varchar(10),tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
# 创建成绩表
create table SC(sid varchar(10),cid varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
学生表如下:
教师表如下:
成绩表如下:
课程表如下:
查询不同老师所教不同课程平均分从高到低显示
SELECT tid,AVG(score)AS avg_score
FROM sc
LEFT JOIN course AS c
ON sc.cid = c.cid
GROUP BY c.tid
ORDER BY avg_score DESC
查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
# 我习惯考虑并列的情况,所以此处使用密集排名dense_rank
SELECT * FROM
(
SELECT *,dense_rank() over (PARTITION BY cid ORDER BY score DESC) AS srank
FROM sc
) AS t
WHERE t.srank=2 OR t.srank=3
统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
SELECT
sc.cid
,cname
,SUM(CASE WHEN score>=85 AND score<=100 THEN 1 ELSE 0 END)/COUNT(score) AS '[85-100]'
,SUM(CASE WHEN score>=70 AND score<85 THEN 1 ELSE 0 END)/COUNT(score) AS '[70-85 )'
,SUM(CASE WHEN score>=60 AND score<70 THEN 1 ELSE 0 END)/COUNT(score) AS '[60-70 )'
,SUM(CASE WHEN score>=0 AND score<60 THEN 1 ELSE 0 END)/COUNT(score) AS '[0 -60 )'
FROM sc
LEFT JOIN course AS c
ON sc.cid = c.cid
GROUP BY sc.cid
查询学生平均成绩及其名次
SELECT
sid
,AVG(score) AS avg_score
,dense_rank() over (ORDER BY AVG(score) DESC) AS srank
FROM sc
GROUP BY sid
查询各科成绩前三名的记录
# 我习惯考虑并列的情况,所以此处使用密集排名dense_rank
SELECT cid,sid,score,srank FROM
(
SELECT *,dense_rank() over (PARTITION BY cid ORDER BY score DESC) AS srank
FROM sc
) AS t
WHERE t.srank<=3
查询每门课程被选修的学生数
SELECT cid,COUNT(DISTINCT sid) AS cnt
FROM sc
GROUP BY cid
查询出只选修了一门课程的全部学生的学号和姓名
SELECT sc.sid,sname
FROM sc
LEFT JOIN student AS s
ON sc.sid = s.sid
GROUP BY sc.sid
HAVING COUNT(DISTINCT cid)=1
查询男生、女生人数
方法1: 使用sum 和 if函数
SELECT
sum(if(ssex='男',1,0)) AS '男生人数'
,sum(if(ssex='女',1,0)) AS '女生人数'
FROM student
# 方法2:
SELECT ssex,COUNT(ssex)
FROM student
GROUP BY ssex
运行结果:
查询名字中含有"风"字的学生信息
SELECT *
FROM student
WHERE sname LIKE '%风%'
查询同名同性学生名单,并统计同名人数
SELECT
sname
,COUNT(sname) AS cnt
FROM student
GROUP BY sname
HAVING COUNT(sname)>=2