数据库登录

  1. 主机地址:rm-bp188nr95fk4l9545ao.mysql.rds.aliyuncs.com
  2. 端口号:3306
  3. 用户名:fanmao65
  4. 密码:abc@fanmao65
  5. 数据库:fanmao65_hm

student表

Sno Sname Ssex Sbirthday class
108 曾华 1990/9/1 20033
107 匡明 1989/8/9 20031
101 王丽 1992/8/9 20033
109 李军 1991/7/8 20033
103 王芳 1993/7/9 20031
105 陆君 1990/7/8 20031

course科目表

Cno Cname Tno
3-105 计算机导论 825
3-245 操作系统 804
6-166 数字电路 856
9-888 高等数学 831

score成绩表

Sno Cno Degree
103 3-245 86
105 3-245 75
109 3-245 68
103 3-105 92
105 3-105 88
109 3-105 76
101 3-105 64
107 3-105 91
108 3-105 78
101 6-166 85
107 6-166 79
108 6-166 81

teacher老师表

Tno Tname Tsex Tbirthday Prof Depart
804 李成 1958/12/12 副教授 计算机系
856 张栩 1969/3/12 讲师 电子工程系
825 汪萍 1972/5/5 助教 计算机系
831 柳冰 1977/8/14 助教 电子工程系

建表语句;

  1. drop TABLE if EXISTS student;
  2. create table student(
  3. `sno` int(5) not null,
  4. `sname` varchar(3),
  5. `ssex` varchar(1),`sbirthday` datetime not null,
  6. `class` VARCHAR(10)
  7. );
  8. insert into student(`sno`,`sname`,`ssex`,`sbirthday`,`class`)
  9. values
  10. ("108","曾华","男","1990/9/1","20033"),
  11. ("107","匡明","男","1989/8/9","20031"),
  12. ("101","王丽","女","1992/8/9","20033"),
  13. ("109","李军","男","1991/7/8","20033"),
  14. ("103","王芳","女","1993/7/9","20031"),
  15. ("105","陆君","男","1990/7/8","20031");
  16. drop table if EXISTS course;
  17. create table course(
  18. `cno` VARCHAR(11) not null,
  19. `cname` varchar(255) not null,
  20. `tno` int(11)not null,
  21. primary key (`cno`)
  22. );
  23. insert into course
  24. (`cno`,`cname`,`tno`)
  25. VALUES
  26. ("3-105","计算机导论","825"),
  27. ("3-245","操作系统","804"),
  28. ("6-166","数字电路","856"),
  29. ("9-888","高等数学","831");
  30. DROP TABLE if EXISTS score;
  31. create table score(
  32. `sno` VARCHAR(10) not null,
  33. `cno` varchar(20) not null,
  34. `degree` varchar(15) not NULL
  35. );
  36. INSERT INTO `score`
  37. (`Sno`, `Cno`,`Degree`)
  38. values
  39. ("103","3-245","86"),
  40. ("105","3-245","75"),
  41. ("109","3-245","68"),
  42. ("103","3-105","92"),
  43. ("105","3-105","88"),
  44. ("109","3-105","76"),
  45. ("101","3-105","64"),
  46. ("107","3-105","91"),
  47. ("108","3-105","78"),
  48. ("101","6-166","85"),
  49. ("107","6-166","79"),
  50. ("108","6-166","81");
  51. DROP TABLE if EXISTS teacher;
  52. create table teacher
  53. (`tno` VARCHAR(11) not null,
  54. `tname` varchar(50) not null,
  55. `tsex` varchar(1) null,
  56. `tbirthday` datetime(6) not null,
  57. `prof` varchar(50) not null ,
  58. `depart` varchar(50) not null)
  59. ;
  60. insert into `teacher`
  61. (`tno`,`tname`,`tsex`,`tbirthday`,`prof`,`depart`)
  62. values
  63. ('804','李成','男','1958/12/1','副教授','计算机系'),
  64. ('856','张翔','男','1969/3/12','讲师','电子工程系'),
  65. ('825','汪萍','女','1972/5/5','助教','计算机系'),
  66. ('831','柳冰','女','1977/8/14','助教','电子工程系');

