当前位置:网站首页>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
边栏推荐
- 创下国产手机在海外市场销量最高纪录的小米,重新关注国内市场
- 知乎有问题,谁来解答?
- Xiaohongshu was exposed to layoffs of 20% as a whole, and the internal volume among large factories was also very serious
- Let matlab2018b support the mex configuration of vs2019
- /etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
- Basic usage of Google colab (I)
- 无线键盘全国产化电子元件推荐方案
- STM32单片机ADC规则组多通道转换-DMA模式
- C language character constant
- [AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
猜你喜欢

【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..

STM32F4单片机ADC采样及ARM-DSP库的FFT
![[AI vision · quick review of robot papers today, issue 29] Mon, 14 Feb 2022](/img/a3/88b20f3e1be702f580169400e417f4.png)
[AI vision · quick review of robot papers today, issue 29] Mon, 14 Feb 2022

ROS series (IV): ROS communication mechanism series (3): parameter server
![[latex] differences in the way scores are written](/img/77/3aebc8812f3e2d239187b95d3bd49f.png)
[latex] differences in the way scores are written

ROS series (I): rapid installation of ROS

AI CC 2019 installation tutorial under win10 (super detailed - small white version)

为什么推荐你学嵌入式

Qt程序集成EasyPlayer-RTSP流媒体播放器出现画面闪烁是什么原因?

ROS series (IV): ROS communication mechanism series (6): parameter server operation
随机推荐
[mapping program design] coordinate azimuth calculation artifact (version C)
洛谷P1858 【多人背包】 (背包求前k优解)
Installation and configuration of MinGW under win10
Cuda11 is installed perfectly in win10 X + pytorch 1.9 (blood flowing into the river) cuda. is_ Available() becomes true!
单片机串口数据处理(1)——串口中断发送数据
[string] ranking of country names ----- problem solving notes
STM32 MCU ADC rule group multi-channel conversion DMA mode
[Li Hongyi 2022 machine learning spring] hw6_ Gan (don't understand...)
The difference between lists, tuples, dictionaries and collections
Network principle | connection management mechanism in TCP / IP important protocol and core mechanism
ROS series (IV): ROS communication mechanism series (6): parameter server operation
C语言:恶搞小游戏
现货黄金操作技巧_估波曲线
Set经典小题目
Opencv -- yoact case segmentation model reasoning
Cause analysis of incorrect time of AI traffic statistics of Dahua Equipment Development Bank
Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation
Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)
Qt程序集成EasyPlayer-RTSP流媒体播放器出现画面闪烁是什么原因?
The great gods in acmer like mathematics very much
