MySQL database exercises

foreword

Hello everyone, I would like to share some MySQL practice questions, all of which are self-organized exam sample questions with answers, which can be used to consolidate knowledge points, and can also be used for review before the exam.

1. Create a database

1. Create a database called Mytest, and create the following 5 tables in the database. The relationship and fields between the tables are shown in the figure:

insert image description here
Create a database:

create database Mytest;

Create the teacher table:

create table teacher(
id smallint(4) not null primary key,
name varchar(20)
);

Create class table :

create table class(
id smallint(6) not null primary key,
name varchar(50)
);

Create course table

create table course(
id smallint(3) not null primary key,
name varchar(100), 
teacher_id smallint(6) not null,
foreign key(teacher_id) references teacher(id)
);

create student table

create table student(
id smallint(6) not null primary key,
name varchar(20),
gender char(2),
class_id smallint(6) not null,
foreign key(class_id) references class(id)
);

Create score table

create table score(
id int(11),
student_id smallint(6) not null,
course_id smallint(3) not null,
mark tinyint(4),
foreign key(student_id) references student(id),
foreign key(course_id) references course(id),
primary key(student_id,course_id)
);

2. Insert the following data

insert image description here
There are two ways to insert data (two tables are shown):

1. Single line insert

Example: insert class table data

insert into class values(1,"软件工程1班");
insert into class values(2,"计算机科学技术1班");
insert into class values(3,"网络工程1班");

2,. batch insert

Example: insert teacher table data

insert into teacher values
(1,"老虎"),
(2,"小马"),
(3,"大牛");

2. Operation database

1. Query the names of all courses and the names of corresponding teachers;

select course.name,teacher.name
from course inner join teacher
on course.teacher_id = teacher.id;

2. Query the student IDs of students whose grades in the course "Data Structure" are lower than those in the course "Java Language";

select shuju.student_id from
(select score.student_id,course.name,score.mark from 
score inner join course on 
score.course_id=course.id where 
course.name="数据结构") as shuju
inner JOIN
(select score.student_id,course.name,score.mark from 
score inner join course on 
score.course_id=course.id where 
course.name="java语言") as java
on shuju.student_id=java.student_id
where shuju .mark<java.mark;

3 Query the id and average score of students whose average score is greater than 65 (reserve two decimal places);

select student_id,round(avg(mark),2) as grade from score
group by student_id having grade>65;

4. Query the names and average grades of students whose average grades are greater than 65 (reserve two decimal places);

select student.name,round(avg(mark),2) 
as grade from score
inner joinstudent
onscore.student_id=student.id
group by student_id having grade>65;

5. Query the names, number of courses and total grades of all students;

select student.name,count(score.course_id) 
as "选课数",sum(score.mark) as "总成绩" from score 
inner join student on score.student_id=student.id
group by student_id;

6. Query the names of students who have not studied the "Daniu" teacher's class;

select name from student 
where id not in(select id from score where course_id in(3,3));

7. Query the names of students who have studied all the courses taught by "Daniu" teacher;

select student.name from 
score inner join student 
on score.student_id=student.id where score.course_id in
(select course.id from course inner join teacher
on course.teacher_id=teacher.id where teacher.name="大牛")
group by score.student_id;

8. Query the names of students whose course scores are less than 60 points;

select name from student where id in
(select student_id from score where mark<60 
group by student_id);

9. Query the names of students who have taken all courses;

select name from student where id in
(select student_id from score 
group by student_id 
having count(1)=(select count(1) from course));

10. Query the names of students who have at least one course that is the same as that of "Xiaocao";

select name as"姓名" from 
student where id in
(select student_id from score where course_id in
(select course_id from score 
inner join student 
on score.student_id=student.id where student.name="小草")
group by student_id)
and name!="小草";

11. Query the names of students who have at least one course different from the courses taken by "Xiaocao";

select name as "姓名" from student where id in
(select student_id from score where course_id in
(select course_id from score 
inner join student 
on score.student_id!=student.id 
where student.name="小草")
group by student_id)
and name!="小草";

12. Query the highest and lowest scores of each subject: display in the following form: course id, highest score, lowest score;

select course_id as "课程id",
max(mark) as "最高分",
min(mark) as "最低分" 
from score group by course_id;

13. Query the student ID and name of the students who have only taken one course;

select 
student_id as"学生学号",
student.name as"学生姓名" from score
inner join student 
on score.student_id=student.id
group by student_id 
having count(course_id)=1;

14. Query the average grade of each course, and the results are arranged in ascending order of the average grade, and when the average grade is the same, they are arranged in descending order of the course id.

select course.name as"课程名称",
avg(mark) as"平均成绩" from score
inner join course 
on score.course_id=course.id
group by course_id
order by avg(mark) asc,
course_id desc;

15. Display the grades of the three courses of "database principle", "java language" and "C language" of all students in reverse order of average grade, and display them in the following format: student id, database principle, java language, C language, number of courses, Average score; (advanced applications are more difficult)

  1. First query the database principle course score of a single student
select mark as"分数" from score 
left join course 
on score.course_id = course.id
where course.name = "数据库原理" and score.student_id=1;
  1. Use the result of the above query as a column field to get the final result
select student_id as"学生学号",

(select mark from score 
left join course 
on score.course_id = course.id 
where course.name = "数据库原理"
and score.student_id=sc.student_id) 
as "数据库原理",

(select mark from score 
left join course 
on score.course_id = course.id 
where course.name = "java语言" 
and score.student_id=sc.student_id) 
as "java语言",

(select mark from score 
left join course 
on score.course_id = course.id 
where course.name = "C语言" 
and score.student_id=sc.student_id) 
as "C语言",

count(course_id) as "课程数",
avg(mark) as "平均分"
from score as sc
group by student_id 
order by avg(mark) desc;

Guess you like

Origin blog.csdn.net/m0_59420288/article/details/128180217