升级版数据
fanmao65_hm.sql


  1. 查询教师表Depart列去重显示; ```sql select distinct(depart) from teacher;

SELECT depart FROM teacher GROUP BY depart;

  1. 2. 查询student表中班级为`20031`班或性别为女的同学记录;
  2. ```sql
  3. SELECT * from student
  4. WHERE ssex="女" or class="20031";
  1. 查询20031班学生人数;

    1. SELECT class, COUNT(*) FROM student
    2. WHERE class="20031"
  2. 以Cno升序Degree降序查询Score表的学号,学生姓名,学科名称,成绩;

    1. select student.sno, sname, cname,degree FROM student
    2. INNER JOIN score
    3. ON score.sno = student.sno
    4. INNER JOIN course
    5. ON course.cno = score.cno
    6. ORDER BY course.cno asc, degree desc;
  3. 查询Score表中单科最高分的学生学号和课程号(子查询或排序); ```sql — 最高分 SELECT max(degree) FROM score;

— 根据最高分找人

select student.sno, sname,cname,degree from score INNER JOIN student on student.sno = score.sno INNER JOIN course ON course.cno = score.cno

WHERE degree = (SELECT max(degree) FROM score)

  1. 6. 查询`20033`班得分在80分以上或者等于 606162的学生;
  2. ```sql
  3. select student.sno, sname,cname,degree,class from score
  4. INNER JOIN student
  5. on student.sno = score.sno
  6. INNER JOIN course
  7. ON course.cno = score.cno
  8. where class="20033" and (degree>80 or degree in (60,61,62))
  1. 查询所有班级的名称,以及每个班级中人数和平均分; ```sql — 1. 算出每个人的平均分

SELECT sno,avg(degree) from score GROUP BY sno;

— 2. 跟班级

SELECT class, COUNT(sname), avg(个人平均) from student LEFT JOIN ( SELECT sno,avg(degree) 个人平均 from score GROUP BY sno) as tmp ON tmp.sno = student.sno GROUP BY class;

  1. 8. 查询总分最高的人员姓名,班级,总分;
  2. ```sql
  3. -- 1. 每个人的总分
  4. SELECT sno,sum(degree) from score
  5. GROUP BY sno;
  6. --
  7. SELECT student.sno,class,sname,总分 from student
  8. INNER JOIN (SELECT score.sno, SUM(degree) 总分 FROM score
  9. GROUP BY sno
  10. HAVING SUM(degree) = (
  11. SELECT sum(degree) as 总分 from score
  12. GROUP BY sno
  13. ORDER BY SUM(degree) desc
  14. LIMIT 1) ) as tmp
  15. ON tmp.sno = student.sno;
  1. select score.sno,sname,class,sum(degree) from student
  2. inner join score
  3. on student.sno=score.sno
  4. group by score.sno,sname,class
  5. having sum(degree)=(select sum(degree) from score
  6. group by sno
  7. order BY sum(degree) desc
  8. limit 1
  9. );

  1. 给你一个这样的数据库表数据 | Y | M | Amount | | —- | —- | —- | | 2020 | 1 | 101 | | 2020 | 2 | 102 | | 2020 | 3 | 103 | | 2021 | 1 | 201 | | 2021 | 2 | 202 | | 2021 | 3 | 203 |

请写一个SQL,得出如下结果:

Y M1 M2 M3
2020 101 102 103
2021 201 202 203
  1. SELECT Y,
  2. MAX(CASE M WHEN 1 THEN Amount ELSE 0 END) as M1,
  3. MAX(CASE M WHEN 2 THEN Amount ELSE 0 END) as M2,
  4. MAX(CASE M WHEN 3 THEN Amount ELSE 0 END) as M3
  5. FROM yma
  6. GROUP BY Y;

简答题

  1. 请分别阐述inner join、left join、right join、full join四种SQL语句的结果差异

    1. inner join: 只显示两个表中共有的部分;
    2. left join: 显示以左表为主,右表中没有显示的部分为Null
    3. right join: 显示以右表为主,左表中没有显示的部分为Null
    4. full join: 左表和右表都不做限制,所有的记录都显示,两表不足的地方均为NULL
  2. 一个表中的ID有多个记录,把所有这个ID的记录查出来,并显示共有多少条记录数

  3. 数据库表记录的增、删、改分别用什么语法?

2021-09-27


  1. 查询 学生名字,考试科目,考试成绩,班级名称,科目对应的老师姓名,系别名称; ```javascript

— 学生名字,考试科目,考试成绩,班级名称,科目对应的老师姓名,系别名称 SELECT sname, cname,degree,class,tname,depart FROM student INNER JOIN score ON score.sno = student.sno INNER JOIN course ON course.cno = score.cno INNER JOIN teacher ON teacher.tno = course.tno;

  1. 2. 查询 每个班级中年龄最大的学生,显示出 班级名,学生名,对应的年龄;
  2. ```javascript
  3. -- 1. 统计每个学生的年龄
  4. SELECT sno,sname, class, YEAR(NOW())-YEAR(sbirthday) as age from student;
  5. -- 2. 每个班最大年龄
  6. SELECT class, MAX(YEAR(NOW())-YEAR(sbirthday)) as age from student
  7. GROUP BY class
  8. -- 3. 两个临时表合并。
  9. SELECT a.class,sname,age from (SELECT class,sname,year(now())-year(sbirthday) age from student) a
  10. inner join (SELECT class,max(year(now())-year(sbirthday)) age1 from student
  11. group by class) b
  12. on a.class=b.class and a.age = b.age1;

