MySQL -- classic interview questions

zyanwei2018 2022-06-23 18:22:57 阅读数:900

mysqlclassicinterviewquestions

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 ');
版权声明:本文为[zyanwei2018]所创,转载请带上原文链接,感谢。 https://javamana.com/2022/174/202206231725430119.html