当前位置:网站首页>MySQL -- classic interview questions
MySQL -- classic interview questions
2022-06-23 18:22:00 【zyanwei2018】
List of articles
1. Basic data creation
-- Building database
create database if not exists school
default charset utf8
COLLATE utf8_general_ci;
-- Student list
CREATE TABLE `student` (
`s_no` VARCHAR(20) NOT NULL PRIMARY KEY,
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT ''
);
-- Teachers list
CREATE TABLE `teacher` (
`t_no` VARCHAR(20) NOT NULL PRIMARY KEY,
`t_name` VARCHAR(20) NOT NULL DEFAULT ''
);
-- The curriculum
CREATE TABLE `course` (
`c_no` VARCHAR(20) NOT NULL PRIMARY KEY,
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_no` varchar(20) NOT NULL
);
-- Score table
CREATE TABLE `score` (
`s_no` VARCHAR(20) NOT NULL,
`c_no` VARCHAR(20) NOT NULL,
`s_score` INT(3),
constraint pk_sc PRIMARY KEY (`s_no`, `c_no`)
);
-- insert data
INSERT INTO student (s_no, s_name, s_birth, s_sex)
VALUES
('s202201', ' Zhao Lei ', '1990-01-01', ' male '),
('s202202', ' Qian Dian ', '1990-12-21', ' male '),
('s202203', ' Sun Feng ', '1990-05-20', ' male '),
('s202204', ' Li Yun ', '1990-08-06', ' male '),
('s202205', ' Zhou Mei ', '1991-12-01', ' Woman '),
('s202206', ' Wu Lan ', '1992-03-01', ' Woman '),
('s202207', ' Zheng Zhu ', '1989-07-01', ' Woman '),
('s202208', ' Wangju ', '1990-01-20', ' Woman ');
INSERT INTO teacher (t_no, t_name)
VALUES
('t202201', ' Zhang San '),
('t202202', ' Li Si '),
('t202203', ' Wang Wu ');
INSERT INTO course (c_no, c_name, t_no)
VALUES
('c202201', ' Chinese language and literature ', 't202202'),
('c202202', ' mathematics ', 't202201'),
('c202203', ' English ', 't202203');
INSERT INTO score VALUES
('s202201', 'c202201', 80),
('s202201', 'c202202', 90),
('s202201', 'c202203', 99),
('s202202', 'c202201', 70),
('s202202', 'c202202', 60),
('s202202', 'c202203', 80),
('s202203', 'c202201', 80),
('s202203', 'c202202', 80),
('s202203', 'c202203', 80),
('s202204', 'c202201', 50),
('s202204', 'c202202', 30),
('s202204', 'c202203', 20),
('s202205', 'c202201', 76),
('s202205', 'c202202', 87),
('s202206', 'c202201', 31);
2. Table data replication
insert into teacher(t_name) select t_name from teacher;
3. Single table multi row consolidation
- Original table
mysql> select * from score;
+---------+---------+---------+
| s_no | c_no | s_score |
+---------+---------+---------+
| s202201 | c202201 | 80 |
| s202201 | c202202 | 90 |
| s202201 | c202203 | 99 |
| s202202 | c202201 | 70 |
| s202202 | c202202 | 60 |
| s202202 | c202203 | 80 |
| s202203 | c202201 | 80 |
| s202203 | c202202 | 80 |
| s202203 | c202203 | 80 |
| s202204 | c202201 | 50 |
| s202204 | c202202 | 30 |
| s202204 | c202203 | 20 |
| s202205 | c202201 | 76 |
| s202205 | c202202 | 87 |
| s202206 | c202201 | 31 |
+---------+---------+---------+
- Merge multiple rows into one column
# Show the results of different subjects of the same student as 1 That's ok , Separate by semicolons
select s_no,
group_concat(s_score Separator ';') as score
from score
group by s_no;
+---------+----------+
| s_no | score |
+---------+----------+
| s202201 | 80;90;99 |
| s202202 | 70;60;80 |
| s202203 | 80;80;80 |
| s202204 | 50;30;20 |
| s202205 | 76;87 |
| s202206 | 31 |
+---------+----------+
- Merge multiple rows into multiple columns
# Show the results of different subjects of the same student as 1 That's ok , By subject
select s_no,
MAX(CASE c_no WHEN 'c202201' THEN s_score ELSE 0 END ) Chinese language and literature ,
MAX(CASE c_no WHEN 'c202202' THEN s_score ELSE 0 END ) mathematics ,
MAX(CASE c_no WHEN 'c202203' THEN s_score ELSE 0 END ) English
from score group by s_no;
+---------+--------+--------+--------+
| s_no | Chinese language and literature | mathematics | English |
+---------+--------+--------+--------+
| s202201 | 80 | 90 | 99 |
| s202202 | 70 | 60 | 80 |
| s202203 | 80 | 80 | 80 |
| s202204 | 50 | 30 | 20 |
| s202205 | 76 | 87 | 0 |
| s202206 | 31 | 0 | 0 |
+---------+--------+--------+--------+
4. Sql sentence
- Search for courses 1⽐ Course 2 achievement ⾼ All my studies ⽣ Student number and grade
select a.s_no, a.s_score Chinese language and literature , b.s_score mathematics from
(select s_no, s_score from score where c_no ='c202201') a,
(select s_no, s_score from score where c_no ='c202202') b
where a.s_no =b.s_no
and a.s_score >b.s_score;
- Query exists "c202201" The student information of the course and its "c202201"、"c202201" The results of the course
select a.* , b.s_score Language score ,c.s_score Math scores from student a
left join score b on a.s_no = b.s_no and b.c_no = 'c202201'
left join score c on a.s_no = c.s_no and c.c_no = 'c202202'
where b.s_score is not null;
- Query the number of students who have passed the average score 、 Name and average score ;
select a.s_no , a.s_name , cast(avg(b.s_score) as decimal(18,2)) avg_score
from student a , score b
where a. s_no = b.s_no
group by a. s_no
having avg(b.s_score) >= 60
order by a.s_no;
- Query the information and average score of students with grades
select b.s_no , b.s_name , avg(a.s_score) avg_score
from score a left join student b
on a.s_no = b.s_no
group by a.s_no
order by a.s_no;
- Query the student information whose name contains Feng
select * from student where s_name REGEXP ' wind *';
select * from student where s_name like '% wind %';
- Check the student numbers of all students 、 full name 、 Number of courses selected 、 Total score ;
select s.s_no,s.s_name,count(sc.c_no),sum(sc.s_score)
from Student s
left join score sc
on s.s_no= sc.s_no
group by s.s_no;
- I didn't learn how to query “ Zhang San ” The student number of the teacher's classmate 、 full name
select s.s_no, s.s_name
from student s
where s_no not in
(select distinct( sc.s_no) from score sc, course c, teacher t
where sc.c_no =c.c_no and t.t_no=c.t_no and t.t_name =' Zhang San ');
边栏推荐
- Leetcode: hash table 07 (sum of three numbers)
- 一,数组--滑动窗口问题--长度最小的子数组--水果成篮问题
- 研控电机步进模式
- 对抗攻击与防御 (2):对抗样本的反制策略
- 6 steps to teach you financial data mining preprocessing
- Kotlin practical skills you should know
- Counter attack and defense (1): counter sample generation in image domain
- 正则表达式使用图床
- 论文阅读 (58):Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based...
- [esp8266-01s] get weather, city, Beijing time
猜你喜欢

Pause update Bulletin - walking Pikachu

Regular expression use graph bed

【故障公告】取代 memcached 的 redis 出现问题造成网站故障

Paper reading (48):a Library of optimization algorithms for organizational design

torch学习(一):环境配置

深入理解 padding

VNC Viewer方式的远程连接树莓派

论文阅读 (48):A Library of Optimization Algorithms for Organizational Design
![Wechat applet reports an error [app.json file content error] app json: app. JSON not found](/img/ab/5c27e1bb80ad662d1a220d29c328e0.png)
Wechat applet reports an error [app.json file content error] app json: app. JSON not found

论文阅读 (56):Mutli-features Predction of Protein Translational Modification Sites (任务)
随机推荐
[sword finger offer] 45 Arrange the array into the smallest number
After the Computer College changed its examination, the College of Cyberspace Security also changed its examination! Nanjing University of technology computer postgraduate entrance examination
对抗攻击与防御 (2):对抗样本的反制策略
leetcode刷题:哈希表05 (四数相加 II)
Implementing Domain Driven Design - using ABP framework - repository
An error is reported when latex uses \usepackage{hyperref}: paragraph ended before [email protected] @link was complete
程序员未来会成为非常内卷的职业么?
【華中科技大學】考研初試複試資料分享
《致敬百年巨匠 , 数藏袖珍书票》
如何利用好每天的时间高效复习?
Goframe framework: add tracing Middleware
【Wwise】Wwise嵌入Unity后打包出现没有声音问题
正则表达式使用图床
深入理解 padding
实用电路分析3
2022年在网上办理股票开户安全吗?
剑指 Offer II 092. 翻转字符 / 剑指 Offer II 093. 最长斐波那契数列
Redis cluster
Counter attack and defense (2): counter strategy against samples
Latex编译成功但是无法输出到PDF