image.png


2021-09-28

  1. 统计每个班级中年龄最小的同学姓名,学号,同学所在班级,考试总分。 ```javascript

— 年龄最小的同学 select * from (SELECT a.class,sname, a.sno, age from (SELECT class,sno, sname,year(now())-year(sbirthday) age from student) a inner join (SELECT class,min(year(now())-year(sbirthday)) age1 from student group by class) b on a.class=b.class and a.age = b.age1) as tmp1

— 统计每个同学的总成绩

select * from (SELECT sno,sum(degree) from score GROUP BY sno) as tmp2

— 合并 select * from (SELECT a.class,sname, a.sno, age from (SELECT class,sno, sname,year(now())-year(sbirthday) age from student) a inner join (SELECT class,min(year(now())-year(sbirthday)) age1 from student group by class) b on a.class=b.class and a.age = b.age1) as tmp1 INNER JOIN (SELECT sno,sum(degree) from score GROUP BY sno) as tmp2

ON tmp1.sno = tmp2.sno;

  1. ---
  2. <a name="FR00d"></a>
  3. ## 2021-09-29
  4. 1. 统计一下每个班中`张` 同学的个数。
  5. ```sql
  6. select class,count(*) from student
  7. WHERE sname like "张%"
  8. GROUP BY class
  1. 查询 参加过 汪老师的课程同学人数。
    1. SELECT COUNT(*) from score WHERE cno= (select cno from course
    2. WHERE tno = (select tno FROM teacher
    3. WHERE tname like "汪%"))
    1. SELECT count(*) from teacher,course,score
    2. WHERE teacher.tno = course.tno and score.cno = course.cno and tname like "汪%";

2021-10-06

  1. 查询90后的男生,女生人数; ```json — 90 后

SELECT * from student WHERE YEAR(sbirthday)BETWEEN 1990 and 1999

— 分组 SELECT ssex, count(*) from student WHERE YEAR(sbirthday)BETWEEN 1990 and 1999 GROUP BY ssex;

  1. 2. 统计每个班缺考的人的姓名,班级,缺考科目。
  2. ```json
  3. -- 总共的科目
  4. select COUNT(*) from course;
  5. -- 统计 每个学生考试 少于 4门科目的学生 学号
  6. SELECT sno, COUNT(*) from score
  7. GROUP BY sno
  8. HAVING COUNT(*) < (select COUNT(*) from course)
  9. -- 学生 信息
  10. select * from student
  11. WHERE sno in (
  12. SELECT sno from score
  13. GROUP BY sno
  14. HAVING COUNT(*) < (select COUNT(*) from course)
  15. )
  16. -- 缺考的学生 考试的信息
  17. select * from score
  18. WHERE sno in (
  19. SELECT sno from score
  20. GROUP BY sno
  21. HAVING COUNT(*) < (select COUNT(*) from course)
  22. )
  23. --
  24. SELECT * from course
  25. LEFT JOIN ( select * from score
  26. WHERE sno in (
  27. SELECT sno from score
  28. GROUP BY sno
  29. HAVING COUNT(*) < (select COUNT(*) from course)
  30. ) ) as tmpa
  31. ON tmpa.cno = course.cno
  32. WHERE tmpa.sno =100
  33. select sname,class,cname from (student,course) left JOIN score on course.cno = score.cno and student.sno = score.sno
  34. where degree is null;
  35. SELECT student.sno,sname,class,计算机,操作系统,数字电路,高等数学 from student
  36. INNER JOIN (SELECT sno,
  37. MAX(CASE cno WHEN "3-105" THEN degree ELSE "-" END) as "计算机",
  38. MAX(CASE cno WHEN "3-245" THEN degree ELSE "-" END) as "操作系统",
  39. MAX(CASE cno WHEN "6-166" THEN degree ELSE "-" END) as "数字电路",
  40. MAX(CASE cno WHEN "9-888" THEN degree ELSE "-" END) as "高等数学"
  41. FROM score
  42. GROUP BY sno)as a on student.sno=a.sno