当前位置:网站首页>MYSQL50道基础练习题
MYSQL50道基础练习题
2022-04-23 04:06:00 【解忧杂货铺Q】
数据
-- 学生
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);
学生
老师
课程
分数
01
查询"01"课程比"02"课程成绩高的学生的信息及课程分数
先查询01课程
再查询02
交集
SELECT
*
FROM
(SELECT * FROM SC WHERE cid='01') a
JOIN
(SELECT * FROM SC WHERE cid='02') b on a.sid = b.sid
增加限制条件,01分数 > 02分数
关联查询课程信息
SELECT
*
FROM
(SELECT * FROM SC WHERE cid='01') a
JOIN
(SELECT * FROM SC WHERE cid='02') b on a.sid = b.sid and a.score > b.score
JOIN
Course c on a.cid = c.cid
JOIN
Course d on b.cid = d.cid
关联查询学生信息
SELECT
*
FROM
(SELECT * FROM SC WHERE cid='01') a
JOIN
(SELECT * FROM SC WHERE cid='02') b on a.sid = b.sid and a.score > b.score
JOIN
Course c on a.cid = c.cid
JOIN
Course d on b.cid = d.cid
JOIN
Student e on a.sid = e.sid
汇总
-- 01
SELECT
e.sid,
e.sname,
e.sage,
e.ssex,
a.cid AS cid01,
c.cname AS cname01,
a.score AS cscore01,
b.cid AS cid02,
d.cname AS cname02,
b.score AS cscore02
FROM
( SELECT * FROM SC WHERE cid = '01' ) a
JOIN ( SELECT * FROM SC WHERE cid = '02' ) b ON a.sid = b.sid AND a.score > b.score
JOIN Course c ON a.cid = c.cid
JOIN Course d ON b.cid = d.cid
JOIN Student e ON a.sid = e.sid
02
查询学生选课存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null)
SELECT
*
FROM
( SELECT * FROM SC WHERE cid = '01' ) a
LEFT JOIN ( SELECT * FROM SC WHERE cid = '02' ) b ON a.sid = b.sid
03
查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
先查询平均分
SELECT
*,
AVG(score) avg
FROM sc
GROUP BY sc.sid
HAVING avg >= 60
再多表关联查询
SELECT
a.sid, b.sname, a.avg
FROM
( SELECT *, AVG( score ) avg FROM sc GROUP BY sc.sid HAVING avg >= 60 ) a
JOIN Student b ON a.sid = b.sid
版权声明
本文为[解忧杂货铺Q]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_26003101/article/details/124345410
边栏推荐
- What is software acceptance testing? What are the benefits of acceptance testing conducted by third-party software testing institutions?
- Shopping mall for transportation tools based on PHP
- Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
- Set经典小题目
- 知乎有问题,谁来解答?
- CRF based medical entity recognition baseline
- Retrieval question answering system baseline
- The difference between lists, tuples, dictionaries and collections
- [AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022
- MySQL 2013 lost connection to MySQL server during query
猜你喜欢
随机推荐
Writing latex with vscode - the latest tutorial 2022 / 4 / 17
Why recommend you to study embedded
Retrieval question answering system baseline
The super large image labels in remote sensing data set are cut into specified sizes and saved into coco data set - target detection
[AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022
Summary of knowledge map (3)
Xshell、Xftp连接新创建的Unbutu系统虚拟机全流程
Matlab minimalist configuration of vscode configuration
[echart] démarrer avec echart
ROS series (III): introduction to ROS architecture
Why is it necessary to divide the variance by 255^2 when adding Gaussian noise using the imnoise function of MATLAB
[BIM introduction practice] Revit building wall: detailed picture and text explanation of structure, envelope and lamination
Numpy's broadcasting mechanism (with examples)
减治思想——二分查找详细总结
【Echart】echart 入門
LabVIEW 小端序和大端序区别
C语言 字符常量
QT program integration easyplayer RTSP streaming media player screen flicker what is the reason?
[latex] formula group
使用大华设备开发行AI人流量统计出现时间不正确的原因分析