Database multi-table query job

Database multi-table query job

insert image description here
insert image description here
insert image description here
Create a database
insert image description here
and insert data

mysql> insert into student values(901,'张老大','男',1985,'计算机系','北京市海淀区'),
    -> (902,'张老二','男',1986,'中文系','北京市昌平市'),
    -> (903,'张三','女',1990,'中文系','湖南省永州市'),                                           -> (904,'李四','男',1990,'英语系','辽宁省阜新市'),                                           -> (905,'王五','女',1991,'英语系','福建省厦门市'),
    -> (906,'王六','男',1988,'计算机系','湖南省衡阳市');

mysql> insert into score values(null,901,'计算机',98),
    -> (null,901,'英语',80),
    -> (null,902,'计算机',65),
    -> (null,902,'中文',88),
    -> (null,903,'中文',95),
    -> (null,904,'计算机',70),
    -> (null,904,'英语',92),
    -> (null,905,'英语',94),
    -> (null,906,'计算机',90),
    -> (null,906,'英语',85);

insert image description here
1. Query all records of the student table

mysql> select * from student;

insert image description here

2. Query the 2nd to 4th records of the student table

mysql> select * from student limit 1,3;

insert image description here

3. Query the student number (id), name
(name) and department (department) information of all students from the student table

mysql> select id as 学号,name as 姓名,department as 院系 from student;

insert image description here

4. Query the information of the students of the computer department and the English department from the student table

mysql> select * from student where department='计算机系' or department='英语系';

insert image description here

5. Query the information of students aged 18~22 from the student table

mysql> select *,year(curdate())-birth as age from student where year(curdate())-birth between 18 and 22;

insert image description here

6. Query how many people are in each department from the student table

mysql> select department as 院系,count(*) as 人数 from student group by department;

insert image description here

7. Query the highest score of each subject from the score table

mysql> select c_name as 科目,max(grade) as 最高分 from score group by c_name;

insert image description here

8. Query Li Si's test subjects (c_name) and test results (grade)

mysql> select name as 姓名,c_name as 科目,grade as 成绩 from student
    -> inner join score on student.id=score.stu_id
    -> where name='李四';

insert image description here

9. Query all student information and test information by connecting

mysql> select * from student iudent inner join score on student.id=score.stu_id;

insert image description here

10. Calculate the total grade of each student

mysql> select name as 姓名,sum(grade) as 总成绩 from student
    -> inner join score on student.id=score.stu_id
    -> group by name;

insert image description here

11. Calculate the average grade for each exam subject

mysql> select avg(grade) from score group by c_name;

insert image description here

12. Query the information of students whose computer scores are lower than 95

mysql> select * from student
    -> inner join (select stu_id,c_name,grade from score where grade<95 and c_name='计算机') as stu
    -> on student.id=stu.stu_id;

insert image description here

13. Query the information of students who take both computer and English exams

Obtain the student ID of the computer and English test at the same time

select * from score where c_name='英语') as sc2 where sc1.stu_id=sc2.stu_id

Get student information by ID

mysql> select * from student inner join (select sc1.stu_id from (select * from score where c_name='计算机') as sc1,(select * from score where c_name='英语') as sc2 where sc1.stu_id=sc2.stu_id) as sc on student.id=sc.stu_id;

insert image description here

14. Sort computer test scores from high to low

mysql> select * from score where c_name='计算机' order by grade desc;

insert image description here

15. Query the student ID number from the student table and score table, and then merge the query results

mysql> select student.id,score.stu_id from student inner join (select distinct stu_id from score) as score on student.id=score.stu_id;

insert image description here

16. Query the name, department, examination subjects and grades of students surnamed Zhang or Wang

mysql> select stu.name as 姓名, stu.department as 院系, score.c_name as 科目,score.grade as  成绩
    -> from score inner join
    -> (select * from student where name like '张%' or name like '王') as stu
    -> on score.stu_id=stu.id;

insert image description here

17. Query the names, ages, departments, examination subjects and grades of students from Hunan

mysql> select stu.name as 姓名,year(curdate())-stu.birth as age,stu.department as 院系,score.c_name as 科目,score.grade as 成绩
    -> from score
    -> inner join
    -> (select * from student where address like '%湖南%') as stu
    -> on score.stu_id=stu.id;

insert image description here

Guess you like

Origin blog.csdn.net/weixin_55822200/article/details/131707252